|
| 1 | +"use strict"; |
| 2 | +/*--------------------------------------------------------------------------------------------- |
| 3 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 5 | + *--------------------------------------------------------------------------------------------*/ |
| 6 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 7 | +const ts = require("typescript"); |
| 8 | +const fs_1 = require("fs"); |
| 9 | +const path_1 = require("path"); |
| 10 | +const minimatch_1 = require("minimatch"); |
| 11 | +// |
| 12 | +// ############################################################################################# |
| 13 | +// |
| 14 | +// A custom typescript linter for the specific task of detecting the use of certain globals in a |
| 15 | +// layer that does not allow the use. For example: |
| 16 | +// - using DOM globals in common/node/electron-main layer (e.g. HTMLElement) |
| 17 | +// - using node.js globals in common/browser layer (e.g. process) |
| 18 | +// |
| 19 | +// Make changes to below RULES to lift certain files from these checks only if absolutely needed |
| 20 | +// |
| 21 | +// ############################################################################################# |
| 22 | +// |
| 23 | +const RULES = { |
| 24 | + "no-nodejs-globals": [ |
| 25 | + { |
| 26 | + "target": "**/vs/**/test/{common,browser}/**", |
| 27 | + "allowed": [ |
| 28 | + "process", |
| 29 | + "Buffer", |
| 30 | + "__filename", |
| 31 | + "__dirname" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "target": "**/vs/workbench/api/common/extHostExtensionService.ts", |
| 36 | + "allowed": [ |
| 37 | + "global" // -> safe access to 'global' |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "target": "**/vs/**/{common,browser}/**", |
| 42 | + "allowed": [ /* none */] |
| 43 | + } |
| 44 | + ], |
| 45 | + "no-dom-globals": [ |
| 46 | + { |
| 47 | + "target": "**/vs/base/parts/quickopen/common/quickOpen.ts", |
| 48 | + "allowed": [ |
| 49 | + "HTMLElement" // quick open will be replaced with a different widget soon |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "target": "**/vs/**/test/{common,node,electron-main}/**", |
| 54 | + "allowed": [ |
| 55 | + "document", |
| 56 | + "HTMLElement", |
| 57 | + "createElement" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "target": "**/vs/**/{common,node,electron-main}/**", |
| 62 | + "allowed": [ /* none */] |
| 63 | + } |
| 64 | + ] |
| 65 | +}; |
| 66 | +const TS_CONFIG_PATH = path_1.join(__dirname, '../../', 'src', 'tsconfig.json'); |
| 67 | +const DOM_GLOBALS_DEFINITION = 'lib.dom.d.ts'; |
| 68 | +const DISALLOWED_DOM_GLOBALS = [ |
| 69 | + "window", |
| 70 | + "document", |
| 71 | + "HTMLElement", |
| 72 | + "createElement" |
| 73 | +]; |
| 74 | +const NODE_GLOBALS_DEFINITION = '@types/node'; |
| 75 | +const DISALLOWED_NODE_GLOBALS = [ |
| 76 | + // https://nodejs.org/api/globals.html#globals_global_objects |
| 77 | + "NodeJS", |
| 78 | + "Buffer", |
| 79 | + "__dirname", |
| 80 | + "__filename", |
| 81 | + "clearImmediate", |
| 82 | + "exports", |
| 83 | + "global", |
| 84 | + "module", |
| 85 | + "process", |
| 86 | + "setImmediate" |
| 87 | +]; |
| 88 | +let hasErrors = false; |
| 89 | +function checkFile(program, sourceFile, rule) { |
| 90 | + checkNode(sourceFile); |
| 91 | + function checkNode(node) { |
| 92 | + if (node.kind !== ts.SyntaxKind.Identifier) { |
| 93 | + return ts.forEachChild(node, checkNode); // recurse down |
| 94 | + } |
| 95 | + const text = node.getText(sourceFile); |
| 96 | + if (!rule.disallowedGlobals.some(disallowedGlobal => disallowedGlobal === text)) { |
| 97 | + return; // only if disallowed |
| 98 | + } |
| 99 | + if (rule.allowedGlobals.some(allowed => allowed === text)) { |
| 100 | + return; // override |
| 101 | + } |
| 102 | + const checker = program.getTypeChecker(); |
| 103 | + const symbol = checker.getSymbolAtLocation(node); |
| 104 | + if (symbol) { |
| 105 | + const declarations = symbol.declarations; |
| 106 | + if (Array.isArray(declarations) && symbol.declarations.some(declaration => { |
| 107 | + if (declaration) { |
| 108 | + const parent = declaration.parent; |
| 109 | + if (parent) { |
| 110 | + const sourceFile = parent.getSourceFile(); |
| 111 | + if (sourceFile) { |
| 112 | + const fileName = sourceFile.fileName; |
| 113 | + if (fileName && fileName.indexOf(rule.definition) >= 0) { |
| 114 | + return true; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + return false; |
| 120 | + })) { |
| 121 | + const { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart()); |
| 122 | + console.log(`build/lib/globalsLinter.ts: Cannot use global '${text}' in ${sourceFile.fileName} (${line + 1},${character + 1})`); |
| 123 | + hasErrors = true; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | +function createProgram(tsconfigPath) { |
| 129 | + const tsConfig = ts.readConfigFile(tsconfigPath, ts.sys.readFile); |
| 130 | + const configHostParser = { fileExists: fs_1.existsSync, readDirectory: ts.sys.readDirectory, readFile: file => fs_1.readFileSync(file, "utf8"), useCaseSensitiveFileNames: process.platform === 'linux' }; |
| 131 | + const tsConfigParsed = ts.parseJsonConfigFileContent(tsConfig.config, configHostParser, path_1.resolve(path_1.dirname(tsconfigPath)), { noEmit: true }); |
| 132 | + const compilerHost = ts.createCompilerHost(tsConfigParsed.options, true); |
| 133 | + return ts.createProgram(tsConfigParsed.fileNames, tsConfigParsed.options, compilerHost); |
| 134 | +} |
| 135 | +// |
| 136 | +// Create program and start checking |
| 137 | +// |
| 138 | +const program = createProgram(TS_CONFIG_PATH); |
| 139 | +for (const sourceFile of program.getSourceFiles()) { |
| 140 | + let noDomGlobalsLinter = undefined; |
| 141 | + let noNodeJSGlobalsLinter = undefined; |
| 142 | + for (const rules of RULES["no-dom-globals"]) { |
| 143 | + if (minimatch_1.match([sourceFile.fileName], rules.target).length > 0) { |
| 144 | + noDomGlobalsLinter = { allowed: rules.allowed }; |
| 145 | + break; |
| 146 | + } |
| 147 | + } |
| 148 | + for (const rules of RULES["no-nodejs-globals"]) { |
| 149 | + if (minimatch_1.match([sourceFile.fileName], rules.target).length > 0) { |
| 150 | + noNodeJSGlobalsLinter = { allowed: rules.allowed }; |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | + if (!noDomGlobalsLinter && !noNodeJSGlobalsLinter) { |
| 155 | + continue; // no rule to run |
| 156 | + } |
| 157 | + // No DOM Globals |
| 158 | + if (noDomGlobalsLinter) { |
| 159 | + checkFile(program, sourceFile, { |
| 160 | + definition: DOM_GLOBALS_DEFINITION, |
| 161 | + disallowedGlobals: DISALLOWED_DOM_GLOBALS, |
| 162 | + allowedGlobals: noDomGlobalsLinter.allowed |
| 163 | + }); |
| 164 | + } |
| 165 | + // No node.js Globals |
| 166 | + if (noNodeJSGlobalsLinter) { |
| 167 | + checkFile(program, sourceFile, { |
| 168 | + definition: NODE_GLOBALS_DEFINITION, |
| 169 | + disallowedGlobals: DISALLOWED_NODE_GLOBALS, |
| 170 | + allowedGlobals: noNodeJSGlobalsLinter.allowed |
| 171 | + }); |
| 172 | + } |
| 173 | +} |
| 174 | +if (hasErrors) { |
| 175 | + process.exit(1); |
| 176 | +} |
0 commit comments