Skip to content

Commit cb546be

Browse files
nzakasfasttime
andauthored
feat: Add types to package (#179)
* chore: Upgrade ESLint * Add back eslintignore * Skip tests relying on project files * feat: Add type definitions fixes #177 * Replace deprecated type * Add missing JSDoc comments * Fix linting error * Remove ExpectType and update types test * Remove lib from tsconfig.json * Update package.json Co-authored-by: Francesco Trotta <[email protected]> --------- Co-authored-by: Francesco Trotta <[email protected]>
1 parent 18d22b7 commit cb546be

File tree

7 files changed

+176
-5
lines changed

7 files changed

+176
-5
lines changed

.github/workflows/ci.yml

+13
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,16 @@ jobs:
4040
run: npm install
4141
- name: Test
4242
run: npm test
43+
44+
test_types:
45+
name: Test Types
46+
runs-on: ubuntu-latest
47+
steps:
48+
- uses: actions/checkout@v4
49+
- uses: actions/setup-node@v4
50+
with:
51+
node-version: lts/*
52+
- name: Install Packages
53+
run: npm install
54+
- name: Test
55+
run: npm run test:types

lib/config-array/ignore-pattern.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const DotPatterns = Object.freeze([".*", "!.eslintrc.*", "!../"]);
118118
//------------------------------------------------------------------------------
119119

120120
/**
121-
*
121+
* Represents a set of glob patterns to ignore against a base path.
122122
*/
123123
class IgnorePattern {
124124

lib/shared/config-validator.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const noOptionsSchema = Object.freeze({
5252
//-----------------------------------------------------------------------------
5353

5454
/**
55-
*
55+
* Validator for configuration objects.
5656
*/
5757
export default class ConfigValidator {
5858
constructor({ builtInRules = new Map() } = {}) {

lib/types/index.d.ts

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* @fileoverview This file contains the core types for ESLint. It was initially extracted
3+
* from the `@types/eslint__eslintrc` package.
4+
*/
5+
6+
/*
7+
* MIT License
8+
* Copyright (c) Microsoft Corporation.
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy
11+
* of this software and associated documentation files (the "Software"), to deal
12+
* in the Software without restriction, including without limitation the rights
13+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
* copies of the Software, and to permit persons to whom the Software is
15+
* furnished to do so, subject to the following conditions:
16+
* The above copyright notice and this permission notice shall be included in all
17+
* copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
* SOFTWARE
26+
*/
27+
28+
import type { Linter } from "eslint";
29+
30+
/**
31+
* A compatibility class for working with configs.
32+
*/
33+
export class FlatCompat {
34+
constructor({
35+
baseDirectory,
36+
resolvePluginsRelativeTo,
37+
recommendedConfig,
38+
allConfig,
39+
}?: {
40+
/**
41+
* default: process.cwd()
42+
*/
43+
baseDirectory?: string;
44+
resolvePluginsRelativeTo?: string;
45+
recommendedConfig?: Linter.LegacyConfig;
46+
allConfig?: Linter.LegacyConfig;
47+
});
48+
49+
/**
50+
* Translates an ESLintRC-style config into a flag-config-style config.
51+
* @param eslintrcConfig The ESLintRC-style config object.
52+
* @returns A flag-config-style config object.
53+
*/
54+
config(eslintrcConfig: Linter.LegacyConfig): Linter.Config[];
55+
56+
/**
57+
* Translates the `env` section of an ESLintRC-style config.
58+
* @param envConfig The `env` section of an ESLintRC config.
59+
* @returns An array of flag-config objects representing the environments.
60+
*/
61+
env(envConfig: { [name: string]: boolean }): Linter.Config[];
62+
63+
/**
64+
* Translates the `extends` section of an ESLintRC-style config.
65+
* @param configsToExtend The names of the configs to load.
66+
* @returns An array of flag-config objects representing the config.
67+
*/
68+
extends(...configsToExtend: string[]): Linter.Config[];
69+
70+
/**
71+
* Translates the `plugins` section of an ESLintRC-style config.
72+
* @param plugins The names of the plugins to load.
73+
* @returns An array of flag-config objects representing the plugins.
74+
*/
75+
plugins(...plugins: string[]): Linter.Config[];
76+
}

package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
"description": "The legacy ESLintRC config file format for ESLint",
55
"type": "module",
66
"main": "./dist/eslintrc.cjs",
7+
"types": "./dist/eslintrc.d.ts",
78
"exports": {
89
".": {
910
"import": "./lib/index.js",
10-
"require": "./dist/eslintrc.cjs"
11+
"require": "./dist/eslintrc.cjs",
12+
"types": "./lib/types/index.d.ts"
1113
},
1214
"./package.json": "./package.json",
1315
"./universal": {
@@ -26,7 +28,7 @@
2628
"access": "public"
2729
},
2830
"scripts": {
29-
"build": "rollup -c",
31+
"build": "rollup -c && node -e \"fs.copyFileSync('./lib/types/index.d.ts', './dist/eslintrc.d.cts')\"",
3032
"lint": "eslint . --report-unused-disable-directives",
3133
"lint:fix": "npm run lint -- --fix",
3234
"prepare": "npm run build",
@@ -35,7 +37,8 @@
3537
"release:generate:beta": "eslint-generate-prerelease beta",
3638
"release:generate:rc": "eslint-generate-prerelease rc",
3739
"release:publish": "eslint-publish-release",
38-
"test": "mocha -R progress -c 'tests/lib/*.cjs' && c8 mocha -R progress -c 'tests/lib/**/*.js'"
40+
"test": "mocha -R progress -c 'tests/lib/*.cjs' && c8 mocha -R progress -c 'tests/lib/**/*.js'",
41+
"test:types": "tsc -p tests/lib/types/tsconfig.json"
3942
},
4043
"repository": "eslint/eslintrc",
4144
"funding": "https://opencollective.com/eslint",

tests/lib/types/tsconfig.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"noImplicitAny": true,
5+
"noImplicitThis": true,
6+
"strictNullChecks": true,
7+
"strictFunctionTypes": true,
8+
"types": [],
9+
"noEmit": true,
10+
"forceConsistentCasingInFileNames": true,
11+
"exactOptionalPropertyTypes": true
12+
},
13+
"files": [
14+
"../../../lib/types/index.d.ts",
15+
"types.test.mts"
16+
]
17+
}

tests/lib/types/types.test.mts

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @fileoverview This file contains tests for types. It was initially extracted
3+
* from the `@types/eslint__eslintrc` package.
4+
*/
5+
6+
/*
7+
* MIT License
8+
* Copyright (c) Microsoft Corporation.
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy
11+
* of this software and associated documentation files (the "Software"), to deal
12+
* in the Software without restriction, including without limitation the rights
13+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
* copies of the Software, and to permit persons to whom the Software is
15+
* furnished to do so, subject to the following conditions:
16+
* The above copyright notice and this permission notice shall be included in all
17+
* copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
* SOFTWARE
26+
*/
27+
28+
29+
import { FlatCompat } from "../../../lib/types/index.js";
30+
import { Linter } from "eslint";
31+
32+
const __dirname = "/path/to/project";
33+
34+
const compat = new FlatCompat({
35+
baseDirectory: __dirname,
36+
resolvePluginsRelativeTo: __dirname,
37+
});
38+
39+
const config: Linter.Config[] = [
40+
...compat.extends("standard", "example"),
41+
42+
...compat.env({
43+
es2020: true,
44+
node: true,
45+
}),
46+
47+
...compat.plugins("airbnb", "react"),
48+
49+
...compat.config({
50+
plugins: ["airbnb", "react"],
51+
extends: "standard",
52+
env: {
53+
es2020: true,
54+
node: true,
55+
},
56+
rules: {
57+
semi: "error",
58+
},
59+
}),
60+
];
61+
62+
export default config;

0 commit comments

Comments
 (0)