diff --git a/.eslintrc.jest.yml b/.eslintrc.jest.yml index 7c48de7..4340ddd 100644 --- a/.eslintrc.jest.yml +++ b/.eslintrc.jest.yml @@ -1,4 +1,5 @@ env: + commonjs: true es2021: true es2022: true jest: true diff --git a/.eslintrc.react.yml b/.eslintrc.react.yml index 923e17c..9b071d7 100644 --- a/.eslintrc.react.yml +++ b/.eslintrc.react.yml @@ -1,5 +1,6 @@ extends: - plugin:react/recommended + - plugin:react/jsx-runtime plugins: - react settings: diff --git a/packages/integration-test/babel.config.json b/packages/integration-test/babel.config.json index 29970b7..3c78db6 100644 --- a/packages/integration-test/babel.config.json +++ b/packages/integration-test/babel.config.json @@ -1,6 +1,11 @@ { "presets": [ - "@babel/preset-react", + [ + "@babel/preset-react", + { + "runtime": "automatic" + } + ], [ "@babel/preset-env", { diff --git a/packages/integration-test/jest.config.json b/packages/integration-test/jest.config.json index f15ada2..abfa91c 100644 --- a/packages/integration-test/jest.config.json +++ b/packages/integration-test/jest.config.json @@ -4,14 +4,26 @@ "\\.cjsx?$": [ "babel-jest", { - "presets": ["@babel/preset-react"] + "presets": [ + [ + "@babel/preset-react", + { + "runtime": "automatic" + } + ] + ] } ], "\\.mjsx?$": [ "babel-jest", { "presets": [ - "@babel/preset-react", + [ + "@babel/preset-react", + { + "runtime": "automatic" + } + ], [ "@babel/preset-env", { diff --git a/packages/integration-test/tsconfig.json b/packages/integration-test/tsconfig.json new file mode 100644 index 0000000..14b2c3e --- /dev/null +++ b/packages/integration-test/tsconfig.json @@ -0,0 +1,6 @@ +// This configuration file is for VSCode only. +{ + "compilerOptions": { + "jsx": "react-jsx" + } +} diff --git a/packages/pages/package.json b/packages/pages/package.json index 1abe6f7..d290cb7 100644 --- a/packages/pages/package.json +++ b/packages/pages/package.json @@ -3,7 +3,7 @@ "version": "0.0.0-0", "description": "", "scripts": { - "build": "esbuild --bundle --entry-names=[name]/[ext]/main --minify --outdir=./public/static/ --sourcemap app=./src/app/index.tsx", + "build": "esbuild --bundle --entry-names=[name]/[ext]/main --jsx=automatic --minify --outdir=./public/static/ --sourcemap app=./src/app/index.tsx", "bump": "npm run bump:prod && npm run bump:dev && npm run bump:auditfix", "bump:auditfix": "npm audit fix || exit 0", "bump:dev": "PACKAGES_TO_BUMP=$(cat package.json | jq -r '.localPeerDependencies // {} as $L | .devDependencies // {} | to_entries | map(select(.key as $K | $L | has($K) | not)) | map(.key + \"@latest\") | join(\" \")') && [ ! -z \"$PACKAGES_TO_BUMP\" ] && npm install $PACKAGES_TO_BUMP || true", diff --git a/packages/pages/src/tsconfig.json b/packages/pages/src/tsconfig.json index 4ac3c0b..9ef1f2f 100644 --- a/packages/pages/src/tsconfig.json +++ b/packages/pages/src/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "esModuleInterop": true, - "jsx": "react", + "jsx": "react-jsx", "lib": ["DOM", "ESNext", "WebWorker"], "moduleResolution": "Bundler", "noEmit": true, diff --git a/packages/use-ref-from/__tests__/__setup__/typingTestTransformer.js b/packages/use-ref-from/__tests__/__setup__/typingTestTransformer.js index f332d09..7d7079d 100644 --- a/packages/use-ref-from/__tests__/__setup__/typingTestTransformer.js +++ b/packages/use-ref-from/__tests__/__setup__/typingTestTransformer.js @@ -1,16 +1,21 @@ // Notes: to test changes in this file, run "jest" with "--no-cache" argument. const run = ({ filename }) => { + const fs = require('fs/promises'); + const { extname } = require('path'); const typeScript = require('typescript'); - function compile(...filenames) { - const program = typeScript.createProgram(filenames, { - allowSyntheticDefaultImports: true, - jsx: typeScript.JsxEmit.React, - noEmit: true, - skipLibCheck: true, - strict: true - }); + const TS_EXPECT_ERROR = /(\/\/\s+)(@ts-expect-error)[\s+(.*)]/gu; + const TSCONFIG = { + allowSyntheticDefaultImports: true, + jsx: typeScript.JsxEmit.React, + noEmit: true, + skipLibCheck: true, + strict: true + }; + + async function compile(filename) { + const program = typeScript.createProgram([filename], TSCONFIG); const emitResult = program.emit(); const allDiagnostics = typeScript.getPreEmitDiagnostics(program).concat(emitResult.diagnostics); @@ -27,11 +32,43 @@ const run = ({ filename }) => { }); } - if (filename.includes('fail')) { - test(`Compile ${filename} should fail`, () => expect(() => compile(filename)).toThrow()); - } else { - test(`Compile ${filename} should succeed`, () => compile(filename)); + async function checkExpectError(filename) { + const sourceText = await fs.readFile(filename, 'utf-8'); + const sourceTextWithoutExpectError = sourceText.replace(TS_EXPECT_ERROR, '$1'); + + const extension = extname(filename); + const tempFilename = filename.substring(0, filename.length - extension.length) + `.tmp${extension}`; + + await fs.writeFile(tempFilename, sourceTextWithoutExpectError); + + try { + const program = typeScript.createProgram([tempFilename], TSCONFIG); + + const emitResult = program.emit(); + const allDiagnostics = typeScript.getPreEmitDiagnostics(program).concat(emitResult.diagnostics); + + allDiagnostics.forEach(({ file, messageText, start }) => { + if (file && start) { + const { line } = file.getLineAndCharacterOfPosition(start); + const message = typeScript.flattenDiagnosticMessageText(messageText, '\n'); + + const expectedErrorLine = file.getFullText().split('\n')[line - 1]; + const expectedError = expectedErrorLine?.replace(/\s*\/\/\s+/u, '').trim(); + + expect(message).toEqual(expect.stringContaining(expectedError)); + } else { + throw new Error(typeScript.flattenDiagnosticMessageText(messageText, '\n')); + } + }); + } finally { + fs.unlink(tempFilename); + } } + + describe(filename, () => { + test('should succeed', () => compile(filename)); + test('should have @ts-expect-error describing compile errors correctly', () => checkExpectError(filename)); + }); }; module.exports = { diff --git a/packages/use-ref-from/__tests__/types/.gitignore b/packages/use-ref-from/__tests__/types/.gitignore new file mode 100644 index 0000000..5ad8225 --- /dev/null +++ b/packages/use-ref-from/__tests__/types/.gitignore @@ -0,0 +1 @@ +*.tmp.tsx diff --git a/packages/use-ref-from/babel.commonjs.config.json b/packages/use-ref-from/babel.commonjs.config.json index 2e819ae..497414e 100644 --- a/packages/use-ref-from/babel.commonjs.config.json +++ b/packages/use-ref-from/babel.commonjs.config.json @@ -10,8 +10,18 @@ ] ], "presets": [ - "@babel/preset-react", - "@babel/preset-typescript", + [ + "@babel/preset-react", + { + "runtime": "automatic" + } + ], + [ + "@babel/preset-typescript", + { + "allowDeclareFields": true + } + ], [ "@babel/preset-env", { diff --git a/packages/use-ref-from/babel.esmodules.config.json b/packages/use-ref-from/babel.esmodules.config.json index a7ff394..100c677 100644 --- a/packages/use-ref-from/babel.esmodules.config.json +++ b/packages/use-ref-from/babel.esmodules.config.json @@ -10,8 +10,18 @@ ] ], "presets": [ - "@babel/preset-react", - "@babel/preset-typescript", + [ + "@babel/preset-react", + { + "runtime": "automatic" + } + ], + [ + "@babel/preset-typescript", + { + "allowDeclareFields": true + } + ], [ "@babel/preset-env", { diff --git a/packages/use-ref-from/jest.config.json b/packages/use-ref-from/jest.config.json index 7fe1c38..6a7ae9f 100644 --- a/packages/use-ref-from/jest.config.json +++ b/packages/use-ref-from/jest.config.json @@ -1,13 +1,23 @@ { - "testPathIgnorePatterns": ["/__tests__/__setup__/", "/lib/", "/node_modules/", "/__types__/", "\\.pnp\\.[^\\/]+$"], + "testPathIgnorePatterns": ["/__setup__/", "/lib/", "/node_modules/", "/__types__/", "\\.pnp\\.[^\\/]+$"], "transform": { "/__tests__/types/": ["/__tests__/__setup__/typingTestTransformer.js"], "\\.[jt]sx?$": [ "babel-jest", { "presets": [ - "@babel/preset-react", - "@babel/preset-typescript", + [ + "@babel/preset-react", + { + "runtime": "automatic" + } + ], + [ + "@babel/preset-typescript", + { + "allowDeclareFields": true + } + ], [ "@babel/preset-env", { @@ -21,5 +31,6 @@ "sourceMaps": true } ] - } + }, + "watchPathIgnorePatterns": ["\\.tmp\\."] } diff --git a/packages/use-ref-from/package.json b/packages/use-ref-from/package.json index c77b6b8..03900f3 100644 --- a/packages/use-ref-from/package.json +++ b/packages/use-ref-from/package.json @@ -48,7 +48,6 @@ "precommit:typescript:production": "tsc --noEmit --project ./src/tsconfig.precommit.production.json", "precommit:typescript:test": "tsc --noEmit --project ./src/tsconfig.precommit.test.json", "prepack": "cp ../../CHANGELOG.md . && cp ../../LICENSE . && cp ../../README.md .", - "start": "esbuild --bundle --outfile=./public/main.js --servedir=./public --sourcemap ./scenarios/index.jsx", "test": "jest" }, "repository": { diff --git a/packages/use-ref-from/src/tsconfig.declaration.commonjs.json b/packages/use-ref-from/src/tsconfig.declaration.commonjs.json index c24128c..69260cb 100644 --- a/packages/use-ref-from/src/tsconfig.declaration.commonjs.json +++ b/packages/use-ref-from/src/tsconfig.declaration.commonjs.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "declarationDir": "../lib/commonjs-types/", + "declarationDir": "../lib/commonjs/", "module": "CommonJS", "moduleResolution": "Node10" }, diff --git a/packages/use-ref-from/src/tsconfig.declaration.esmodules.json b/packages/use-ref-from/src/tsconfig.declaration.esmodules.json index c8a7528..ab1876a 100644 --- a/packages/use-ref-from/src/tsconfig.declaration.esmodules.json +++ b/packages/use-ref-from/src/tsconfig.declaration.esmodules.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "declarationDir": "../lib/esmodules-types/", + "declarationDir": "../lib/esmodules/", "module": "ESNext", "moduleResolution": "Bundler" }, diff --git a/packages/use-ref-from/src/tsconfig.declaration.json b/packages/use-ref-from/src/tsconfig.declaration.json index 153e6cc..4438a06 100644 --- a/packages/use-ref-from/src/tsconfig.declaration.json +++ b/packages/use-ref-from/src/tsconfig.declaration.json @@ -3,9 +3,9 @@ "declaration": true, "emitDeclarationOnly": true, "esModuleInterop": true, - "jsx": "react", + "jsx": "react-jsx", "noEmit": false }, - "exclude": ["**/*.spec.*", "**/*.test.*", "__test__/**/*"], + "exclude": ["**/*.spec.*", "**/*.test.*", "__tests__/**/*"], "extends": "@tsconfig/strictest/tsconfig.json" } diff --git a/packages/use-ref-from/src/tsconfig.json b/packages/use-ref-from/src/tsconfig.json new file mode 100644 index 0000000..de8ceeb --- /dev/null +++ b/packages/use-ref-from/src/tsconfig.json @@ -0,0 +1,12 @@ +// This configuration file is for VSCode only. +{ + "compilerOptions": { + "esModuleInterop": true, + "jsx": "react-jsx", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "noEmit": true, + "strict": true + }, + "extends": "@tsconfig/strictest/tsconfig.json" +} diff --git a/packages/use-ref-from/src/tsconfig.precommit.production.json b/packages/use-ref-from/src/tsconfig.precommit.production.json index 1cbac25..2fb7db6 100644 --- a/packages/use-ref-from/src/tsconfig.precommit.production.json +++ b/packages/use-ref-from/src/tsconfig.precommit.production.json @@ -1,7 +1,7 @@ { "compilerOptions": { "esModuleInterop": true, - "jsx": "react", + "jsx": "react-jsx", "module": "NodeNext", "moduleResolution": "NodeNext", "noEmit": true, diff --git a/packages/use-ref-from/src/tsconfig.precommit.test.json b/packages/use-ref-from/src/tsconfig.precommit.test.json index 55eb86c..330776d 100644 --- a/packages/use-ref-from/src/tsconfig.precommit.test.json +++ b/packages/use-ref-from/src/tsconfig.precommit.test.json @@ -1,7 +1,7 @@ { "compilerOptions": { "esModuleInterop": true, - "jsx": "react", + "jsx": "react-jsx", "module": "NodeNext", "moduleResolution": "NodeNext", "noEmit": true,