Skip to content

Commit

Permalink
refactor: Remove AST converter class
Browse files Browse the repository at this point in the history
  • Loading branch information
Quramy committed Mar 27, 2023
1 parent c2e5435 commit 49263f9
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 58 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@
"devDependencies": {
"@types/eslint": "8.4.6",
"@types/estree": "1.0.0",
"@types/is-glob": "4.0.2",
"@types/jest": "28.1.8",
"@types/node": "18.15.10",
"@typescript-eslint/eslint-plugin": "5.26.0",
"@typescript-eslint/parser": "5.26.0",
"@typescript-eslint/typescript-estree": "5.26.0",
"eslint": "8.25.0",
"eslint-config-prettier": "8.8.0",
"fretted-strings": "1.0.1",
Expand Down
34 changes: 0 additions & 34 deletions src/ast-converter.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/eslint-adapter.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from "path";
import ts from "typescript";
import { mark, Frets } from "fretted-strings";
import { AstConverter } from "./ast-converter";
import { ESLintAdapter } from "./eslint-adapter";
import { ConfigProvider } from "./eslint-config-provider";
import { Legacy } from "@eslint/eslintrc";
Expand Down Expand Up @@ -39,7 +38,6 @@ describe("ESLintAdapter", () => {
semi: 2,
};
const adapter = new ESLintAdapter({
converter: new AstConverter(),
getSourceFile: () => ts.createSourceFile("main.ts", "const x = 1", ts.ScriptTarget.ESNext, true),
configProvider,
logger: () => {},
Expand All @@ -65,7 +63,6 @@ describe("ESLintAdapter", () => {
frets,
);
const adapter = new ESLintAdapter({
converter: new AstConverter(),
getSourceFile: () => ts.createSourceFile("main.ts", content, ts.ScriptTarget.ESNext, true),
configProvider,
logger: () => {},
Expand All @@ -91,7 +88,6 @@ describe("ESLintAdapter", () => {
frets,
);
const adapter = new ESLintAdapter({
converter: new AstConverter(),
getSourceFile: () => ts.createSourceFile("main.ts", content, ts.ScriptTarget.ESNext, true),
configProvider,
logger: () => {},
Expand Down Expand Up @@ -123,7 +119,6 @@ describe("ESLintAdapter", () => {
frets,
);
const adapter = new ESLintAdapter({
converter: new AstConverter(),
getSourceFile: () => ts.createSourceFile("main.ts", content, ts.ScriptTarget.ESNext, true),
configProvider,
logger: () => {},
Expand Down
35 changes: 27 additions & 8 deletions src/eslint-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import ts from "typescript";
import { Linter, ESLint } from "eslint";
import { AstConverter } from "./ast-converter";
import { SourceCode, Linter, ESLint } from "eslint";
import { parseForESLint, type ParserOptions } from "@typescript-eslint/parser";

import { InvalidParserError } from "./errors";
import { ConfigProvider } from "./eslint-config-provider";
import { TS_LANGSERVICE_ESLINT_DIAGNOSTIC_ERROR_CODE } from "./consts";
import { ParserOptions } from "@typescript-eslint/parser";

export function translateToDiagnosticsFromESLintResult(
result: Linter.LintMessage[],
Expand Down Expand Up @@ -90,27 +90,46 @@ export function translateToCodeFixesFromESLintResult(
export type ESLintAdapterOptions = {
logger: (msg: string) => void;
getSourceFile: (fileName: string) => ts.SourceFile | undefined;
converter: AstConverter;
configProvider: ConfigProvider;
};

export class ESLintAdapter {
private readonly linter: Linter;
private readonly logger: (msg: string) => void;
private readonly converter: AstConverter;
private readonly configProvider: ConfigProvider;
private readonly getSourceFile: (fileName: string) => ts.SourceFile | undefined;
private readonly ignoredFilepathMap: Map<string, boolean>;

public constructor({ logger, converter, configProvider, getSourceFile }: ESLintAdapterOptions) {
public constructor({ logger, configProvider, getSourceFile }: ESLintAdapterOptions) {
this.linter = new Linter();
this.logger = logger;
this.converter = converter;
this.configProvider = configProvider;
this.getSourceFile = getSourceFile;
this.ignoredFilepathMap = new Map();
}

private convertToESLintSourceCode(src: ts.SourceFile, filename: string, options?: ParserOptions | null) {
const code = src.getFullText();
const originalOpt = options ?? {};
const { ast, scopeManager, services, visitorKeys } = parseForESLint(code, {
...originalOpt,
filePath: filename,
comment: true,
loc: true,
range: true,
tokens: true,
warnOnUnsupportedTypeScriptVersion: false,
});
const source = new SourceCode({
text: code,
ast,
scopeManager,
parserServices: services,
visitorKeys,
} as unknown as any);
return source;
}

private getESLintResult(fileName: string, sourceFile: ts.SourceFile) {
if (this.ignoredFilepathMap.get(fileName) === true) return [];
const configArray = this.configProvider.getConfigArrayForFile(fileName);
Expand All @@ -119,7 +138,7 @@ export class ESLintAdapter {
throw new InvalidParserError();
}
const parserOptions = (configFileContent.parserOptions ? configFileContent.parserOptions : {}) as ParserOptions;
const sourceCode = this.converter.convertToESLintSourceCode(sourceFile, fileName, parserOptions);
const sourceCode = this.convertToESLintSourceCode(sourceFile, fileName, parserOptions);

// See https://github.com/eslint/eslint/blob/v6.1.0/lib/linter/linter.js#L1130
return this.linter.verify(sourceCode, configArray as any, { filename: fileName });
Expand Down
4 changes: 0 additions & 4 deletions src/plugin-module-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import path from "path";
import ts from "typescript/lib/tsserverlibrary";
import { LanguageServiceProxyBuilder } from "./language-service-proxy-builder";
import { ESLintAdapter } from "./eslint-adapter";
import { AstConverter } from "./ast-converter";
import { ESLintConfigProvider } from "./eslint-config-provider";
import { TS_LANGSERVICE_ESLINT_DIAGNOSTIC_ERROR_CODE } from "./consts";

Expand Down Expand Up @@ -33,8 +32,6 @@ function create(info: ts.server.PluginCreateInfo): ts.LanguageService {
return program;
};

const converter = new AstConverter();

const configProvider = new ESLintConfigProvider({
directoriesToWatch: watchDirs,
log: logger,
Expand All @@ -43,7 +40,6 @@ function create(info: ts.server.PluginCreateInfo): ts.LanguageService {

const adapter = new ESLintAdapter({
logger,
converter,
configProvider,
getSourceFile(fileName: string) {
return getProgram().getSourceFile(fileName);
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -953,11 +953,6 @@
dependencies:
"@types/node" "*"

"@types/is-glob@4.0.2":
version "4.0.2"
resolved "https://registry.yarnpkg.com/@types/is-glob/-/is-glob-4.0.2.tgz#c243dd0d09eac2992130142419ff2308ffd988bf"
integrity sha512-4j5G9Y5jljDSICQ1R2f/Rcyoj6DZmYGneny+p/cDkjep0rkqNg0W73Ty0bVjMUTZgLXHf8oiMjg1XC3CDwCz+g==

"@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0", "@types/istanbul-lib-coverage@^2.0.1":
version "2.0.3"
resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.3.tgz#4ba8ddb720221f432e443bd5f9117fd22cfd4762"
Expand Down

0 comments on commit 49263f9

Please sign in to comment.