From a6c0bc9db1e265484c275860fdb41fcfd8aefaf2 Mon Sep 17 00:00:00 2001 From: Francesco Trotta Date: Thu, 21 Nov 2024 17:42:45 +0100 Subject: [PATCH] fix: add type tests (#65) --- .github/workflows/ci.yml | 15 +++++++++++++++ package.json | 3 ++- src/index.js | 4 ++-- src/rules/no-duplicate-keys.js | 2 +- src/rules/no-empty-keys.js | 2 +- src/rules/no-unsafe-values.js | 2 +- tests/types/tsconfig.json | 9 +++++++++ tests/types/types.test.ts | 23 +++++++++++++++++++++++ 8 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 tests/types/tsconfig.json create mode 100644 tests/types/types.test.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 06a2216..974282e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,6 +45,21 @@ jobs: run: npm install - name: Run tests run: npm run test + test_types: + name: Test Types + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: "lts/*" + - name: Install dependencies + run: npm install + - name: Build + run: npm run build + - name: Check Types + run: npm run test:types jsr_test: name: Verify JSR Publish runs-on: ubuntu-latest diff --git a/package.json b/package.json index f2d9406..d6f030b 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,8 @@ "fmt": "prettier --write .", "fmt:check": "prettier --check .", "test": "mocha tests/**/*.js", - "test:coverage": "c8 npm test" + "test:coverage": "c8 npm test", + "test:types": "tsc -p tests/types/tsconfig.json" }, "keywords": [ "eslint", diff --git a/src/index.js b/src/index.js index bcc04ca..6181006 100644 --- a/src/index.js +++ b/src/index.js @@ -35,11 +35,11 @@ const plugin = { configs: { recommended: { plugins: {}, - rules: { + rules: /** @type {const} */ ({ "json/no-duplicate-keys": "error", "json/no-empty-keys": "error", "json/no-unsafe-values": "error", - }, + }), }, }, }; diff --git a/src/rules/no-duplicate-keys.js b/src/rules/no-duplicate-keys.js index 345425d..3499526 100644 --- a/src/rules/no-duplicate-keys.js +++ b/src/rules/no-duplicate-keys.js @@ -9,7 +9,7 @@ export default { meta: { - type: "problem", + type: /** @type {const} */ ("problem"), docs: { description: "Disallow duplicate keys in JSON objects", diff --git a/src/rules/no-empty-keys.js b/src/rules/no-empty-keys.js index c86e25a..3fedc4f 100644 --- a/src/rules/no-empty-keys.js +++ b/src/rules/no-empty-keys.js @@ -5,7 +5,7 @@ export default { meta: { - type: "problem", + type: /** @type {const} */ ("problem"), docs: { description: "Disallow empty keys in JSON objects", diff --git a/src/rules/no-unsafe-values.js b/src/rules/no-unsafe-values.js index 85a21f5..f341729 100644 --- a/src/rules/no-unsafe-values.js +++ b/src/rules/no-unsafe-values.js @@ -5,7 +5,7 @@ export default { meta: { - type: "problem", + type: /** @type {const} */ ("problem"), docs: { description: "Disallow JSON values that are unsafe for interchange", diff --git a/tests/types/tsconfig.json b/tests/types/tsconfig.json new file mode 100644 index 0000000..6bf4343 --- /dev/null +++ b/tests/types/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "noEmit": true, + "rootDir": "../..", + "strict": true + }, + "files": ["../../dist/esm/index.d.ts", "types.test.ts"] +} diff --git a/tests/types/types.test.ts b/tests/types/types.test.ts new file mode 100644 index 0000000..a9f5374 --- /dev/null +++ b/tests/types/types.test.ts @@ -0,0 +1,23 @@ +import json from "@eslint/json"; +import { ESLint } from "eslint"; + +json satisfies ESLint.Plugin; +json.meta.name satisfies string; +json.meta.version satisfies string; + +// Check that these languages are defined: +json.languages.json satisfies object; +json.languages.json5 satisfies object; +json.languages.jsonc satisfies object; + +// Check that `plugins` in the recommended config is defined: +json.configs.recommended.plugins satisfies object; + +{ + type RecommendedRuleName = keyof typeof json.configs.recommended.rules; + type RuleName = `json/${keyof typeof json.rules}`; + type AssertAllNamesIn = never; + + // Check that all recommended rule names match the names of existing rules in this plugin. + null as AssertAllNamesIn; +}