Skip to content

Commit 519dd7b

Browse files
authored
chore(website): rewrite WebLinter to typescript and fix support for ts 4.7 (typescript-eslint#5034)
1 parent d214556 commit 519dd7b

File tree

22 files changed

+221
-296
lines changed

22 files changed

+221
-296
lines changed

packages/utils/src/ts-eslint/Linter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ declare class LinterBase {
7676

7777
/**
7878
* Performs multiple autofix passes over the text until as many fixes as possible have been applied.
79-
* @param text The source text to apply fixes to.
79+
* @param code The source text to apply fixes to.
8080
* @param config The ESLint config object to use.
8181
* @param options The ESLint options object to use.
8282
* @returns The result of the fix operation as returned from the SourceCodeFixer.
@@ -316,7 +316,7 @@ namespace Linter {
316316

317317
export interface ESLintParseResult {
318318
ast: TSESTree.Program;
319-
parserServices?: ParserServices;
319+
services?: ParserServices;
320320
scopeManager?: Scope.ScopeManager;
321321
visitorKeys?: SourceCode.VisitorKeys;
322322
}

packages/website-eslint/src/linter/CompilerHost.js

Lines changed: 0 additions & 92 deletions
This file was deleted.
Lines changed: 8 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,16 @@
11
import 'vs/language/typescript/tsWorker';
2-
import { parseForESLint } from './parser';
32
import { Linter } from 'eslint';
43
import rules from '@typescript-eslint/eslint-plugin/dist/rules';
54

6-
const PARSER_NAME = '@typescript-eslint/parser';
7-
8-
export function loadLinter(libs, options) {
5+
export function createLinter() {
96
const linter = new Linter();
10-
let storedAST;
11-
let storedTsAST;
12-
let storedScope;
13-
14-
let compilerOptions = options;
15-
16-
linter.defineParser(PARSER_NAME, {
17-
parseForESLint(code, eslintOptions) {
18-
const toParse = parseForESLint(
19-
code,
20-
eslintOptions,
21-
compilerOptions,
22-
libs,
23-
);
24-
storedAST = toParse.ast;
25-
storedTsAST = toParse.tsAst;
26-
storedScope = toParse.scopeManager;
27-
return toParse;
28-
},
29-
// parse(code: string, options: ParserOptions): ParseForESLintResult['ast'] {
30-
// const toParse = parseForESLint(code, options);
31-
// storedAST = toParse.ast;
32-
// return toParse.ast;
33-
// },
34-
});
35-
36-
for (const name of Object.keys(rules)) {
7+
for (const name in rules) {
378
linter.defineRule(`@typescript-eslint/${name}`, rules[name]);
389
}
39-
40-
const ruleNames = Array.from(linter.getRules()).map(value => {
41-
return {
42-
name: value[0],
43-
description: value[1]?.meta?.docs?.description,
44-
};
45-
});
46-
47-
return {
48-
ruleNames: ruleNames,
49-
50-
updateOptions(options) {
51-
compilerOptions = options || {};
52-
},
53-
54-
getScope() {
55-
return storedScope;
56-
},
57-
58-
getAst() {
59-
return storedAST;
60-
},
61-
62-
getTsAst() {
63-
return storedTsAST;
64-
},
65-
66-
lint(code, parserOptions, rules) {
67-
return linter.verify(code, {
68-
parser: PARSER_NAME,
69-
parserOptions,
70-
rules,
71-
});
72-
},
73-
};
10+
return linter;
7411
}
12+
13+
export { analyze } from '@typescript-eslint/scope-manager/dist/analyze';
14+
export { visitorKeys } from '@typescript-eslint/visitor-keys/dist/visitor-keys';
15+
export { astConverter } from '@typescript-eslint/typescript-estree/dist/ast-converter';
16+
export { getScriptKind } from '@typescript-eslint/typescript-estree/dist/create-program/getScriptKind';

packages/website-eslint/src/linter/parser.js

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,13 @@
1-
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
2-
import type { ParserOptions } from '@typescript-eslint/types';
3-
import type { SourceFile, CompilerOptions } from 'typescript';
4-
5-
export type LintMessage = TSESLint.Linter.LintMessage;
6-
export type RuleFix = TSESLint.RuleFix;
7-
export type RulesRecord = TSESLint.Linter.RulesRecord;
8-
export type RuleEntry = TSESLint.Linter.RuleEntry;
9-
10-
export interface WebLinter {
11-
ruleNames: { name: string; description?: string }[];
12-
13-
getAst(): TSESTree.Program;
14-
getTsAst(): SourceFile;
15-
getScope(): Record<string, unknown>;
16-
updateOptions(options?: Record<string, unknown>): void;
17-
18-
lint(
19-
code: string,
20-
parserOptions: ParserOptions,
21-
rules?: RulesRecord,
22-
): LintMessage[];
1+
import type { TSESLint } from '@typescript-eslint/utils';
2+
3+
import { analyze } from '@typescript-eslint/scope-manager/dist/analyze';
4+
import { astConverter } from '@typescript-eslint/typescript-estree/dist/ast-converter';
5+
import { getScriptKind } from '@typescript-eslint/typescript-estree/dist/create-program/getScriptKind';
6+
7+
export interface LintUtils {
8+
createLinter: () => TSESLint.Linter;
9+
analyze: typeof analyze;
10+
visitorKeys: TSESLint.SourceCode.VisitorKeys;
11+
astConverter: typeof astConverter;
12+
getScriptKind: typeof getScriptKind;
2313
}
24-
25-
export interface LinterLoader {
26-
loadLinter(
27-
libMap: Map<string, string>,
28-
compilerOptions: CompilerOptions,
29-
): WebLinter;
30-
}
31-
32-
export type {
33-
DebugLevel,
34-
EcmaVersion,
35-
ParserOptions,
36-
SourceType,
37-
TSESTree,
38-
} from '@typescript-eslint/types';

packages/website/src/components/ASTViewerESTree.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react';
22

33
import ASTViewer from './ast/ASTViewer';
44
import type { ASTViewerBaseProps, ASTViewerModelMap } from './ast/types';
5-
import type { TSESTree } from '@typescript-eslint/website-eslint';
5+
import type { TSESTree } from '@typescript-eslint/utils';
66
import { serialize } from './ast/serializer/serializer';
77
import { createESTreeSerializer } from './ast/serializer/serializerESTree';
88

packages/website/src/components/Playground.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import ASTViewerTS from './ASTViewerTS';
1717

1818
import type { RuleDetails, SelectedRange } from './types';
1919

20-
import type { TSESTree } from '@typescript-eslint/website-eslint';
20+
import type { TSESTree } from '@typescript-eslint/utils';
2121
import type { SourceFile } from 'typescript';
2222
import ASTViewerScope from '@site/src/components/ASTViewerScope';
2323

@@ -44,7 +44,7 @@ function Playground(): JSX.Element {
4444
showAST: false,
4545
sourceType: 'module',
4646
code: '',
47-
ts: process.env.TS_VERSION,
47+
ts: process.env.TS_VERSION!,
4848
rules: {},
4949
tsConfig: {},
5050
});

packages/website/src/components/ast/serializer/serializerESTree.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ASTViewerModel, Serializer } from '../types';
2+
import type { TSESTree } from '@typescript-eslint/utils';
23
import { isRecord } from '../utils';
3-
import type { TSESTree } from '@typescript-eslint/website-eslint';
44

55
export const propsToFilter = ['parent', 'comments', 'tokens'];
66

packages/website/src/components/ast/serializer/serializerScope.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ASTViewerModel, Serializer, SelectedRange } from '../types';
2-
import type { TSESTree } from '@typescript-eslint/website-eslint';
2+
import type { TSESTree } from '@typescript-eslint/utils';
33
import { isRecord } from '../utils';
44

55
function isESTreeNode(

packages/website/src/components/ast/serializer/serializerTS.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const propsToFilter = [
2020
'jsDocComment',
2121
'lineMap',
2222
'externalModuleIndicator',
23+
'setExternalModuleIndicator',
2324
'bindDiagnostics',
2425
'transformFlags',
2526
'resolvedModules',

0 commit comments

Comments
 (0)