Skip to content

Commit 9612d81

Browse files
chore: enable eslint-plugin-perfectionist on parser package (#9845)
1 parent 6377f18 commit 9612d81

File tree

8 files changed

+51
-47
lines changed

8 files changed

+51
-47
lines changed

eslint.config.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ export default tseslint.config(
573573
files: [
574574
'packages/ast-spec/{src,tests,typings}/**/*.ts',
575575
'packages/integration-tests/{tests,tools,typing}/**/*.ts',
576+
'packages/parser/{src,tests}/**/*.ts',
576577
'packages/utils/src/**/*.ts',
577578
'packages/visitor-keys/src/**/*.ts',
578579
'packages/website*/src/**/*.ts',

packages/parser/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
export { parse, parseForESLint, ParserOptions } from './parser';
22
export {
3-
ParserServices,
4-
ParserServicesWithTypeInformation,
5-
ParserServicesWithoutTypeInformation,
63
clearCaches,
74
createProgram,
5+
ParserServices,
6+
ParserServicesWithoutTypeInformation,
7+
ParserServicesWithTypeInformation,
88
withoutProjectParserOptions,
99
} from '@typescript-eslint/typescript-estree';
1010

packages/parser/src/parser.ts

+24-23
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ import type {
22
AnalyzeOptions,
33
ScopeManager,
44
} from '@typescript-eslint/scope-manager';
5-
import { analyze } from '@typescript-eslint/scope-manager';
65
import type { Lib, TSESTree } from '@typescript-eslint/types';
7-
import { ParserOptions } from '@typescript-eslint/types';
86
import type {
97
AST,
108
ParserServices,
119
TSESTreeOptions,
1210
} from '@typescript-eslint/typescript-estree';
13-
import { parseAndGenerateServices } from '@typescript-eslint/typescript-estree';
1411
import type { VisitorKeys } from '@typescript-eslint/visitor-keys';
12+
import type * as ts from 'typescript';
13+
14+
import { analyze } from '@typescript-eslint/scope-manager';
15+
import { ParserOptions } from '@typescript-eslint/types';
16+
import { parseAndGenerateServices } from '@typescript-eslint/typescript-estree';
1517
import { visitorKeys } from '@typescript-eslint/visitor-keys';
1618
import debug from 'debug';
17-
import type * as ts from 'typescript';
1819
import { ScriptTarget } from 'typescript';
1920

2021
const log = debug('typescript-eslint:parser:parser');
@@ -27,9 +28,9 @@ interface ESLintProgram extends AST<{ comment: true; tokens: true }> {
2728

2829
interface ParseForESLintResult {
2930
ast: ESLintProgram;
31+
scopeManager: ScopeManager;
3032
services: ParserServices;
3133
visitorKeys: VisitorKeys;
32-
scopeManager: ScopeManager;
3334
}
3435

3536
function validateBoolean(
@@ -58,24 +59,24 @@ function getLib(compilerOptions: ts.CompilerOptions): Lib[] {
5859
const target = compilerOptions.target ?? ScriptTarget.ES5;
5960
// https://github.com/microsoft/TypeScript/blob/ae582a22ee1bb052e19b7c1bc4cac60509b574e0/src/compiler/utilitiesPublic.ts#L13-L36
6061
switch (target) {
61-
case ScriptTarget.ESNext:
62-
return ['esnext.full'];
63-
case ScriptTarget.ES2022:
64-
return ['es2022.full'];
65-
case ScriptTarget.ES2021:
66-
return ['es2021.full'];
67-
case ScriptTarget.ES2020:
68-
return ['es2020.full'];
69-
case ScriptTarget.ES2019:
70-
return ['es2019.full'];
71-
case ScriptTarget.ES2018:
72-
return ['es2018.full'];
73-
case ScriptTarget.ES2017:
74-
return ['es2017.full'];
75-
case ScriptTarget.ES2016:
76-
return ['es2016.full'];
7762
case ScriptTarget.ES2015:
7863
return ['es6'];
64+
case ScriptTarget.ES2016:
65+
return ['es2016.full'];
66+
case ScriptTarget.ES2017:
67+
return ['es2017.full'];
68+
case ScriptTarget.ES2018:
69+
return ['es2018.full'];
70+
case ScriptTarget.ES2019:
71+
return ['es2019.full'];
72+
case ScriptTarget.ES2020:
73+
return ['es2020.full'];
74+
case ScriptTarget.ES2021:
75+
return ['es2021.full'];
76+
case ScriptTarget.ES2022:
77+
return ['es2022.full'];
78+
case ScriptTarget.ESNext:
79+
return ['esnext.full'];
7980
default:
8081
return ['lib'];
8182
}
@@ -135,8 +136,8 @@ function parseForESLint(
135136

136137
const analyzeOptions: AnalyzeOptions = {
137138
globalReturn: parserOptions.ecmaFeatures.globalReturn,
138-
jsxPragma: parserOptions.jsxPragma,
139139
jsxFragmentName: parserOptions.jsxFragmentName,
140+
jsxPragma: parserOptions.jsxPragma,
140141
lib: parserOptions.lib,
141142
sourceType: parserOptions.sourceType,
142143
};
@@ -184,7 +185,7 @@ function parseForESLint(
184185
services.experimentalDecorators ??=
185186
parserOptions.experimentalDecorators === true;
186187

187-
return { ast, services, scopeManager, visitorKeys };
188+
return { ast, scopeManager, services, visitorKeys };
188189
}
189190

190191
export { parse, parseForESLint, ParserOptions };

packages/parser/tests/lib/parser.test.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import path from 'node:path';
1+
import type { ParserOptions } from '@typescript-eslint/types';
22

33
import * as scopeManager from '@typescript-eslint/scope-manager';
4-
import type { ParserOptions } from '@typescript-eslint/types';
54
import * as typescriptESTree from '@typescript-eslint/typescript-estree';
5+
import path from 'node:path';
66
import { ScriptTarget } from 'typescript';
77

88
import { parse, parseForESLint } from '../../src/parser';
@@ -26,17 +26,17 @@ describe('parser', () => {
2626
const code = 'const valid = true;';
2727
const spy = jest.spyOn(typescriptESTree, 'parseAndGenerateServices');
2828
const config: ParserOptions = {
29-
sourceType: 'module' as const,
3029
ecmaFeatures: {
3130
globalReturn: false,
3231
jsx: false,
3332
},
33+
sourceType: 'module' as const,
3434
// ts-estree specific
35+
errorOnTypeScriptSyntacticAndSemanticIssues: false,
36+
extraFileExtensions: ['.foo'],
3537
filePath: './isolated-file.src.ts',
3638
project: 'tsconfig.json',
37-
errorOnTypeScriptSyntacticAndSemanticIssues: false,
3839
tsconfigRootDir: path.resolve(__dirname, '../fixtures/services'),
39-
extraFileExtensions: ['.foo'],
4040
};
4141
parseForESLint(code, config);
4242
expect(spy).toHaveBeenCalledTimes(1);
@@ -173,31 +173,31 @@ describe('parser', () => {
173173
const code = 'const valid = true;';
174174
const spy = jest.spyOn(scopeManager, 'analyze');
175175
const config: ParserOptions = {
176-
sourceType: 'module' as const,
177176
ecmaFeatures: {
178177
globalReturn: false,
179178
jsx: false,
180179
},
180+
sourceType: 'module' as const,
181181
// scope-manager specific
182-
lib: ['dom.iterable'],
183-
jsxPragma: 'Foo',
184182
jsxFragmentName: 'Bar',
183+
jsxPragma: 'Foo',
184+
lib: ['dom.iterable'],
185185
// ts-estree specific
186+
errorOnTypeScriptSyntacticAndSemanticIssues: false,
187+
extraFileExtensions: ['.foo'],
186188
filePath: 'isolated-file.src.ts',
187189
project: 'tsconfig.json',
188-
errorOnTypeScriptSyntacticAndSemanticIssues: false,
189190
tsconfigRootDir: path.join(__dirname, '../fixtures/services'),
190-
extraFileExtensions: ['.foo'],
191191
};
192192

193193
parseForESLint(code, config);
194194

195195
expect(spy).toHaveBeenCalledTimes(1);
196196
expect(spy).toHaveBeenLastCalledWith(expect.anything(), {
197197
globalReturn: false,
198-
lib: ['dom.iterable'],
199-
jsxPragma: 'Foo',
200198
jsxFragmentName: 'Bar',
199+
jsxPragma: 'Foo',
200+
lib: ['dom.iterable'],
201201
sourceType: 'module',
202202
});
203203
});

packages/parser/tests/lib/services.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import fs from 'node:fs';
2-
import path from 'node:path';
3-
41
import { createProgram } from '@typescript-eslint/typescript-estree';
52
import * as glob from 'glob';
3+
import fs from 'node:fs';
4+
import path from 'node:path';
65

76
import type { ParserOptions } from '../../src/parser';
7+
88
import {
99
createSnapshotTestBlock,
1010
formatSnapshotName,

packages/parser/tests/lib/tsx.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ describe('TSX', () => {
6464

6565
expect(
6666
parseWithError(code, {
67-
filePath: 'test.ts',
6867
ecmaFeatures: {
6968
jsx: true,
7069
},
70+
filePath: 'test.ts',
7171
}),
7272
).toMatchInlineSnapshot(`
7373
TSError {

packages/parser/tests/test-utils/test-utils.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import type { TSESTree } from '@typescript-eslint/typescript-estree';
22

33
import type { ParserOptions } from '../../src/parser';
4+
45
import * as parser from '../../src/parser';
56

67
const defaultConfig = {
8+
comment: true,
9+
errorOnUnknownASTType: true,
710
loc: true,
811
range: true,
912
raw: true,
10-
tokens: true,
11-
comment: true,
12-
errorOnUnknownASTType: true,
1313
sourceType: 'module' as const,
14+
tokens: true,
1415
};
1516

1617
/**

packages/parser/tests/test-utils/ts-error-serializer.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { TSError } from '@typescript-eslint/typescript-estree';
21
import type { Plugin } from 'pretty-format';
32

3+
import { TSError } from '@typescript-eslint/typescript-estree';
4+
45
export const serializer: Plugin = {
5-
test: (val: unknown): val is TSError => val instanceof TSError,
66
serialize(val: TSError, config, indentation, depth, refs, printer) {
77
const format = (value: unknown): string =>
88
printer(value, config, indentation, depth + 1, refs);
@@ -15,4 +15,5 @@ export const serializer: Plugin = {
1515
`}`
1616
);
1717
},
18+
test: (val: unknown): val is TSError => val instanceof TSError,
1819
};

0 commit comments

Comments
 (0)