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

feat: add missing type definitions #246

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
64 changes: 64 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,70 @@ 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 check-exports script
run: npm run check-exports -- --format=table

- 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]

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 ]
Expand Down
16 changes: 9 additions & 7 deletions configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -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,
}
30 changes: 26 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,29 @@
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
},
"main": "index.js",
"types": "./types/index.d.ts",
"files": [
"configs.js",
"lib"
"lib",
"types"
],
"exports": {
"./configs": "./configs.js",
".": "./index.js"
"exports": {
"./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"
]
}
Comment on lines +26 to +31
Copy link
Member

Choose a reason for hiding this comment

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

We already have configs types defined, so I don't think it's necessary to declare them again in typesVersions?

Copy link
Author

Choose a reason for hiding this comment

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

Without it Node10 module resolution will fail when importing from @eslint-community/eslint-plugin-eslint-comments/configs.

},
"peerDependencies": {
"eslint": "^6.0.0 || ^7.0.0 || ^8.0.0 || ^9.0.0"
Expand All @@ -22,21 +38,25 @@
"ignore": "^5.2.4"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.17.0",
"@babel/core": "^7.22.9",
"@babel/eslint-parser": "^7.22.9",
"@eslint-community/eslint-plugin-mysticatea": "^15.5.1",
"@types/eslint": "^8",
"@types/node": "^14.18.54",
"@vuepress/plugin-pwa": "^1.9.9",
"cross-spawn": "^7.0.3",
"esbuild": "^0.19.3",
"eslint": "^8.46.0",
"expect-type": "^1.1.0",
"fs-extra": "^10.1.0",
"mocha": "^10.4.0",
"monaco-editor": "^0.47.0",
"nyc": "^15.1.0",
"opener": "^1.5.2",
"rimraf": "^3.0.2",
"semver": "^7.5.4",
"typescript": "^5.6.3",
"vite-plugin-eslint4b": "^0.2.1",
"vitepress": "^1.0.0-rc.15"
},
Expand All @@ -49,6 +69,8 @@
"docs:watch": "vitepress dev docs",
"lint": "eslint lib scripts tests",
"test": "nyc mocha \"tests/lib/**/*.js\" --reporter dot --timeout 8000",
"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"
},
Expand Down
18 changes: 18 additions & 0 deletions tests/types/configs.test-d.cts
Original file line number Diff line number Diff line change
@@ -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
Comment on lines +1 to +6
Copy link

Choose a reason for hiding this comment

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

Should most of this not be const instead of import?

Suggested change
import configs = require("@eslint-community/eslint-plugin-eslint-comments/configs")
import expectTypeModule = require("expect-type")
import type { Linter } from "eslint"
import expectTypeOf = expectTypeModule.expectTypeOf
const configs = require("@eslint-community/eslint-plugin-eslint-comments/configs")
const { expectTypeOf } = require("expect-type")
import type { Linter } from "eslint"

I have never seen this syntax before, so do correct me if I am wrong 😅

Copy link
Author

Choose a reason for hiding this comment

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

In this file I'm testing the types to make sure they work with TypeScript's CJS syntax. So something like const configs = require("@eslint-community/eslint-plugin-eslint-comments/configs") becomes import configs = require("@eslint-community/eslint-plugin-eslint-comments/configs"). It's to test ESM/CJS Interoperability on a type level.

Reference

Copy link

@scagood scagood Nov 20, 2024

Choose a reason for hiding this comment

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

Oh, neat! Thanks for the explanation :)


Edit: I can't seem to mark this as resolved 👀

Copy link
Author

@aryaemami59 aryaemami59 Nov 20, 2024

Choose a reason for hiding this comment

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

@scagood It might be good to fully migrate to TypeScript so the types and runtime code don't get out of sync. Thoughts?

Copy link

Choose a reason for hiding this comment

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

Here is the previous discussion in the eslint-plugin-n repo, I am okay with either tbh
eslint-community/eslint-plugin-n#371


That being said, that is more a question for @eslint-community/eslint-plugin-eslint-comments.

Copy link
Author

Choose a reason for hiding this comment

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

ah I see, honestly I would be happy to do it, I've done a lot of TS migrations and I could definitely do this one too. We can bundle with tsup which would be even easier. Do you want me to put together a quick POC so you can kind of see what it will look like?


expectTypeOf(configs)
.toHaveProperty("recommended")
.toMatchTypeOf<Linter.FlatConfig>()

expectTypeOf([configs.recommended]).toMatchTypeOf<Linter.FlatConfig[]>()

expectTypeOf(configs.recommended).toMatchTypeOf<Linter.FlatConfig>()

expectTypeOf(configs)
.toHaveProperty("recommended")
.toMatchTypeOf<Linter.FlatConfig>()
15 changes: 15 additions & 0 deletions tests/types/configs.test-d.mts
Original file line number Diff line number Diff line change
@@ -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")
.toMatchTypeOf<Linter.FlatConfig>()

expectTypeOf([configs.recommended]).toMatchTypeOf<Linter.FlatConfig[]>()

expectTypeOf(configs.recommended).toMatchTypeOf<Linter.FlatConfig>()

expectTypeOf(configs)
.toHaveProperty("recommended")
.toMatchTypeOf<Linter.FlatConfig>()
20 changes: 20 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"allowSyntheticDefaultImports": false,
"esModuleInterop": false,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"lib": ["ESNext"],
"module": "NodeNext",
"moduleResolution": "NodeNext",
"noEmit": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "ESNext",
"useDefineForClassFields": true,
"useUnknownInCatchVariables": true,
"verbatimModuleSyntax": true
},
"include": ["."]
}
11 changes: 11 additions & 0 deletions types/configs.d.ts
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { ESLint, Linter } from "eslint"

export declare const configs: { recommended: Linter.FlatConfig }

export declare const rules: NonNullable<ESLint.Plugin["rules"]>

export declare const utils: { patch: (ruleId?: string) => void }