Skip to content
This repository was archived by the owner on Sep 14, 2022. It is now read-only.

Commit 08d8b6b

Browse files
committed
refactor: add option converter
1 parent a6e3978 commit 08d8b6b

7 files changed

+92
-26
lines changed

.babelrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
],
1111
"@babel/preset-typescript",
1212
"@babel/preset-react"
13-
]
13+
],
14+
"ignore": ["**/__tests__"]
1415
}

jest.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
preset: "ts-jest",
3+
testEnvironment: "node",
4+
moduleNameMapper: {
5+
"@/(.*)$": "<rootDir>/src/$1",
6+
},
7+
};

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"gen:libs": "ts-node scripts/gen-libs.ts",
1818
"clear": "ts-node scripts/rm-rf.ts ./dist ",
1919
"//deploy": "deploy",
20-
"deploy": "npm run build && gh-pages -d dist"
20+
"deploy": "npm run build && gh-pages -d dist",
21+
"//test": "test",
22+
"test": "jest"
2123
},
2224
"repository": {
2325
"type": "git",
@@ -50,6 +52,7 @@
5052
"@babel/preset-typescript": "^7.10.4",
5153
"@types/codemirror": "0.0.98",
5254
"@types/eslint": "^7.2.4",
55+
"@types/jest": "^26.0.14",
5356
"@types/react": "^16.9.51",
5457
"@types/react-bootstrap": "^0.32.24",
5558
"@types/react-dom": "^16.9.8",
@@ -63,6 +66,7 @@
6366
"gh-pages": "^3.1.0",
6467
"html-loader": "^1.3.1",
6568
"html-webpack-plugin": "^4.5.0",
69+
"jest": "^26.5.3",
6670
"json-loader": "^0.5.7",
6771
"lodash": "^4.17.20",
6872
"null-loader": "^4.0.0",
@@ -71,6 +75,7 @@
7175
"react-app-rewired": "^2.1.6",
7276
"style-loader": "^1.3.0",
7377
"terser-webpack-plugin": "^4.2.3",
78+
"ts-jest": "^26.4.1",
7479
"ts-node": "^9.0.0",
7580
"webpack": "^4.44.2",
7681
"webpack-bundle-analyzer": "^3.9.0",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as converter from "@/lib/parser-options-converter";
2+
import { ParserOptions } from "@typescript-eslint/parser";
3+
import { JsxEmit } from "typescript";
4+
5+
const JSX_PARSER_OPTIONS: ParserOptions = {
6+
ecmaVersion: 2019,
7+
ecmaFeatures: {
8+
jsx: true,
9+
globalReturn: true,
10+
},
11+
sourceType: "module",
12+
};
13+
14+
describe("parser-options-converter", () => {
15+
it("toTSESTOptions", () => {
16+
expect(converter.toTSESTOptions(JSX_PARSER_OPTIONS)).toMatchObject({
17+
jsx: true,
18+
useJSXTextNode: true,
19+
});
20+
});
21+
22+
it("toCompilerOptions", () => {
23+
expect(converter.toCompilerOptions(JSX_PARSER_OPTIONS)).toMatchObject({
24+
jsx: JsxEmit.Preserve,
25+
});
26+
});
27+
});

src/lib/create-ast-program.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,32 @@ import type { SourceFile, Program } from "typescript";
99
import { createCompilerHost } from "./compiler-host";
1010
import { createFilesIncludeLibs } from "./files";
1111
import { createSourceFilesIncludeLibs } from "./source-files";
12+
import { toCompilerOptions } from "@/lib/parser-options-converter";
1213
import { DEMO_FILE_NAME } from "@/constants";
14+
import type { ParserOptions } from "@typescript-eslint/types";
1315

1416
interface ASTandProgram {
1517
ast?: SourceFile;
1618
program: Program;
1719
}
1820

19-
export function createAstAndProgram(code: string, extra: any): ASTandProgram {
21+
export function createAstAndProgram(
22+
code: string,
23+
parserOptions: ParserOptions
24+
): ASTandProgram {
2025
const files = createFilesIncludeLibs(DEMO_FILE_NAME, code);
2126
const sourceFiles = createSourceFilesIncludeLibs(
2227
DEMO_FILE_NAME,
2328
code,
24-
extra.jsx ? ScriptKind.TSX : ScriptKind.TS
29+
parserOptions?.ecmaFeatures?.jsx ? ScriptKind.TSX : ScriptKind.TS
2530
);
2631

2732
const compilierHost = createCompilerHost(files, sourceFiles);
28-
33+
const compilerOptions = toCompilerOptions(parserOptions);
34+
compilerOptions.jsx;
2935
const program = createProgram(
3036
Object.keys(files),
31-
{
32-
noResolve: true,
33-
strict: true,
34-
target: ScriptTarget.Latest,
35-
jsx: extra.jsx ? JsxEmit.Preserve : undefined,
36-
module: ModuleKind.ES2015,
37-
},
37+
compilerOptions,
3838
compilierHost
3939
);
4040
const ast = program.getSourceFile(DEMO_FILE_NAME);

src/lib/parser-options-converter.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { ParserOptions } from "@typescript-eslint/types";
2+
import type { AnalyzeOptions } from "@typescript-eslint/scope-manager";
3+
import type { TSESTreeOptions } from "@typescript-eslint/typescript-estree";
4+
import { CompilerOptions, ScriptTarget, JsxEmit, ModuleKind } from "typescript";
5+
6+
export function toAnalyzeOptions(parserOptions: ParserOptions): AnalyzeOptions {
7+
return {
8+
ecmaVersion: parserOptions.ecmaVersion,
9+
globalReturn: parserOptions.ecmaFeatures?.globalReturn ?? false,
10+
sourceType: parserOptions.sourceType ?? "script",
11+
};
12+
}
13+
14+
export function toTSESTOptions(parserOptions: ParserOptions): TSESTreeOptions {
15+
return Object.assign({}, parserOptions, {
16+
jsx: parserOptions.ecmaFeatures?.jsx ?? false,
17+
useJSXTextNode: true,
18+
projectFolderIgnoreList: [],
19+
});
20+
}
21+
22+
export function toCompilerOptions(
23+
parserOptions: ParserOptions
24+
): CompilerOptions {
25+
return {
26+
noResolve: true,
27+
strict: true,
28+
target: ScriptTarget.Latest,
29+
jsx: parserOptions.ecmaFeatures?.jsx ? JsxEmit.Preserve : undefined,
30+
module: ModuleKind.ES2015,
31+
};
32+
}

src/lib/parser.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { analyze } from "@typescript-eslint/scope-manager";
22
import { visitorKeys } from "@typescript-eslint/typescript-estree";
33
import { astConverter } from "../../node_modules/@typescript-eslint/typescript-estree/dist/ast-converter";
44
import { createAstAndProgram } from "@/lib/create-ast-program";
5+
import {
6+
toAnalyzeOptions,
7+
toTSESTOptions,
8+
} from "@/lib/parser-options-converter";
59
import type { ParserOptions, TSESTree } from "@typescript-eslint/types";
610
import type {
711
ParserServices,
@@ -54,7 +58,7 @@ function parseAndGenerateServices<T extends TSESTreeOptions = TSESTreeOptions>(
5458
extra.code = code;
5559
extra.jsx = options.jsx;
5660

57-
const { ast, program } = createAstAndProgram(code, extra);
61+
const { ast, program } = createAstAndProgram(code, options);
5862
const { estree, astMaps } = astConverter(ast!, extra, true);
5963
return {
6064
ast: estree as AST<T>,
@@ -71,19 +75,9 @@ export function parseForESLint(
7175
code: string,
7276
parserOptions: ParserOptions
7377
): ParseForESLintResult {
74-
const TSESOptions: TSESTreeOptions = Object.assign(parserOptions, {
75-
jsx: parserOptions.ecmaFeatures?.jsx || false,
76-
useJSXTextNode: true,
77-
});
78-
79-
const { ast, services } = parseAndGenerateServices(code, TSESOptions);
80-
81-
const analyzeOptions = {
82-
ecmaVersion: parserOptions.ecmaVersion,
83-
globalReturn: parserOptions.ecmaFeatures?.globalReturn,
84-
sourceType: parserOptions.sourceType,
85-
};
86-
78+
const tsesOptions = toTSESTOptions(parserOptions);
79+
const { ast, services } = parseAndGenerateServices(code, tsesOptions);
80+
const analyzeOptions = toAnalyzeOptions(parserOptions);
8781
const scopeManager = analyze(ast, analyzeOptions);
8882
return { ast, services, scopeManager, visitorKeys };
8983
}

0 commit comments

Comments
 (0)