From cad5aff59e2920b22d050e7e9453c82ca691f282 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Sat, 26 Oct 2024 05:25:01 -0500 Subject: [PATCH 1/3] Add type definitions --- .github/workflows/ci.yml | 18 ++++++++++++++++++ configs.js | 16 +++++++++------- index.js | 11 +++++++---- package.json | 26 +++++++++++++++++++++++--- tsconfig.json | 21 +++++++++++++++++++++ types/configs.d.ts | 11 +++++++++++ types/index.d.ts | 7 +++++++ 7 files changed, 96 insertions(+), 14 deletions(-) create mode 100644 tsconfig.json create mode 100644 types/configs.d.ts create mode 100644 types/index.d.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0f9e4ee..7604bb8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -97,6 +97,24 @@ jobs: - name: โฌ†๏ธ Upload coverage report uses: codecov/codecov-action@v4 + are-the-types-wrong: + name: ๐Ÿค” Are the types wrong? + runs-on: ubuntu-latest + steps: + - name: โฌ‡๏ธ Checkout repo + uses: actions/checkout@v4 + + - name: โŽ” Setup Node + uses: actions/setup-node@v4 + with: + node-version: 18 + + - name: ๐Ÿ“ฅ Install dependencies + run: npm install --legacy-peer-deps + + - name: โ–ถ๏ธ Run test:types script + run: npm run test:types -- --format=table + release: name: ๐Ÿš€ Release needs: [ lint, test ] diff --git a/configs.js b/configs.js index 7e0bd30..dd6fa39 100644 --- a/configs.js +++ b/configs.js @@ -9,14 +9,16 @@ const plugin = { rules, } +const recommended = { + name: "@eslint-community/eslint-comments/recommended", + plugins: { + "@eslint-community/eslint-comments": plugin, + }, + rules: rulesRecommended, +} + module.exports = { - recommended: { - name: '@eslint-community/eslint-comments/recommended', - plugins: { - "@eslint-community/eslint-comments": plugin, - }, - rules: rulesRecommended, - }, + recommended, } module.exports.default = module.exports diff --git a/index.js b/index.js index 5ace35b..78db10c 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,11 @@ -/** DON'T EDIT THIS FILE WHICH WAS CREATED BY 'scripts/generate-index.js'. */ "use strict" +const rules = require("./lib/rules") +const utils = require("./lib/utils") +const configs = require("./lib/configs") + module.exports = { - configs: require("./lib/configs"), - rules: require("./lib/rules"), - utils: require("./lib/utils"), + configs, + rules, + utils, } diff --git a/package.json b/package.json index e0e4d78..386a253 100644 --- a/package.json +++ b/package.json @@ -6,14 +6,30 @@ "node": "^12.22.0 || ^14.17.0 || >=16.0.0" }, "main": "index.js", + "types": "./types/index.d.ts", "type": "commonjs", "files": [ "configs.js", - "lib" + "lib", + "types" ], "exports": { - "./configs": "./configs.js", - ".": "./index.js" + "./package.json": "./package.json", + "./configs": { + "types": "./types/configs.d.ts", + "default": "./configs.js" + }, + ".": { + "types": "./types/index.d.ts", + "default": "./index.js" + } + }, + "typesVersions": { + "*": { + "configs": [ + "./types/configs.d.ts" + ] + } }, "peerDependencies": { "eslint": "^6.0.0 || ^7.0.0 || ^8.0.0 || ^9.0.0" @@ -23,11 +39,13 @@ "ignore": "^5.2.4" }, "devDependencies": { + "@arethetypeswrong/cli": "^0.17.4", "@babel/core": "^7.22.9", "@babel/eslint-parser": "^7.22.9", "@eslint-community/eslint-plugin-mysticatea": "^15.5.1", "@eslint/core": "^0.13.0", "@eslint/css": "^0.6.0", + "@types/eslint": "^8", "@types/node": "^14.18.54", "@vuepress/plugin-pwa": "^1.9.9", "cross-spawn": "^7.0.3", @@ -40,6 +58,7 @@ "opener": "^1.5.2", "rimraf": "^3.0.2", "semver": "^7.5.4", + "typescript": "^5.8.3", "vite-plugin-eslint4b": "^0.2.1", "vitepress": "^1.0.0-rc.15" }, @@ -53,6 +72,7 @@ "lint": "eslint lib scripts tests", "test": "nyc npm run debug", "debug": "mocha \"tests/lib/**/*.js\" --reporter dot --timeout 8000", + "test:types": "attw --pack", "coverage": "nyc report --reporter lcov && opener coverage/lcov-report/index.html", "watch": "npm run -s test -- --watch --growl" }, diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..fff2a40 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "allowSyntheticDefaultImports": false, + "esModuleInterop": false, + "exactOptionalPropertyTypes": true, + "forceConsistentCasingInFileNames": true, + "isolatedModules": true, + "lib": ["ESNext"], + "module": "NodeNext", + "moduleResolution": "NodeNext", + "noEmit": true, + "resolveJsonModule": true, + "skipLibCheck": false, + "strict": true, + "target": "ESNext", + "useDefineForClassFields": true, + "useUnknownInCatchVariables": true, + "verbatimModuleSyntax": true + }, + "include": ["."] +} diff --git a/types/configs.d.ts b/types/configs.d.ts new file mode 100644 index 0000000..011b635 --- /dev/null +++ b/types/configs.d.ts @@ -0,0 +1,11 @@ +import type { Linter } from "eslint" + +declare namespace Configs { + import defaultExports = Configs + + export const recommended: Linter.FlatConfig + + export { defaultExports as default } +} + +export = Configs diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..765fd2c --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,7 @@ +import type { ESLint, Linter } from "eslint" + +export declare const configs: { recommended: Linter.FlatConfig } + +export declare const rules: NonNullable + +export declare const utils: { patch: (ruleId?: string) => void } From 31e12c473dfafe85cfef8964c07a1a10242a5401 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 19 Nov 2024 23:48:44 -0600 Subject: [PATCH 2/3] Add basic type tests --- .github/workflows/ci.yml | 5 ++++- package.json | 4 +++- tests/types/configs.test-d.cts | 18 ++++++++++++++++++ tests/types/configs.test-d.mts | 15 +++++++++++++++ tsconfig.json | 3 +-- 5 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 tests/types/configs.test-d.cts create mode 100644 tests/types/configs.test-d.mts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7604bb8..a85ef51 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -112,8 +112,11 @@ jobs: - name: ๐Ÿ“ฅ Install dependencies run: npm install --legacy-peer-deps + - name: โ–ถ๏ธ Run check-exports script + run: npm run check-exports -- --format=table + - name: โ–ถ๏ธ Run test:types script - run: npm run test:types -- --format=table + run: npm run test:types release: name: ๐Ÿš€ Release diff --git a/package.json b/package.json index 386a253..ee5cda9 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "cross-spawn": "^7.0.3", "esbuild": "^0.19.3", "eslint": "^8.46.0", + "expect-type": "^1.2.1", "fs-extra": "^10.1.0", "mocha": "^10.4.0", "monaco-editor": "^0.47.0", @@ -72,7 +73,8 @@ "lint": "eslint lib scripts tests", "test": "nyc npm run debug", "debug": "mocha \"tests/lib/**/*.js\" --reporter dot --timeout 8000", - "test:types": "attw --pack", + "test:types": "tsc -p tsconfig.json", + "check-exports": "attw --pack", "coverage": "nyc report --reporter lcov && opener coverage/lcov-report/index.html", "watch": "npm run -s test -- --watch --growl" }, diff --git a/tests/types/configs.test-d.cts b/tests/types/configs.test-d.cts new file mode 100644 index 0000000..ae75c78 --- /dev/null +++ b/tests/types/configs.test-d.cts @@ -0,0 +1,18 @@ +import configs = require("@eslint-community/eslint-plugin-eslint-comments/configs") +import expectTypeModule = require("expect-type") + +import type { Linter } from "eslint" + +import expectTypeOf = expectTypeModule.expectTypeOf + +expectTypeOf(configs) + .toHaveProperty("recommended") + .toExtend() + +expectTypeOf([configs.recommended]).toExtend() + +expectTypeOf(configs.recommended).toExtend() + +expectTypeOf(configs) + .toHaveProperty("recommended") + .toExtend() diff --git a/tests/types/configs.test-d.mts b/tests/types/configs.test-d.mts new file mode 100644 index 0000000..324dea2 --- /dev/null +++ b/tests/types/configs.test-d.mts @@ -0,0 +1,15 @@ +import configs from "@eslint-community/eslint-plugin-eslint-comments/configs" +import type { Linter } from "eslint" +import { expectTypeOf } from "expect-type" + +expectTypeOf(configs) + .toHaveProperty("recommended") + .toExtend() + +expectTypeOf([configs.recommended]).toExtend() + +expectTypeOf(configs.recommended).toExtend() + +expectTypeOf(configs) + .toHaveProperty("recommended") + .toExtend() diff --git a/tsconfig.json b/tsconfig.json index fff2a40..99d54ef 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,6 @@ "compilerOptions": { "allowSyntheticDefaultImports": false, "esModuleInterop": false, - "exactOptionalPropertyTypes": true, "forceConsistentCasingInFileNames": true, "isolatedModules": true, "lib": ["ESNext"], @@ -10,7 +9,7 @@ "moduleResolution": "NodeNext", "noEmit": true, "resolveJsonModule": true, - "skipLibCheck": false, + "skipLibCheck": true, "strict": true, "target": "ESNext", "useDefineForClassFields": true, From b8e9a7a4c1a0420efdcb9e34e4eba48d1e672c59 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Wed, 20 Nov 2024 04:47:49 -0600 Subject: [PATCH 3/3] Run type tests against different versions of ESLint and TS during CI --- .github/workflows/ci.yml | 51 ++++++++++++++++++++++++++++++++++++---- package.json | 6 ++--- types/index.d.ts | 2 +- 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a85ef51..8cf7d28 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -43,8 +43,7 @@ jobs: run: npm run lint test: - name: - ๐Ÿงช Test (Node@${{ matrix.node }} - ESLint@${{ matrix.eslint }} - ${{ + name: ๐Ÿงช Test (Node@${{ matrix.node }} - ESLint@${{ matrix.eslint }} - ${{ matrix.os }}) strategy: fail-fast: false @@ -118,12 +117,54 @@ jobs: - name: โ–ถ๏ธ Run test:types script run: npm run test:types + type-tests: + name: ๐Ÿงช Type tests with ESLint ${{ matrix.eslint }} and TypeScript ${{ matrix.ts }} + runs-on: ubuntu-latest + + strategy: + fail-fast: false + + matrix: + eslint: [8, 9] + ts: ["5.0", "5.1", "5.2", "5.3", "5.4", "5.5", "5.6", "5.7", "5.8"] + + steps: + - name: โฌ‡๏ธ Checkout repo + uses: actions/checkout@v4 + + - name: โŽ” Setup Node + uses: actions/setup-node@v4 + with: + node-version: 18 + + - name: ๐Ÿ“ฅ Install dependencies + run: npm install + + - name: Pack the package + id: pack + run: npm install "$(npm pack | tail -n1)" + + - name: ๐Ÿ“ฅ Uninstall @types/eslint + if: matrix.eslint != 8 + run: npm uninstall @types/eslint + + - name: ๐Ÿ“ฅ Install ESLint version ${{ matrix.eslint }} + run: npm install --save-dev eslint@${{ matrix.eslint }} + + - name: ๐Ÿ“ฅ Install TypeScript version ${{ matrix.ts }} + run: npm install --save-dev typescript@${{ matrix.ts }} -f + + - name: โ–ถ๏ธ Run test:types script + run: npm run test:types + + - name: ๐Ÿ“ List version of ESLint + run: npm why eslint @types/eslint + release: name: ๐Ÿš€ Release - needs: [ lint, test ] + needs: [lint, test] runs-on: ubuntu-latest - if: - github.repository == 'eslint-community/eslint-plugin-eslint-comments' && + if: github.repository == 'eslint-community/eslint-plugin-eslint-comments' && contains('refs/heads/main,refs/heads/next,refs/heads/beta,refs/heads/alpha', github.ref) && github.event_name == 'push' steps: diff --git a/package.json b/package.json index ee5cda9..6eb641e 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "ignore": "^5.2.4" }, "devDependencies": { - "@arethetypeswrong/cli": "^0.17.4", + "@arethetypeswrong/cli": "^0.18.2", "@babel/core": "^7.22.9", "@babel/eslint-parser": "^7.22.9", "@eslint-community/eslint-plugin-mysticatea": "^15.5.1", @@ -51,7 +51,7 @@ "cross-spawn": "^7.0.3", "esbuild": "^0.19.3", "eslint": "^8.46.0", - "expect-type": "^1.2.1", + "expect-type": "^1.2.2", "fs-extra": "^10.1.0", "mocha": "^10.4.0", "monaco-editor": "^0.47.0", @@ -59,7 +59,7 @@ "opener": "^1.5.2", "rimraf": "^3.0.2", "semver": "^7.5.4", - "typescript": "^5.8.3", + "typescript": "^5.9.3", "vite-plugin-eslint4b": "^0.2.1", "vitepress": "^1.0.0-rc.15" }, diff --git a/types/index.d.ts b/types/index.d.ts index 765fd2c..a2d2d3a 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,6 +1,6 @@ import type { ESLint, Linter } from "eslint" -export declare const configs: { recommended: Linter.FlatConfig } +export declare const configs: { recommended: Linter.LegacyConfig } export declare const rules: NonNullable