Skip to content

Commit

Permalink
chore(website): reuse getScriptKind from typescript-estree
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 committed May 24, 2022
1 parent df7e947 commit 863b054
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 31 deletions.
1 change: 1 addition & 0 deletions packages/website-eslint/src/linter/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export function createLinter() {
export { analyze } from '@typescript-eslint/scope-manager/dist/analyze';
export { visitorKeys } from '@typescript-eslint/visitor-keys/dist/visitor-keys';
export { astConverter } from '@typescript-eslint/typescript-estree/dist/ast-converter';
export { getScriptKind } from '@typescript-eslint/typescript-estree/dist/create-program/shared';
2 changes: 2 additions & 0 deletions packages/website-eslint/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import type { TSESLint } from '@typescript-eslint/utils';

import { analyze } from '@typescript-eslint/scope-manager/dist/analyze';
import { astConverter } from '@typescript-eslint/typescript-estree/dist/ast-converter';
import { getScriptKind } from '@typescript-eslint/typescript-estree/dist/create-program/shared';

export interface LintUtils {
createLinter: () => TSESLint.Linter;
analyze: typeof analyze;
visitorKeys: TSESLint.SourceCode.VisitorKeys;
astConverter: typeof astConverter;
getScriptKind: typeof getScriptKind;
}
38 changes: 8 additions & 30 deletions packages/website/src/components/linter/CompilerHost.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
import type { ScriptKind, System, SourceFile, CompilerHost } from 'typescript';
import type { System, SourceFile, CompilerHost } from 'typescript';
import type { LintUtils } from '@typescript-eslint/website-eslint';

function getScriptKind(
ts: typeof import('typescript'),
filePath: string,
): ScriptKind {
const extension = (/(\.[a-z]+)$/.exec(filePath)?.[0] ?? '').toLowerCase();

switch (extension) {
case '.ts':
return ts.ScriptKind.TS;
case '.tsx':
return ts.ScriptKind.TSX;

case '.js':
return ts.ScriptKind.JS;
case '.jsx':
return ts.ScriptKind.JSX;

case '.json':
return ts.ScriptKind.JSON;

default:
// unknown extension, force typescript to ignore the file extension, and respect the user's setting
return ts.ScriptKind.TS;
}
}
import { extra } from './config';

/**
* Creates an in-memory CompilerHost -which is essentially an extra wrapper to System
Expand All @@ -36,22 +13,23 @@ function getScriptKind(
*/
export function createVirtualCompilerHost(
sys: System,
ts: typeof import('typescript'),
lintUtils: LintUtils,
): CompilerHost {
return {
...sys,
getCanonicalFileName: (fileName: string) => fileName,
getDefaultLibFileName: options => '/' + ts.getDefaultLibFileName(options),
getDefaultLibFileName: options =>
'/' + window.ts.getDefaultLibFileName(options),
getNewLine: () => sys.newLine,
getSourceFile(fileName, languageVersionOrOptions): SourceFile | undefined {
if (this.fileExists(fileName)) {
const file = this.readFile(fileName) ?? '';
return ts.createSourceFile(
return window.ts.createSourceFile(
fileName,
file,
languageVersionOrOptions,
true,
getScriptKind(ts, fileName),
lintUtils.getScriptKind(extra, fileName),
);
}
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion packages/website/src/components/linter/WebLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class WebLinter {
this.lintUtils = lintUtils;
this.linter = lintUtils.createLinter();

this.host = createVirtualCompilerHost(system, window.ts);
this.host = createVirtualCompilerHost(system, lintUtils);

this.linter.defineParser(PARSER_NAME, {
parseForESLint: (text, options?: ParserOptions) => {
Expand Down

0 comments on commit 863b054

Please sign in to comment.