Merge from vscode merge-base (#22769)

* Merge from vscode merge-base

* Turn off basic checks

* Enable compilation, unit, and integration tests
This commit is contained in:
Lewis Sanchez
2023-04-18 18:28:58 -07:00
committed by GitHub
parent 6186358001
commit 6bd0a17d3c
2389 changed files with 92183 additions and 42601 deletions

View File

@@ -3,8 +3,6 @@
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as fs from 'fs';
import * as path from 'path';
import type * as ts from 'typescript';
@@ -73,7 +71,7 @@ function printDiagnostics(options: ITreeShakingOptions, diagnostics: ReadonlyArr
result += `${path.join(options.sourcesRoot, diag.file.fileName)}`;
}
if (diag.file && diag.start) {
let location = diag.file.getLineAndCharacterOfPosition(diag.start);
const location = diag.file.getLineAndCharacterOfPosition(diag.start);
result += `:${location.line + 1}:${location.character}`;
}
result += ` - ` + JSON.stringify(diag.messageText);
@@ -144,6 +142,8 @@ function discoverAndReadFiles(ts: typeof import('typescript'), options: ITreeSha
const queue: string[] = [];
const enqueue = (moduleId: string) => {
// To make the treeshaker work on windows...
moduleId = moduleId.replace(/\\/g, '/');
if (in_queue[moduleId]) {
return;
}
@@ -216,7 +216,7 @@ function processLibFiles(ts: typeof import('typescript'), options: ITreeShakingO
// precess dependencies and "recurse"
const info = ts.preProcessFile(sourceText);
for (let ref of info.libReferenceDirectives) {
for (const ref of info.libReferenceDirectives) {
stack.push(ref.fileName);
}
}
@@ -307,6 +307,12 @@ function getColor(node: ts.Node): NodeColor {
function setColor(node: ts.Node, color: NodeColor): void {
(<any>node).$$$color = color;
}
function markNeededSourceFile(node: ts.SourceFile): void {
(<any>node).$$$neededSourceFile = true;
}
function isNeededSourceFile(node: ts.SourceFile): boolean {
return Boolean((<any>node).$$$neededSourceFile);
}
function nodeOrParentIsBlack(node: ts.Node): boolean {
while (node) {
const color = getColor(node);
@@ -449,6 +455,20 @@ function markNodes(ts: typeof import('typescript'), languageService: ts.Language
});
}
/**
* Return the parent of `node` which is an ImportDeclaration
*/
function findParentImportDeclaration(node: ts.Declaration): ts.ImportDeclaration | null {
let _node: ts.Node = node;
do {
if (ts.isImportDeclaration(_node)) {
return _node;
}
_node = _node.parent;
} while (_node);
return null;
}
function enqueue_gray(node: ts.Node): void {
if (nodeOrParentIsBlack(node) || getColor(node) === NodeColor.Gray) {
return;
@@ -531,6 +551,8 @@ function markNodes(ts: typeof import('typescript'), languageService: ts.Language
console.warn(`Cannot find source file ${filename}`);
return;
}
// This source file should survive even if it is empty
markNeededSourceFile(sourceFile);
enqueue_black(sourceFile);
}
@@ -590,6 +612,10 @@ function markNodes(ts: typeof import('typescript'), languageService: ts.Language
const [symbol, symbolImportNode] = getRealNodeSymbol(ts, checker, node);
if (symbolImportNode) {
setColor(symbolImportNode, NodeColor.Black);
const importDeclarationNode = findParentImportDeclaration(symbolImportNode);
if (importDeclarationNode && ts.isStringLiteral(importDeclarationNode.moduleSpecifier)) {
enqueueImport(importDeclarationNode, importDeclarationNode.moduleSpecifier.text);
}
}
if (isSymbolWithDeclarations(symbol) && !nodeIsInItsOwnDeclaration(nodeSourceFile, node, symbol)) {
@@ -629,7 +655,7 @@ function markNodes(ts: typeof import('typescript'), languageService: ts.Language
// queue the heritage clauses
if (declaration.heritageClauses) {
for (let heritageClause of declaration.heritageClauses) {
for (const heritageClause of declaration.heritageClauses) {
enqueue_black(heritageClause);
}
}
@@ -682,7 +708,7 @@ function generateResult(ts: typeof import('typescript'), languageService: ts.Lan
throw new Error('Could not get program from language service');
}
let result: ITreeShakingResult = {};
const result: ITreeShakingResult = {};
const writeFile = (filePath: string, contents: string): void => {
result[filePath] = contents;
};
@@ -700,7 +726,7 @@ function generateResult(ts: typeof import('typescript'), languageService: ts.Lan
return;
}
let text = sourceFile.text;
const text = sourceFile.text;
let result = '';
function keep(node: ts.Node): void {
@@ -734,7 +760,7 @@ function generateResult(ts: typeof import('typescript'), languageService: ts.Lan
return keep(node);
}
} else {
let survivingImports: string[] = [];
const survivingImports: string[] = [];
for (const importNode of node.importClause.namedBindings.elements) {
if (getColor(importNode) === NodeColor.Black) {
survivingImports.push(importNode.getFullText(sourceFile));
@@ -762,7 +788,7 @@ function generateResult(ts: typeof import('typescript'), languageService: ts.Lan
if (ts.isExportDeclaration(node)) {
if (node.exportClause && node.moduleSpecifier && ts.isNamedExports(node.exportClause)) {
let survivingExports: string[] = [];
const survivingExports: string[] = [];
for (const exportSpecifier of node.exportClause.elements) {
if (getColor(exportSpecifier) === NodeColor.Black) {
survivingExports.push(exportSpecifier.getFullText(sourceFile));
@@ -785,8 +811,8 @@ function generateResult(ts: typeof import('typescript'), languageService: ts.Lan
continue;
}
let pos = member.pos - node.pos;
let end = member.end - node.pos;
const pos = member.pos - node.pos;
const end = member.end - node.pos;
toWrite = toWrite.substring(0, pos) + toWrite.substring(end);
}
return write(toWrite);
@@ -802,11 +828,21 @@ function generateResult(ts: typeof import('typescript'), languageService: ts.Lan
if (getColor(sourceFile) !== NodeColor.Black) {
if (!nodeOrChildIsBlack(sourceFile)) {
// none of the elements are reachable => don't write this file at all!
return;
// none of the elements are reachable
if (isNeededSourceFile(sourceFile)) {
// this source file must be written, even if nothing is used from it
// because there is an import somewhere for it.
// However, TS complains with empty files with the error "x" is not a module,
// so we will export a dummy variable
result = 'export const __dummy = 0;';
} else {
// don't write this file at all!
return;
}
} else {
sourceFile.forEachChild(writeMarkedNodes);
result += sourceFile.endOfFileToken.getFullText(sourceFile);
}
sourceFile.forEachChild(writeMarkedNodes);
result += sourceFile.endOfFileToken.getFullText(sourceFile);
} else {
result = text;
}