Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add type tests #65

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}),
Comment on lines -38 to +42
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is const cast syntax. I'm using this to ensure that properties in the rules object have type "error" rather than string, because string is too wide to be assigned to a severity:

    type StringSeverity = "off" | "warn" | "error";

https://github.com/eslint/eslint/blob/v9.15.0/lib/types/index.d.ts#L917

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat trick!

},
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-duplicate-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

export default {
meta: {
type: "problem",
type: /** @type {const} */ ("problem"),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ensures that the type property has type "problem" rather than string so it matches the definition in the interface:

        type?: "problem" | "suggestion" | "layout" | undefined;

https://github.com/eslint/eslint/blob/v9.15.0/lib/types/index.d.ts#L760


docs: {
description: "Disallow duplicate keys in JSON objects",
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-empty-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

export default {
meta: {
type: "problem",
type: /** @type {const} */ ("problem"),

docs: {
description: "Disallow empty keys in JSON objects",
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-unsafe-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

export default {
meta: {
type: "problem",
type: /** @type {const} */ ("problem"),

docs: {
description: "Disallow JSON values that are unsafe for interchange",
Expand Down
9 changes: 9 additions & 0 deletions tests/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"noEmit": true,
"rootDir": "../..",
"strict": true
},
"files": ["../../dist/esm/index.d.ts", "types.test.ts"]
}
23 changes: 23 additions & 0 deletions tests/types/types.test.ts
Original file line number Diff line number Diff line change
@@ -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<T1 extends T2, T2> = never;

// Check that all recommended rule names match the names of existing rules in this plugin.
null as AssertAllNamesIn<RecommendedRuleName, RuleName>;
}