Skip to content

Commit

Permalink
Enable TypeScript strict mode
Browse files Browse the repository at this point in the history
Signed-off-by: Arve Knudsen <[email protected]>
  • Loading branch information
aknuds1 committed Jun 3, 2023
1 parent bbcfe6e commit 41915bf
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 26 deletions.
40 changes: 40 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/react": "^18.0.33",
"@types/react-dom": "^18.0.11",
"coveralls": "^3.1.0",
"eslint": "^8.1",
"mocha": "^10.0",
Expand Down
48 changes: 24 additions & 24 deletions test/html-to-react-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {ProcessNodeDefinitions} from '..';
import {booleanAttrs} from './boolattrs';

describe('Html2React', () => {
const parser = new Parser();
const parser = Parser();

describe('parse valid HTML', () => {
it('should return a valid HTML string', () => {
Expand Down Expand Up @@ -185,10 +185,10 @@ describe('Html2React', () => {

const reactComponent = parser.parse(htmlInput);

const children = reactComponent.props.children.flat().filter((c) => {
const children = reactComponent.props.children.flat().filter((c: any) => {
return c.hasOwnProperty('key');
});
const keys = children.map((child) => {
const keys = children.map((child: any) => {
return child.key;
});
deepStrictEqual(keys, ['0', '1', ]);
Expand Down Expand Up @@ -327,8 +327,8 @@ describe('Html2React', () => {
});

describe('with custom processing instructions', () => {
const parser = new Parser();
const processNodeDefinitions = new ProcessNodeDefinitions();
const parser = Parser();
const processNodeDefinitions = ProcessNodeDefinitions();

describe('parse valid HTML', () => {
it('should return nothing with only a single <p> element', () => {
Expand All @@ -337,7 +337,7 @@ describe('Html2React', () => {
return true;
};
const processingInstructions = [{
shouldProcessNode: function (node) {
shouldProcessNode: (node: any) => {
return node.name && node.name !== 'p';
},
processNode: processNodeDefinitions.processDefaultNode,
Expand All @@ -359,7 +359,7 @@ describe('Html2React', () => {
};

const processingInstructions = [{
shouldProcessNode: function (node) {
shouldProcessNode: (node: any) => {
return node.type === 'text' || node.name !== 'p';
},
processNode: processNodeDefinitions.processDefaultNode,
Expand All @@ -381,16 +381,16 @@ describe('Html2React', () => {
const processingInstructions = [
{
replaceChildren: true,
shouldProcessNode: function (node) {
shouldProcessNode: (node: any) => {
return (node.attribs || {})['data-test'] === 'foo';
},
processNode: function (node, children, index) {
processNode: (node: any, children: any, index: number) => {
return React.createElement('h1', {key: index,}, 'Heading');
},
},
{
// Anything else
shouldProcessNode: function (node) {
shouldProcessNode: (node: any) => {
return true;
},
processNode: processNodeDefinitions.processDefaultNode,
Expand All @@ -416,16 +416,16 @@ describe('Html2React', () => {
const processingInstructions = [
{
// Custom <h1> processing
shouldProcessNode: function (node) {
shouldProcessNode: (node: any) => {
return node.parent && node.parent.name &&
node.parent.name === 'h1';
},
processNode: function (node, children) {
processNode: (node: any, children: any) => {
return node.data.toUpperCase();
},
}, {
// Anything else
shouldProcessNode: function (node) {
shouldProcessNode: (node: any) => {
return true;
},
processNode: processNodeDefinitions.processDefaultNode,
Expand All @@ -440,7 +440,7 @@ describe('Html2React', () => {
it('should return false in case of invalid node', () => {
const htmlInput = '<p></p>';
const processingInstructions = [{
shouldProcessNode: function (node) { return true; },
shouldProcessNode: (node: any) => { return true; },
processNode: processNodeDefinitions.processDefaultNode,
}, ];
const reactComponent = parser.parseWithInstructions(htmlInput,
Expand All @@ -453,10 +453,10 @@ describe('Html2React', () => {
const htmlInput = '<div> <p></p> <p></p> </div>';

const processingInstructions = [{
shouldProcessNode: function (node) { return true; },
shouldProcessNode: (node: any) => { return true; },
processNode: processNodeDefinitions.processDefaultNode,
}, ];
const reactComponent = parser.parseWithInstructions(htmlInput, function (node) {
const reactComponent = parser.parseWithInstructions(htmlInput, (node: any) => {
// skip whitespace text nodes to clean up children
if (node.type === 'text') {
return node.data.trim() !== '';
Expand Down Expand Up @@ -492,42 +492,42 @@ describe('Html2React', () => {
// (i.e., it will only affect nodes touched by the previous preprocessor).
const preprocessingInstructions = [
{
shouldPreprocessNode: function (node) {
shouldPreprocessNode: (node: any) => {
return (node.attribs || {})['data-process'] === 'shared';
},
preprocessNode: function (node) {
preprocessNode: (node: any) => {
if (node.attribs == null) {
node.attribs = {};
}
node.attribs['data-preprocessed'] = 'true';
},
},
{
shouldPreprocessNode: function (node) {
shouldPreprocessNode: (node: any) => {
return (node.attribs || {})['data-preprocessed'] === 'true';
},
preprocessNode: function (node) {
preprocessNode: (node: any) => {
node.attribs.id = `preprocessed-${node.attribs.id}`;
},
},
];
const processingInstructions = [
{
shouldProcessNode: function (node) {
shouldProcessNode: (node: any) => {
return (node.attribs || {}).id === 'preprocessed-first';
},
processNode: function (node, children, index) {
processNode: (node: any, children: any, index: number) => {
return React.createElement('h1', {
key: index,
id: node.attribs.id,
}, 'First');
},
},
{
shouldProcessNode: function (node) {
shouldProcessNode: (node: any) => {
return (node.attribs || {}).id === 'preprocessed-second';
},
processNode: function (node, children, index) {
processNode: (node: any, children: any, index: number) => {
return React.createElement('h2', {
key: index,
id: node.attribs.id,
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "types",
"declarationMap": true
"declarationMap": true,
"strict": true
}
}
2 changes: 1 addition & 1 deletion types/is-valid-node-definitions.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export function alwaysValid(): boolean;
//# sourceMappingURL=is-valid-node-definitions.d.ts.map
//# sourceMappingURL=is-valid-node-definitions.d.ts.map

0 comments on commit 41915bf

Please sign in to comment.