Skip to content

Commit d676683

Browse files
authored
feat: add package.json exports for public packages (#6458)
1 parent fc2df63 commit d676683

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+517
-298
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ jobs:
129129
'eslint-plugin-internal',
130130
'eslint-plugin-tslint',
131131
'parser',
132+
'repo-tools',
132133
'scope-manager',
133134
'type-utils',
134135
'typescript-estree',

package.json

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"armano2",
1717
"Jed Fox"
1818
],
19+
"type": "commonjs",
1920
"license": "BSD-2-Clause",
2021
"repository": "typescript-eslint/typescript-eslint",
2122
"bugs": {
@@ -121,13 +122,16 @@
121122
"jest-resolve": "^29",
122123
"jest-util": "^29",
123124
"pretty-format": "^29",
124-
"@swc/core-android-arm-eabi": "npm:dummypkg-a@1.0.0",
125-
"@swc/core-android-arm64": "npm:dummypkg-a@1.0.0",
126-
"@swc/core-freebsd-x64": "npm:dummypkg-a@1.0.0",
127-
"@swc/core-linux-arm-gnueabihf": "npm:dummypkg-a@1.0.0",
128-
"@swc/core-linux-arm64-gnu": "npm:dummypkg-a@1.0.0",
129-
"@swc/core-linux-arm64-musl": "npm:dummypkg-a@1.0.0",
130-
"@swc/core-win32-arm64-msvc": "npm:dummypkg-a@1.0.0",
131-
"@swc/core-win32-ia32-msvc": "npm:dummypkg-a@1.0.0"
125+
"@swc/core-android-arm-eabi": "file:./tools/dummypkg",
126+
"@swc/core-android-arm64": "file:./tools/dummypkg",
127+
"@swc/core-freebsd-x64": "file:./tools/dummypkg",
128+
"@swc/core-linux-arm-gnueabihf": "file:./tools/dummypkg",
129+
"@swc/core-linux-arm64-gnu": "file:./tools/dummypkg",
130+
"@swc/core-linux-arm64-musl": "file:./tools/dummypkg",
131+
"@swc/core-win32-arm64-msvc": "file:./tools/dummypkg",
132+
"@swc/core-win32-ia32-msvc": "file:./tools/dummypkg",
133+
"@types/eslint": "file:./tools/dummypkg",
134+
"@types/eslint-scope": "file:./tools/dummypkg",
135+
"@types/estree": "file:./tools/dummypkg"
132136
}
133137
}

packages/ast-spec/tests/util/parsers/typescript-estree-import.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,4 @@
1313
* This should be the only place in the package that we import from typescript-estree.
1414
*/
1515

16-
// We need to ignore this lint error regarding it being missing from the package.json, see above.
17-
// eslint-disable-next-line import/no-extraneous-dependencies
1816
export { parse } from '@typescript-eslint/typescript-estree';

