diff --git a/package-lock.json b/package-lock.json index 38dca00..2ab45c3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,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", @@ -750,6 +752,38 @@ "dev": true, "peer": true }, + "node_modules/@types/prop-types": { + "version": "15.7.5", + "resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.5.tgz", + "integrity": "sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==", + "dev": true + }, + "node_modules/@types/react": { + "version": "18.0.33", + "resolved": "https://registry.npmjs.org/@types/react/-/react-18.0.33.tgz", + "integrity": "sha512-sHxzVxeanvQyQ1lr8NSHaj0kDzcNiGpILEVt69g9S31/7PfMvNCKLKcsHw4lYKjs3cGNJjXSP4mYzX43QlnjNA==", + "dev": true, + "dependencies": { + "@types/prop-types": "*", + "@types/scheduler": "*", + "csstype": "^3.0.2" + } + }, + "node_modules/@types/react-dom": { + "version": "18.0.11", + "resolved": "https://registry.npmjs.org/@types/react-dom/-/react-dom-18.0.11.tgz", + "integrity": "sha512-O38bPbI2CWtgw/OoQoY+BRelw7uysmXbWvw3nLWO21H1HSh+GOlqPuXshJfjmpNlKiiSDG9cc1JZAaMmVdcTlw==", + "dev": true, + "dependencies": { + "@types/react": "*" + } + }, + "node_modules/@types/scheduler": { + "version": "0.16.3", + "resolved": "https://registry.npmjs.org/@types/scheduler/-/scheduler-0.16.3.tgz", + "integrity": "sha512-5cJ8CB4yAx7BH1oMvdU0Jh9lrEXyPkar6F9G/ERswkCuvP4KQZfZkSjcMbAICCpQTN4OuZn8tz0HiKv9TGZgrQ==", + "dev": true + }, "node_modules/acorn": { "version": "8.8.2", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.8.2.tgz", @@ -1234,6 +1268,12 @@ "node": ">= 8" } }, + "node_modules/csstype": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.2.tgz", + "integrity": "sha512-I7K1Uu0MBPzaFKg4nI5Q7Vs2t+3gWWW648spaF+Rg7pI9ds18Ugn+lvg4SHczUdKlHI5LWBXyqfS8+DufyBsgQ==", + "dev": true + }, "node_modules/dashdash": { "version": "1.14.1", "resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz", diff --git a/package.json b/package.json index eb32da6..bc31b02 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/html-to-react-tests.ts b/test/html-to-react-tests.ts index 65873ca..b30b947 100644 --- a/test/html-to-react-tests.ts +++ b/test/html-to-react-tests.ts @@ -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', () => { @@ -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', ]); @@ -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

element', () => { @@ -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, @@ -359,7 +359,7 @@ describe('Html2React', () => { }; const processingInstructions = [{ - shouldProcessNode: function (node) { + shouldProcessNode: (node: any) => { return node.type === 'text' || node.name !== 'p'; }, processNode: processNodeDefinitions.processDefaultNode, @@ -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, @@ -416,16 +416,16 @@ describe('Html2React', () => { const processingInstructions = [ { // Custom

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, @@ -440,7 +440,7 @@ describe('Html2React', () => { it('should return false in case of invalid node', () => { const htmlInput = '

'; const processingInstructions = [{ - shouldProcessNode: function (node) { return true; }, + shouldProcessNode: (node: any) => { return true; }, processNode: processNodeDefinitions.processDefaultNode, }, ]; const reactComponent = parser.parseWithInstructions(htmlInput, @@ -453,10 +453,10 @@ describe('Html2React', () => { const htmlInput = '

'; 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() !== ''; @@ -492,10 +492,10 @@ 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 = {}; } @@ -503,20 +503,20 @@ describe('Html2React', () => { }, }, { - 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, @@ -524,10 +524,10 @@ describe('Html2React', () => { }, }, { - 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, diff --git a/tsconfig.json b/tsconfig.json index 95df7f6..c62b3eb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,7 @@ "declaration": true, "emitDeclarationOnly": true, "outDir": "types", - "declarationMap": true + "declarationMap": true, + "strict": true } } diff --git a/types/is-valid-node-definitions.d.ts b/types/is-valid-node-definitions.d.ts index d52871e..34c9ab1 100644 --- a/types/is-valid-node-definitions.d.ts +++ b/types/is-valid-node-definitions.d.ts @@ -1,2 +1,2 @@ export function alwaysValid(): boolean; -//# sourceMappingURL=is-valid-node-definitions.d.ts.map \ No newline at end of file +//# sourceMappingURL=is-valid-node-definitions.d.ts.map