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