From 7f3ac1bc196b139b553867a919114235002f646d Mon Sep 17 00:00:00 2001 From: Pelle Wessman Date: Mon, 23 Sep 2024 11:23:03 +0200 Subject: [PATCH] [New] add type generation --- .eslintrc | 1 + .github/workflows/type-check.yml | 61 ++++++++++++++++++++++++++++ .gitignore | 6 +++ CHANGELOG.md | 5 +++ build.tsconfig.json | 12 ++++++ lib/rules/jsx-no-literals.js | 5 +-- lib/util/makeNoMethodSetStateRule.js | 7 ++++ package.json | 7 +++- test-published-types/.npmrc | 1 + test-published-types/index.js | 12 ++++++ test-published-types/package.json | 9 ++++ test-published-types/tsconfig.json | 12 ++++++ 12 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/type-check.yml create mode 100644 build.tsconfig.json create mode 100644 test-published-types/.npmrc create mode 100644 test-published-types/index.js create mode 100644 test-published-types/package.json create mode 100644 test-published-types/tsconfig.json diff --git a/.eslintrc b/.eslintrc index b77f8a94e7..fd3ae2193c 100644 --- a/.eslintrc +++ b/.eslintrc @@ -16,6 +16,7 @@ "ignorePatterns": [ "coverage/", ".nyc_output/", + "test-published-types/", "tests/fixtures/flat-config/" ], "rules": { diff --git a/.github/workflows/type-check.yml b/.github/workflows/type-check.yml new file mode 100644 index 0000000000..c0e6b4d033 --- /dev/null +++ b/.github/workflows/type-check.yml @@ -0,0 +1,61 @@ +name: "Types: check published types" + +on: [pull_request, push] + +permissions: + contents: read + +jobs: + test: + name: TS ${{ matrix.ts_version }}, "${{ matrix.ts_lib }}" + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + ts_version: + # The official ESLint types are not compatible with TS 3.9 + # - 3.9 + - '4.0' + - 4.1 + - 4.2 + - 4.3 + - 4.4 + - 4.5 + - '5.0' + - 5.5 + - 5.6 + ts_lib: + - es2015 + - es2015,dom + - es2020 + - esnext + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + show-progress: false + + - uses: ljharb/actions/node/install@main + name: 'nvm install lts/* && npm install' + with: + node-version: 'lts/*' + skip-ls-check: true + + - name: build types + run: npm run build-types + + - name: npm install working directory + run: npm install + working-directory: test-published-types + + - name: install typescript version ${{ matrix.ts_version }} + run: npm install --no-save typescript@${{ matrix.ts_version }} + working-directory: test-published-types + + - name: show installed typescript version + run: npm list typescript --depth=0 + working-directory: test-published-types + + - name: check types with lib "${{ matrix.ts_lib }}" + run: npx tsc --lib ${{ matrix.ts_lib }} + working-directory: test-published-types diff --git a/.gitignore b/.gitignore index 453b7fd5f6..43605edffc 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,9 @@ package-lock.json yarn.lock .npmignore + +/lib/**/*.d.ts +/lib/**/*.d.ts.map +!/lib/types.d.ts +/index.d.ts +/index.d.ts.map diff --git a/CHANGELOG.md b/CHANGELOG.md index edfba39195..eb4aa16c0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange ## Unreleased +### Added +* add type generation ([#3830][] @voxpelli) + +[#3830]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3830 + ## [7.36.1] - 2024.09.12 ### Fixed diff --git a/build.tsconfig.json b/build.tsconfig.json new file mode 100644 index 0000000000..df874658e8 --- /dev/null +++ b/build.tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "./tsconfig", + "files": [ + "index.js" + ], + "compilerOptions": { + "declaration": true, + "declarationMap": true, + "noEmit": false, + "emitDeclarationOnly": true + } +} diff --git a/lib/rules/jsx-no-literals.js b/lib/rules/jsx-no-literals.js index 1d4a30835f..230d33a18d 100644 --- a/lib/rules/jsx-no-literals.js +++ b/lib/rules/jsx-no-literals.js @@ -182,9 +182,8 @@ const elementOverrides = { }, }; -/** @type {import('eslint').Rule.RuleModule} */ module.exports = { - meta: { + meta: /** @type {import('eslint').Rule.RuleModule["meta"]} */ ({ docs: { description: 'Disallow usage of string literals in JSX', category: 'Stylistic Issues', @@ -202,7 +201,7 @@ module.exports = { ), additionalProperties: false, }], - }, + }), create(context) { /** @type {RawConfig} */ diff --git a/lib/util/makeNoMethodSetStateRule.js b/lib/util/makeNoMethodSetStateRule.js index faee6de163..b6c55d5cf6 100644 --- a/lib/util/makeNoMethodSetStateRule.js +++ b/lib/util/makeNoMethodSetStateRule.js @@ -44,6 +44,12 @@ function shouldBeNoop(context, methodName) { && !testReactVersion(context, '999.999.999'); // for when the version is not specified } +// eslint-disable-next-line valid-jsdoc +/** + * @param {string} methodName + * @param {(context: import('eslint').Rule.RuleContext) => boolean} [shouldCheckUnsafeCb] + * @returns {import('eslint').Rule.RuleModule} + */ module.exports = function makeNoMethodSetStateRule(methodName, shouldCheckUnsafeCb) { return { meta: { @@ -90,6 +96,7 @@ module.exports = function makeNoMethodSetStateRule(methodName, shouldCheckUnsafe if ( callee.type !== 'MemberExpression' || callee.object.type !== 'ThisExpression' + || !('name' in callee.property) || callee.property.name !== 'setState' ) { return; diff --git a/package.json b/package.json index 2e541d99e1..5f02fb9c7a 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,10 @@ "description": "React specific linting rules for ESLint", "main": "index.js", "scripts": { - "prepack": "npmignore --auto --commentLines=autogenerated", + "clean-built-types": "rm -f $(find . -maxdepth 1 -type f -name '*.d.ts*') $(find lib -type f -name '*.d.ts*' ! -name 'types.d.ts')", + "prebuild-types": "npm run clean-built-types", + "build-types": "tsc -p build.tsconfig.json", + "prepack": "npm run build-types && npmignore --auto --commentLines=autogenerated", "prelint": "npm run lint:docs", "lint:docs": "markdownlint \"**/*.md\"", "postlint:docs": "npm run update:eslint-docs -- --check", @@ -96,6 +99,7 @@ "!lib", "docs/", "test/", + "test-published-types/", "tests/", "*.md", "*.config.js", @@ -103,6 +107,7 @@ ".eslintrc", ".editorconfig", "tsconfig.json", + "build.tsconfig.json", ".markdownlint*", "types" ] diff --git a/test-published-types/.npmrc b/test-published-types/.npmrc new file mode 100644 index 0000000000..43c97e719a --- /dev/null +++ b/test-published-types/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/test-published-types/index.js b/test-published-types/index.js new file mode 100644 index 0000000000..31e6005985 --- /dev/null +++ b/test-published-types/index.js @@ -0,0 +1,12 @@ +'use strict'; + +const react = require('eslint-plugin-react'); + +/** @type {import('eslint').Linter.Config[]} */ +module.exports = [ + { + plugins: { + react, + }, + }, +]; diff --git a/test-published-types/package.json b/test-published-types/package.json new file mode 100644 index 0000000000..ab8a7160c6 --- /dev/null +++ b/test-published-types/package.json @@ -0,0 +1,9 @@ +{ + "name": "eslint-plugin-react-test-published-types", + "private": true, + "version": "0.0.0", + "dependencies": { + "eslint": "^9.11.1", + "eslint-plugin-react": "file:.." + } +} diff --git a/test-published-types/tsconfig.json b/test-published-types/tsconfig.json new file mode 100644 index 0000000000..7fc1500df3 --- /dev/null +++ b/test-published-types/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "../tsconfig.json", + + "files": [ + "index.js" + ], + + "compilerOptions": { + "lib": ["esnext"], + "types": ["node"] + } +}