-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
shared.ts
144 lines (122 loc) · 3.77 KB
/
shared.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import path from 'path';
import type { Program } from 'typescript';
import * as ts from 'typescript';
import type { ModuleResolver } from '../parser-options';
import type { ParseSettings } from '../parseSettings';
interface ASTAndNoProgram {
ast: ts.SourceFile;
program: null;
}
interface ASTAndDefiniteProgram {
ast: ts.SourceFile;
program: ts.Program;
}
type ASTAndProgram = ASTAndNoProgram | ASTAndDefiniteProgram;
/**
* Compiler options required to avoid critical functionality issues
*/
const CORE_COMPILER_OPTIONS: ts.CompilerOptions = {
noEmit: true, // required to avoid parse from causing emit to occur
/**
* Flags required to make no-unused-vars work
*/
noUnusedLocals: true,
noUnusedParameters: true,
};
/**
* Default compiler options for program generation
*/
const DEFAULT_COMPILER_OPTIONS: ts.CompilerOptions = {
...CORE_COMPILER_OPTIONS,
allowNonTsExtensions: true,
allowJs: true,
checkJs: true,
};
function createDefaultCompilerOptionsFromExtra(
parseSettings: ParseSettings,
): ts.CompilerOptions {
if (parseSettings.debugLevel.has('typescript')) {
return {
...DEFAULT_COMPILER_OPTIONS,
extendedDiagnostics: true,
};
}
return DEFAULT_COMPILER_OPTIONS;
}
// This narrows the type so we can be sure we're passing canonical names in the correct places
type CanonicalPath = string & { __brand: unknown };
// typescript doesn't provide a ts.sys implementation for browser environments
const useCaseSensitiveFileNames =
ts.sys !== undefined ? ts.sys.useCaseSensitiveFileNames : true;
const correctPathCasing = useCaseSensitiveFileNames
? (filePath: string): string => filePath
: (filePath: string): string => filePath.toLowerCase();
function getCanonicalFileName(filePath: string): CanonicalPath {
let normalized = path.normalize(filePath);
if (normalized.endsWith(path.sep)) {
normalized = normalized.slice(0, -1);
}
return correctPathCasing(normalized) as CanonicalPath;
}
function ensureAbsolutePath(p: string, tsconfigRootDir: string): string {
return path.isAbsolute(p)
? p
: path.join(tsconfigRootDir || process.cwd(), p);
}
function canonicalDirname(p: CanonicalPath): CanonicalPath {
return path.dirname(p) as CanonicalPath;
}
const DEFINITION_EXTENSIONS = [
ts.Extension.Dts,
ts.Extension.Dcts,
ts.Extension.Dmts,
] as const;
function getExtension(fileName: string | undefined): string | null {
if (!fileName) {
return null;
}
return (
DEFINITION_EXTENSIONS.find(definitionExt =>
fileName.endsWith(definitionExt),
) ?? path.extname(fileName)
);
}
function getAstFromProgram(
currentProgram: Program,
parseSettings: ParseSettings,
): ASTAndDefiniteProgram | undefined {
const ast = currentProgram.getSourceFile(parseSettings.filePath);
// working around https://github.com/typescript-eslint/typescript-eslint/issues/1573
const expectedExt = getExtension(parseSettings.filePath);
const returnedExt = getExtension(ast?.fileName);
if (expectedExt !== returnedExt) {
return undefined;
}
return ast && { ast, program: currentProgram };
}
function getModuleResolver(moduleResolverPath: string): ModuleResolver {
let moduleResolver: ModuleResolver;
try {
moduleResolver = require(moduleResolverPath) as ModuleResolver;
} catch (error) {
const errorLines = [
'Could not find the provided parserOptions.moduleResolver.',
'Hint: use an absolute path if you are not in control over where the ESLint instance runs.',
];
throw new Error(errorLines.join('\n'));
}
return moduleResolver;
}
export {
ASTAndDefiniteProgram,
ASTAndNoProgram,
ASTAndProgram,
CORE_COMPILER_OPTIONS,
canonicalDirname,
CanonicalPath,
createDefaultCompilerOptionsFromExtra,
ensureAbsolutePath,
getCanonicalFileName,
getAstFromProgram,
getModuleResolver,
};