Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/checker/layersChecker.ts
5238 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 minimatch 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
// Browser view preload script
65
{
66
target: '**/vs/platform/browserView/electron-browser/preload-browserView.ts',
67
disallowedTypes: NATIVE_TYPES,
68
},
69
70
// Common
71
{
72
target: '**/vs/**/common/**',
73
disallowedTypes: NATIVE_TYPES,
74
},
75
76
// Common
77
{
78
target: '**/vs/**/worker/**',
79
disallowedTypes: NATIVE_TYPES,
80
},
81
82
// Browser
83
{
84
target: '**/vs/**/browser/**',
85
disallowedTypes: NATIVE_TYPES,
86
},
87
88
// Electron (main, utility)
89
{
90
target: '**/vs/**/{electron-main,electron-utility}/**',
91
disallowedTypes: [
92
'ipcMain' // not allowed, use validatedIpcMain instead
93
]
94
}
95
];
96
97
const TS_CONFIG_PATH = join(import.meta.dirname, '../../', 'src', 'tsconfig.json');
98
99
interface IRule {
100
target: string;
101
skip?: boolean;
102
disallowedTypes?: string[];
103
}
104
105
let hasErrors = false;
106
107
function checkFile(program: ts.Program, sourceFile: ts.SourceFile, rule: IRule) {
108
checkNode(sourceFile);
109
110
function checkNode(node: ts.Node): void {
111
if (node.kind !== ts.SyntaxKind.Identifier) {
112
return ts.forEachChild(node, checkNode); // recurse down
113
}
114
115
const checker = program.getTypeChecker();
116
const symbol = checker.getSymbolAtLocation(node);
117
118
if (!symbol) {
119
return;
120
}
121
122
let text = symbol.getName();
123
let _parentSymbol: any = symbol;
124
125
while (_parentSymbol.parent) {
126
_parentSymbol = _parentSymbol.parent;
127
}
128
129
const parentSymbol = _parentSymbol as ts.Symbol;
130
text = parentSymbol.getName();
131
132
if (rule.disallowedTypes?.some(disallowed => disallowed === text)) {
133
const { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart());
134
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.`);
135
136
hasErrors = true;
137
return;
138
}
139
}
140
}
141
142
function createProgram(tsconfigPath: string): ts.Program {
143
const tsConfig = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
144
145
const configHostParser: ts.ParseConfigHost = { fileExists: existsSync, readDirectory: ts.sys.readDirectory, readFile: file => readFileSync(file, 'utf8'), useCaseSensitiveFileNames: process.platform === 'linux' };
146
const tsConfigParsed = ts.parseJsonConfigFileContent(tsConfig.config, configHostParser, resolve(dirname(tsconfigPath)), { noEmit: true });
147
148
const compilerHost = ts.createCompilerHost(tsConfigParsed.options, true);
149
150
return ts.createProgram(tsConfigParsed.fileNames, tsConfigParsed.options, compilerHost);
151
}
152
153
//
154
// Create program and start checking
155
//
156
const program = createProgram(TS_CONFIG_PATH);
157
158
for (const sourceFile of program.getSourceFiles()) {
159
for (const rule of RULES) {
160
if (minimatch.match([sourceFile.fileName], rule.target).length > 0) {
161
if (!rule.skip) {
162
checkFile(program, sourceFile, rule);
163
}
164
165
break;
166
}
167
}
168
}
169
170
if (hasErrors) {
171
process.exit(1);
172
}
173
174