packages/eslint-plugin-internal/src/rules/plugin-test-formatting.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ export default createRule<Options, MessageIds>({
528528
}
529529

530530
const typeString = checker.typeToString(type);
531-
if (/^RunTests\b/.test(typeString)) {
531+
if (/^(TSESLint\.)?RunTests\b/.test(typeString)) {
532532
checkedObjects.add(node);
533533

534534
for (const prop of node.properties) {
@@ -558,12 +558,12 @@ export default createRule<Options, MessageIds>({
558558
return;
559559
}
560560

561-
if (/^ValidTestCase\b/.test(typeString)) {
561+
if (/^(TSESLint\.)?ValidTestCase\b/.test(typeString)) {
562562
checkInvalidTest(node);
563563
return;
564564
}
565565

566-
if (/^InvalidTestCase\b/.test(typeString)) {
566+
if (/^(TSESLint\.)?InvalidTestCase\b/.test(typeString)) {
567567
checkInvalidTest(node);
568568
for (const testProp of node.properties) {
569569
if (

packages/eslint-plugin-tslint/package.json

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
{
22
"name": "@typescript-eslint/eslint-plugin-tslint",
33
"version": "5.53.0",
4-
"main": "dist/index.js",
5-
"typings": "src/index.ts",
64
"description": "ESLint plugin that wraps a TSLint configuration and lints the whole source using TSLint",
7-
"keywords": [
8-
"eslint",
9-
"eslintplugin",
10-
"eslint-plugin",
11-
"tslint"
12-
],
13-
"engines": {
14-
"node": "^14.18.0 || ^16.0.0 || >=18.0.0"
15-
},
165
"files": [
176
"dist",
187
"package.json",
198
"README.md",
209
"LICENSE"
2110
],
11+
"type": "commonjs",
12+
"exports": {
13+
".": {
14+
"types": "./dist/index.d.ts",
15+
"require": "./dist/index.js"
16+
},
17+
"./package.json": {
18+
"require": "./package.json"
19+
}
20+
},
21+
"engines": {
22+
"node": "^14.18.0 || ^16.0.0 || >=18.0.0"
23+
},
2224
"repository": {
2325
"type": "git",
2426
"url": "https://github.com/typescript-eslint/typescript-eslint.git",
@@ -28,6 +30,12 @@
2830
"url": "https://github.com/typescript-eslint/typescript-eslint/issues"
2931
},
3032
"license": "MIT",
33+
"keywords": [
34+
"eslint",
35+
"eslintplugin",
36+
"eslint-plugin",
37+
"tslint"
38+
],
3139
"scripts": {
3240
"build": "tsc -b tsconfig.build.json",
3341
"clean": "tsc -b tsconfig.build.json --clean",
@@ -49,5 +57,9 @@
4957
"devDependencies": {
5058
"@types/lodash": "*",
5159
"@typescript-eslint/parser": "5.53.0"
60+
},
61+
"funding": {
62+
"type": "opencollective",
63+
"url": "https://opencollective.com/typescript-eslint"
5264
}
5365
}

packages/eslint-plugin-tslint/tests/tsconfig.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/eslint-plugin-tslint/tsconfig.build.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"extends": "../../tsconfig.base.json",
33
"compilerOptions": {
4+
// specifically disable declarations for the plugin
5+
// see reasoning in packages/eslint-plugin/rules.d.ts
6+
"declaration": false,
7+
"declarationMap": false,
48
"outDir": "./dist",
59
"rootDir": "./src",
610
"resolveJsonModule": true

packages/eslint-plugin/index.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import type { TSESLint } from '@typescript-eslint/utils';
22

3-
export const rules: Record<string, TSESLint.RuleModule<string, unknown[]>>;
4-
export const configs: Record<string, TSESLint.Linter.Config>;
3+
import type rules from './rules';
4+
5+
declare const cjsExport: {
6+
configs: Record<string, TSESLint.Linter.Config>;
7+
rules: typeof rules;
8+
};
9+
export = cjsExport;

packages/eslint-plugin/package.json

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22
"name": "@typescript-eslint/eslint-plugin",
33
"version": "5.53.0",
44
"description": "TypeScript plugin for ESLint",
5-
"keywords": [
6-
"eslint",
7-
"eslintplugin",
8-
"eslint-plugin",
9-
"typescript"
10-
],
11-
"engines": {
12-
"node": "^14.18.0 || ^16.0.0 || >=18.0.0"
13-
},
145
"files": [
156
"dist",
167
"docs",
@@ -19,6 +10,23 @@
1910
"README.md",
2011
"LICENSE"
2112
],
13+
"type": "commonjs",
14+
"exports": {
15+
".": {
16+
"types": "./index.d.ts",
17+
"require": "./dist/index.js"
18+
},
19+
"./package.json": {
20+
"require": "./package.json"
21+
},
22+
"./use-at-your-own-risk/rules": {
23+
"types": "./rules.d.ts",
24+
"require": "./dist/rules/index.js"
25+
}
26+
},
27+
"engines": {
28+
"node": "^14.18.0 || ^16.0.0 || >=18.0.0"
29+
},
2230
"repository": {
2331
"type": "git",
2432
"url": "https://github.com/typescript-eslint/typescript-eslint.git",
@@ -28,16 +36,20 @@
2836
"url": "https://github.com/typescript-eslint/typescript-eslint/issues"
2937
},
3038
"license": "MIT",
31-
"main": "dist/index.js",
32-
"types": "index.d.ts",
39+
"keywords": [
40+
"eslint",
41+
"eslintplugin",
42+
"eslint-plugin",
43+
"typescript"
44+
],
3345
"scripts": {
3446
"build": "tsc -b tsconfig.build.json",
3547
"check-docs": "jest tests/docs.test.ts --runTestsByPath --silent --runInBand",
3648
"check-configs": "jest tests/configs.test.ts --runTestsByPath --silent --runInBand",
3749
"clean": "tsc -b tsconfig.build.json --clean",
3850
"postclean": "rimraf dist && rimraf coverage",
3951
"format": "prettier --write \"./**/*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}\" --ignore-path ../../.prettierignore",
40-
"generate:breaking-changes": "yarn tsx tools/generate-breaking-changes.ts",
52+
"generate:breaking-changes": "yarn tsx tools/generate-breaking-changes.mts",
4153
"generate:configs": "yarn tsx tools/generate-configs.ts",
4254
"lint": "nx lint",
4355
"test": "jest --coverage",
@@ -61,9 +73,7 @@
6173
"@types/marked": "*",
6274
"@types/natural-compare-lite": "^1.4.0",
6375
"@types/prettier": "*",
64-
"chalk": "^5.0.1",
6576
"cross-fetch": "*",
66-
"grapheme-splitter": "^1.0.4",
6777
"json-schema": "*",
6878
"markdown-table": "^3.0.2",
6979
"marked": "^4.0.15",

packages/eslint-plugin/rules.d.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
We purposely don't generate types for our plugin because TL;DR:
3+
1) there's no real reason that anyone should do a typed import of our rules,
4+
2) it would require us to change our code so there aren't as many inferred types
5+
6+
This type declaration exists as a hacky way to add a type to the export for our
7+
internal packages that require it.
8+
9+
*** Long reason ***
10+
11+
When you turn on declaration files, TS requires all types to be "fully resolvable"
12+
without changes to the code.
13+
All of our lint rules `export default createRule(...)`, which means they all
14+
implicitly reference the `TSESLint.Rule` type for the export.
15+
16+
TS wants to transpile each rule file to this `.d.ts` file:
17+
18+
```ts
19+
import type { TSESLint } from '@typescript-eslint/utils';
20+
declare const _default: TSESLint.RuleModule<TMessageIds, TOptions, TSESLint.RuleListener>;
21+
export default _default;
22+
```
23+
24+
Because we don't import `TSESLint` in most files, it means that TS would have to
25+
insert a new import during the declaration emit to make this work.
26+
However TS wants to avoid adding new imports to the file because a new module
27+
could have type side-effects (like global augmentation) which could cause weird
28+
type side-effects in the decl file that wouldn't exist in source TS file.
29+
30+
So TS errors on most of our rules with the following error:
31+
```
32+
The inferred type of 'default' cannot be named without a reference to
33+
'../../../../node_modules/@typescript-eslint/utils/src/ts-eslint/Rule'.
34+
This is likely not portable. A type annotation is necessary. ts(2742)
35+
```
36+
*/
37+
38+
import type { RuleModule } from '@typescript-eslint/utils/ts-eslint';
39+
40+
export interface TypeScriptESLintRules {
41+
[ruleName: string]: RuleModule<string, unknown[]>;
42+
}
43+
declare const rules: TypeScriptESLintRules;
44+
export = rules;

0 commit comments

Comments
 (0)