Skip to content

Commit

Permalink
chore: enable eslint-plugin-perfectionist on parser package (#9845)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg authored Aug 24, 2024
1 parent 6377f18 commit 9612d81
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 47 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ export default tseslint.config(
files: [
'packages/ast-spec/{src,tests,typings}/**/*.ts',
'packages/integration-tests/{tests,tools,typing}/**/*.ts',
'packages/parser/{src,tests}/**/*.ts',
'packages/utils/src/**/*.ts',
'packages/visitor-keys/src/**/*.ts',
'packages/website*/src/**/*.ts',
Expand Down
6 changes: 3 additions & 3 deletions packages/parser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export { parse, parseForESLint, ParserOptions } from './parser';
export {
ParserServices,
ParserServicesWithTypeInformation,
ParserServicesWithoutTypeInformation,
clearCaches,
createProgram,
ParserServices,
ParserServicesWithoutTypeInformation,
ParserServicesWithTypeInformation,
withoutProjectParserOptions,
} from '@typescript-eslint/typescript-estree';

Expand Down
47 changes: 24 additions & 23 deletions packages/parser/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import type {
AnalyzeOptions,
ScopeManager,
} from '@typescript-eslint/scope-manager';
import { analyze } from '@typescript-eslint/scope-manager';
import type { Lib, TSESTree } from '@typescript-eslint/types';
import { ParserOptions } from '@typescript-eslint/types';
import type {
AST,
ParserServices,
TSESTreeOptions,
} from '@typescript-eslint/typescript-estree';
import { parseAndGenerateServices } from '@typescript-eslint/typescript-estree';
import type { VisitorKeys } from '@typescript-eslint/visitor-keys';
import type * as ts from 'typescript';

import { analyze } from '@typescript-eslint/scope-manager';
import { ParserOptions } from '@typescript-eslint/types';
import { parseAndGenerateServices } from '@typescript-eslint/typescript-estree';
import { visitorKeys } from '@typescript-eslint/visitor-keys';
import debug from 'debug';
import type * as ts from 'typescript';
import { ScriptTarget } from 'typescript';

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

interface ParseForESLintResult {
ast: ESLintProgram;
scopeManager: ScopeManager;
services: ParserServices;
visitorKeys: VisitorKeys;
scopeManager: ScopeManager;
}

function validateBoolean(
Expand Down Expand Up @@ -58,24 +59,24 @@ function getLib(compilerOptions: ts.CompilerOptions): Lib[] {
const target = compilerOptions.target ?? ScriptTarget.ES5;
// https://github.com/microsoft/TypeScript/blob/ae582a22ee1bb052e19b7c1bc4cac60509b574e0/src/compiler/utilitiesPublic.ts#L13-L36
switch (target) {
case ScriptTarget.ESNext:
return ['esnext.full'];
case ScriptTarget.ES2022:
return ['es2022.full'];
case ScriptTarget.ES2021:
return ['es2021.full'];
case ScriptTarget.ES2020:
return ['es2020.full'];
case ScriptTarget.ES2019:
return ['es2019.full'];
case ScriptTarget.ES2018:
return ['es2018.full'];
case ScriptTarget.ES2017:
return ['es2017.full'];
case ScriptTarget.ES2016:
return ['es2016.full'];
case ScriptTarget.ES2015:
return ['es6'];
case ScriptTarget.ES2016:
return ['es2016.full'];
case ScriptTarget.ES2017:
return ['es2017.full'];
case ScriptTarget.ES2018:
return ['es2018.full'];
case ScriptTarget.ES2019:
return ['es2019.full'];
case ScriptTarget.ES2020:
return ['es2020.full'];
case ScriptTarget.ES2021:
return ['es2021.full'];
case ScriptTarget.ES2022:
return ['es2022.full'];
case ScriptTarget.ESNext:
return ['esnext.full'];
default:
return ['lib'];
}
Expand Down Expand Up @@ -135,8 +136,8 @@ function parseForESLint(

const analyzeOptions: AnalyzeOptions = {
globalReturn: parserOptions.ecmaFeatures.globalReturn,
jsxPragma: parserOptions.jsxPragma,
jsxFragmentName: parserOptions.jsxFragmentName,
jsxPragma: parserOptions.jsxPragma,
lib: parserOptions.lib,
sourceType: parserOptions.sourceType,
};
Expand Down Expand Up @@ -184,7 +185,7 @@ function parseForESLint(
services.experimentalDecorators ??=
parserOptions.experimentalDecorators === true;

return { ast, services, scopeManager, visitorKeys };
return { ast, scopeManager, services, visitorKeys };
}

export { parse, parseForESLint, ParserOptions };
24 changes: 12 additions & 12 deletions packages/parser/tests/lib/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'node:path';
import type { ParserOptions } from '@typescript-eslint/types';

import * as scopeManager from '@typescript-eslint/scope-manager';
import type { ParserOptions } from '@typescript-eslint/types';
import * as typescriptESTree from '@typescript-eslint/typescript-estree';
import path from 'node:path';
import { ScriptTarget } from 'typescript';

import { parse, parseForESLint } from '../../src/parser';
Expand All @@ -26,17 +26,17 @@ describe('parser', () => {
const code = 'const valid = true;';
const spy = jest.spyOn(typescriptESTree, 'parseAndGenerateServices');
const config: ParserOptions = {
sourceType: 'module' as const,
ecmaFeatures: {
globalReturn: false,
jsx: false,
},
sourceType: 'module' as const,
// ts-estree specific
errorOnTypeScriptSyntacticAndSemanticIssues: false,
extraFileExtensions: ['.foo'],
filePath: './isolated-file.src.ts',
project: 'tsconfig.json',
errorOnTypeScriptSyntacticAndSemanticIssues: false,
tsconfigRootDir: path.resolve(__dirname, '../fixtures/services'),
extraFileExtensions: ['.foo'],
};
parseForESLint(code, config);
expect(spy).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -173,31 +173,31 @@ describe('parser', () => {
const code = 'const valid = true;';
const spy = jest.spyOn(scopeManager, 'analyze');
const config: ParserOptions = {
sourceType: 'module' as const,
ecmaFeatures: {
globalReturn: false,
jsx: false,
},
sourceType: 'module' as const,
// scope-manager specific
lib: ['dom.iterable'],
jsxPragma: 'Foo',
jsxFragmentName: 'Bar',
jsxPragma: 'Foo',
lib: ['dom.iterable'],
// ts-estree specific
errorOnTypeScriptSyntacticAndSemanticIssues: false,
extraFileExtensions: ['.foo'],
filePath: 'isolated-file.src.ts',
project: 'tsconfig.json',
errorOnTypeScriptSyntacticAndSemanticIssues: false,
tsconfigRootDir: path.join(__dirname, '../fixtures/services'),
extraFileExtensions: ['.foo'],
};

parseForESLint(code, config);

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenLastCalledWith(expect.anything(), {
globalReturn: false,
lib: ['dom.iterable'],
jsxPragma: 'Foo',
jsxFragmentName: 'Bar',
jsxPragma: 'Foo',
lib: ['dom.iterable'],
sourceType: 'module',
});
});
Expand Down
6 changes: 3 additions & 3 deletions packages/parser/tests/lib/services.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fs from 'node:fs';
import path from 'node:path';

import { createProgram } from '@typescript-eslint/typescript-estree';
import * as glob from 'glob';
import fs from 'node:fs';
import path from 'node:path';

import type { ParserOptions } from '../../src/parser';

import {
createSnapshotTestBlock,
formatSnapshotName,
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/tests/lib/tsx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ describe('TSX', () => {

expect(
parseWithError(code, {
filePath: 'test.ts',
ecmaFeatures: {
jsx: true,
},
filePath: 'test.ts',
}),
).toMatchInlineSnapshot(`
TSError {
Expand Down
7 changes: 4 additions & 3 deletions packages/parser/tests/test-utils/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import type { TSESTree } from '@typescript-eslint/typescript-estree';

import type { ParserOptions } from '../../src/parser';

import * as parser from '../../src/parser';

const defaultConfig = {
comment: true,
errorOnUnknownASTType: true,
loc: true,
range: true,
raw: true,
tokens: true,
comment: true,
errorOnUnknownASTType: true,
sourceType: 'module' as const,
tokens: true,
};

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/parser/tests/test-utils/ts-error-serializer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TSError } from '@typescript-eslint/typescript-estree';
import type { Plugin } from 'pretty-format';

import { TSError } from '@typescript-eslint/typescript-estree';

export const serializer: Plugin = {
test: (val: unknown): val is TSError => val instanceof TSError,
serialize(val: TSError, config, indentation, depth, refs, printer) {
const format = (value: unknown): string =>
printer(value, config, indentation, depth + 1, refs);
Expand All @@ -15,4 +15,5 @@ export const serializer: Plugin = {
`}`
);
},
test: (val: unknown): val is TSError => val instanceof TSError,
};

0 comments on commit 9612d81

Please sign in to comment.