Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/checker/layersChecker.ts
3520 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import ts from 'typescript';
7
import { readFileSync, existsSync } from 'fs';
8
import { resolve, dirname, join } from 'path';
9
import { match } from 'minimatch';
10
11
//
12
// #############################################################################################
13
//
14
// A custom typescript checker for the specific task of detecting the use of certain types in a
15
// layer that does not allow such use.
16
//
17
// Make changes to below RULES to lift certain files from these checks only if absolutely needed
18
//
19
// NOTE: Most layer checks are done via tsconfig.<layer>.json files.
20
//
21
// #############################################################################################
22
//
23
24
// Types that are defined in a common layer but are known to be only
25
// available in native environments should not be allowed in browser
26
const NATIVE_TYPES = [
27
'NativeParsedArgs',
28
'INativeEnvironmentService',
29
'AbstractNativeEnvironmentService',
30
'INativeWindowConfiguration',
31
'ICommonNativeHostService',
32
'INativeHostService',
33
'IMainProcessService',
34
'INativeBrowserElementsService',
35
];
36
37
const RULES: IRule[] = [
38
39
// Tests: skip
40
{
41
target: '**/vs/**/test/**',
42
skip: true // -> skip all test files
43
},
44
45
// Common: vs/platform services that can access native types
46
{
47
target: `**/vs/platform/{${[
48
'environment/common/*.ts',
49
'window/common/window.ts',
50
'native/common/native.ts',
51
'native/common/nativeHostService.ts',
52
'browserElements/common/browserElements.ts',
53
'browserElements/common/nativeBrowserElementsService.ts'
54
].join(',')}}`,
55
disallowedTypes: [/* Ignore native types that are defined from here */],
56
},
57
58
// Common: vs/base/parts/sandbox/electron-browser/preload{,-aux}.ts
59
{
60
target: '**/vs/base/parts/sandbox/electron-browser/preload{,-aux}.ts',
61
disallowedTypes: NATIVE_TYPES,
62
},
63
64
// Common
65
{
66
target: '**/vs/**/common/**',
67
disallowedTypes: NATIVE_TYPES,
68
},
69
70
// Common
71
{
72
target: '**/vs/**/worker/**',
73
disallowedTypes: NATIVE_TYPES,
74
},
75
76
// Browser
77
{
78
target: '**/vs/**/browser/**',
79
disallowedTypes: NATIVE_TYPES,
80
},
81
82
// Electron (main, utility)
83
{
84
target: '**/vs/**/{electron-main,electron-utility}/**',
85
disallowedTypes: [
86
'ipcMain' // not allowed, use validatedIpcMain instead
87
]
88
}
89
];
90
91
const TS_CONFIG_PATH = join(__dirname, '../../', 'src', 'tsconfig.json');
92
93
interface IRule {
94
target: string;
95
skip?: boolean;
96
disallowedTypes?: string[];
97
}
98
99
let hasErrors = false;
100
101
function checkFile(program: ts.Program, sourceFile: ts.SourceFile, rule: IRule) {
102
checkNode(sourceFile);
103
104
function checkNode(node: ts.Node): void {
105
if (node.kind !== ts.SyntaxKind.Identifier) {
106
return ts.forEachChild(node, checkNode); // recurse down
107
}
108
109
const checker = program.getTypeChecker();
110
const symbol = checker.getSymbolAtLocation(node);
111
112
if (!symbol) {
113
return;
114
}
115
116
let text = symbol.getName();
117
let _parentSymbol: any = symbol;
118
119
while (_parentSymbol.parent) {
120
_parentSymbol = _parentSymbol.parent;
121
}
122
123
const parentSymbol = _parentSymbol as ts.Symbol;
124
text = parentSymbol.getName();
125
126
if (rule.disallowedTypes?.some(disallowed => disallowed === text)) {
127
const { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart());
128
console.log(`[build/checker/layersChecker.ts]: Reference to type '${text}' violates layer '${rule.target}' (${sourceFile.fileName} (${line + 1},${character + 1}). Learn more about our source code organization at https://github.com/microsoft/vscode/wiki/Source-Code-Organization.`);
129
130
hasErrors = true;
131
return;
132
}
133
}
134
}
135
136
function createProgram(tsconfigPath: string): ts.Program {
137
const tsConfig = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
138
139
const configHostParser: ts.ParseConfigHost = { fileExists: existsSync, readDirectory: ts.sys.readDirectory, readFile: file => readFileSync(file, 'utf8'), useCaseSensitiveFileNames: process.platform === 'linux' };
140
const tsConfigParsed = ts.parseJsonConfigFileContent(tsConfig.config, configHostParser, resolve(dirname(tsconfigPath)), { noEmit: true });
141
142
const compilerHost = ts.createCompilerHost(tsConfigParsed.options, true);
143
144
return ts.createProgram(tsConfigParsed.fileNames, tsConfigParsed.options, compilerHost);
145
}
146
147
//
148
// Create program and start checking
149
//
150
const program = createProgram(TS_CONFIG_PATH);
151
152
for (const sourceFile of program.getSourceFiles()) {
153
for (const rule of RULES) {
154
if (match([sourceFile.fileName], rule.target).length > 0) {
155
if (!rule.skip) {
156
checkFile(program, sourceFile, rule);
157
}
158
159
break;
160
}
161
}
162
}
163
164
if (hasErrors) {
165
process.exit(1);
166
}
167
168