Remove typescriptServices, protocol.d.ts, typescript_standalone.d.ts

This is the same as TypeScript PR 51026.
This commit is contained in:
Jake Bailey
2022-09-13 13:25:45 -07:00
parent 01e5200096
commit 6dfa4299d7
6 changed files with 3 additions and 334 deletions

View File

@@ -153,20 +153,12 @@ const buildServices = (() => {
.pipe(rename("typescript.d.ts"))
.pipe(dest("built/local"));
// create typescript_standalone.d.ts
const createTypescriptStandaloneDts = () => src("built/local/typescriptServices.d.ts")
.pipe(newer("built/local/typescript_standalone.d.ts"))
.pipe(transform(content => content.replace(/declare (namespace|module) ts/g, 'declare module "typescript"')))
.pipe(rename("typescript_standalone.d.ts"))
.pipe(dest("built/local"));
return series(
buildTypescriptServicesOut,
createTypescriptServicesJs,
createTypescriptServicesDts,
createTypescriptJs,
createTypescriptDts,
createTypescriptStandaloneDts,
);
})();
task("services", series(preBuild, buildServices));
@@ -186,7 +178,6 @@ const cleanServices = async () => {
"built/local/typescriptServices.js",
"built/local/typescript.js",
"built/local/typescript.d.ts",
"built/local/typescript_standalone.d.ts"
]);
};
cleanTasks.push(cleanServices);

View File

@@ -1,260 +0,0 @@
import ts from "../lib/typescript.js";
import path from "path";
import assert from "assert";
/**
*
* @param {string} s
* @param {string} suffix
* @returns {boolean}
*/
function endsWith(s, suffix) {
return s.lastIndexOf(suffix, s.length - suffix.length) !== -1;
}
/**
* @param {ts.EnumDeclaration} declaration
* @returns {boolean}
*/
function isStringEnum(declaration) {
return !!declaration.members.length && declaration.members.every(m => !!m.initializer && m.initializer.kind === ts.SyntaxKind.StringLiteral);
}
class DeclarationsWalker {
/**
* @type {ts.Type[]}
* @private
*/
visitedTypes = [];
/**
* @type {string}
* @private
*/
text = "";
/**
* @type {ts.Type[]}
* @private
*/
removedTypes = [];
/**
* @param {ts.TypeChecker} typeChecker
* @param {ts.SourceFile} protocolFile
* @private
*/
constructor(typeChecker, protocolFile) {
this.typeChecker = typeChecker;
this.protocolFile = protocolFile;
}
/**
*
* @param {ts.TypeChecker} typeChecker
* @param {ts.SourceFile} protocolFile
* @returns {string}
*/
static getExtraDeclarations(typeChecker, protocolFile) {
const walker = new DeclarationsWalker(typeChecker, protocolFile);
walker.visitTypeNodes(protocolFile);
let text = walker.text
? `declare namespace ts.server.protocol {\n${walker.text}}`
: "";
if (walker.removedTypes) {
text += "\ndeclare namespace ts {\n";
text += " // these types are empty stubs for types from services and should not be used directly\n";
for (const type of walker.removedTypes) {
text += ` export type ${type.symbol.name} = never;\n`;
}
text += "}";
}
return text;
}
/**
* @param {ts.Type} type
* @returns {void}
* @private
*/
processType(type) {
if (this.visitedTypes.indexOf(type) >= 0) {
return;
}
this.visitedTypes.push(type);
const s = type.aliasSymbol || type.getSymbol();
if (!s) {
return;
}
if (s.name === "Array" || s.name === "ReadOnlyArray") {
// we should process type argument instead
return this.processType(/** @type {any} */(type).typeArguments[0]);
}
else {
const declarations = s.getDeclarations();
if (declarations) {
for (const decl of declarations) {
const sourceFile = decl.getSourceFile();
if (sourceFile === this.protocolFile || /lib(\..+)?\.d.ts/.test(path.basename(sourceFile.fileName))) {
return;
}
if (ts.isEnumDeclaration(decl) && !isStringEnum(decl)) {
this.removedTypes.push(type);
return;
}
else {
// splice declaration in final d.ts file
const text = decl.getFullText();
this.text += `${text}\n`;
// recursively pull all dependencies into result dts file
this.visitTypeNodes(decl);
}
}
}
}
}
/**
* @param {ts.Node} node
* @private
*/
visitTypeNodes(node) {
if (node.parent) {
switch (node.parent.kind) {
case ts.SyntaxKind.VariableDeclaration:
case ts.SyntaxKind.MethodDeclaration:
case ts.SyntaxKind.MethodSignature:
case ts.SyntaxKind.PropertyDeclaration:
case ts.SyntaxKind.PropertySignature:
case ts.SyntaxKind.Parameter:
case ts.SyntaxKind.IndexSignature:
const parent = /** @type {ts.VariableDeclaration | ts.MethodDeclaration | ts.PropertyDeclaration | ts.ParameterDeclaration | ts.PropertySignature | ts.MethodSignature | ts.IndexSignatureDeclaration} */ (node.parent);
if (parent.type === node) {
this.processTypeOfNode(node);
}
break;
case ts.SyntaxKind.InterfaceDeclaration:
const heritageClauses = /** @type {ts.InterfaceDeclaration} */ (node.parent).heritageClauses;
if (heritageClauses) {
if (heritageClauses[0].token !== ts.SyntaxKind.ExtendsKeyword) {
throw new Error(`Unexpected kind of heritage clause: ${ts.SyntaxKind[heritageClauses[0].kind]}`);
}
for (const type of heritageClauses[0].types) {
this.processTypeOfNode(type);
}
}
break;
}
}
ts.forEachChild(node, n => this.visitTypeNodes(n));
}
/**
* @param {ts.Node} node
* @private
*/
processTypeOfNode(node) {
if (node.kind === ts.SyntaxKind.UnionType) {
for (const t of /** @type {ts.UnionTypeNode} */ (node).types) {
this.processTypeOfNode(t);
}
}
else {
const type = this.typeChecker.getTypeAtLocation(node);
if (type && !(type.flags & (ts.TypeFlags.TypeParameter))) {
this.processType(type);
}
}
}
}
/**
* @param {string} outputFile
* @param {string} protocolTs
* @param {string} typeScriptServicesDts
*/
function writeProtocolFile(outputFile, protocolTs, typeScriptServicesDts) {
/** @type {ts.CompilerOptions} */
const options = { target: ts.ScriptTarget.ES5, declaration: true, noResolve: false, types: [], stripInternal: true };
/**
* 1st pass - generate a program from protocol.ts and typescriptservices.d.ts and emit core version of protocol.d.ts with all internal members stripped
* @return text of protocol.d.t.s
*/
function getInitialDtsFileForProtocol() {
const program = ts.createProgram([protocolTs, typeScriptServicesDts, path.join(typeScriptServicesDts, "../lib.es5.d.ts")], options);
/** @type {string | undefined} */
let protocolDts;
const emitResult = program.emit(program.getSourceFile(protocolTs), (file, content) => {
if (endsWith(file, ".d.ts")) {
protocolDts = content;
}
});
if (protocolDts === undefined) {
/** @type {ts.FormatDiagnosticsHost} */
const diagHost = {
getCanonicalFileName(f) { return f; },
getCurrentDirectory() { return "."; },
getNewLine() { return "\r\n"; }
};
const diags = emitResult.diagnostics.map(d => ts.formatDiagnostic(d, diagHost)).join("\r\n");
throw new Error(`Declaration file for protocol.ts is not generated:\r\n${diags}`);
}
return protocolDts;
}
const protocolFileName = "protocol.d.ts";
/**
* Second pass - generate a program from protocol.d.ts and typescriptservices.d.ts, then augment core protocol.d.ts with extra types from typescriptservices.d.ts
* @param {string} protocolDts
* @param {boolean} includeTypeScriptServices
*/
function getProgramWithProtocolText(protocolDts, includeTypeScriptServices) {
const host = ts.createCompilerHost(options);
const originalGetSourceFile = host.getSourceFile;
host.getSourceFile = (fileName) => {
if (fileName === protocolFileName) {
assert(options.target !== undefined);
return ts.createSourceFile(fileName, protocolDts, options.target);
}
return originalGetSourceFile.apply(host, [fileName, ts.ScriptTarget.Latest]);
};
const rootFiles = includeTypeScriptServices ? [protocolFileName, typeScriptServicesDts] : [protocolFileName];
return ts.createProgram(rootFiles, options, host);
}
let protocolDts = getInitialDtsFileForProtocol();
const program = getProgramWithProtocolText(protocolDts, /*includeTypeScriptServices*/ true);
const protocolFile = program.getSourceFile("protocol.d.ts");
assert(protocolFile);
const extraDeclarations = DeclarationsWalker.getExtraDeclarations(program.getTypeChecker(), protocolFile);
if (extraDeclarations) {
protocolDts += extraDeclarations;
}
protocolDts += "\nimport protocol = ts.server.protocol;";
protocolDts += "\nexport = protocol;";
protocolDts += "\nexport as namespace protocol;";
// do sanity check and try to compile generated text as standalone program
const sanityCheckProgram = getProgramWithProtocolText(protocolDts, /*includeTypeScriptServices*/ false);
const diagnostics = [...sanityCheckProgram.getSyntacticDiagnostics(), ...sanityCheckProgram.getSemanticDiagnostics(), ...sanityCheckProgram.getGlobalDiagnostics()];
ts.sys.writeFile(outputFile, protocolDts);
if (diagnostics.length) {
const flattenedDiagnostics = diagnostics.map(d => `${ts.flattenDiagnosticMessageText(d.messageText, "\n")} at ${d.file ? d.file.fileName : "<unknown>"} line ${d.start}`).join("\n");
throw new Error(`Unexpected errors during sanity check: ${flattenedDiagnostics}`);
}
}
if (process.argv.length < 5) {
console.log(`Expected 3 arguments: path to 'protocol.ts', path to 'typescriptservices.d.ts' and path to output file`);
process.exit(1);
}
const protocolTs = process.argv[2];
const typeScriptServicesDts = process.argv[3];
const outputFile = process.argv[4];
writeProtocolFile(outputFile, protocolTs, typeScriptServicesDts);

View File

@@ -1,8 +1,8 @@
import childProcess from "child_process";
import fs from "fs-extra";
import path from "path";
import glob from "glob";
import url from "url";
import del from "del";
const __filename = url.fileURLToPath(new URL(import.meta.url));
const __dirname = path.dirname(__filename);
@@ -14,12 +14,13 @@ const copyright = fs.readFileSync(path.join(__dirname, "../CopyrightNotice.txt")
async function produceLKG() {
console.log(`Building LKG from ${source} to ${dest}`);
await del(`${dest.replace(/\\/g, "/")}/**`, { ignore: ["**/README.md"] });
await fs.mkdirp(dest);
await copyLibFiles();
await copyLocalizedDiagnostics();
await copyTypesMap();
await copyScriptOutputs();
await copyDeclarationOutputs();
await buildProtocol();
await writeGitAttributes();
}
@@ -46,20 +47,6 @@ async function copyTypesMap() {
await copyFromBuiltLocal("typesMap.json"); // Cannot accommodate copyright header
}
async function buildProtocol() {
const protocolScript = path.join(__dirname, "buildProtocol.mjs");
if (!fs.existsSync(protocolScript)) {
throw new Error(`Expected protocol script ${protocolScript} to exist`);
}
const protocolInput = path.join(__dirname, "../src/server/protocol.ts");
const protocolServices = path.join(source, "typescriptServices.d.ts");
const protocolOutput = path.join(dest, "protocol.d.ts");
console.log(`Building ${protocolOutput}...`);
await exec(protocolScript, [protocolInput, protocolServices, protocolOutput]);
}
async function copyScriptOutputs() {
await copyWithCopyright("cancellationToken.js");
await copyWithCopyright("tsc.release.js", "tsc.js");
@@ -109,16 +96,6 @@ async function copyFilesWithGlob(pattern) {
console.log(`Copied ${files.length} files matching pattern ${pattern}`);
}
/**
* @param {string} path
* @param {string[]} args
*/
async function exec(path, args = []) {
const cmdLine = ["node", path, ...args].join(" ");
console.log(cmdLine);
childProcess.execSync(cmdLine);
}
process.on("unhandledRejection", err => {
throw err;
});

View File

@@ -1,7 +0,0 @@
/* Generated file to emulate the ts namespace. */
export * from "../../compiler/_namespaces/ts";
export * from "../../jsTyping/_namespaces/ts";
export * from "../../services/_namespaces/ts";
export * from "../../deprecatedCompat/_namespaces/ts";
export * from "../typescriptServices";

View File

@@ -1,16 +0,0 @@
{
"extends": "../tsconfig-library-base",
"compilerOptions": {
"outDir": "../../built/local"
},
"files": [
"typescriptServices.ts",
"_namespaces/ts.ts"
],
"references": [
{ "path": "../compiler" },
{ "path": "../jsTyping" },
{ "path": "../services" },
{ "path": "../deprecatedCompat" }
]
}

View File

@@ -1,16 +0,0 @@
import { Debug, LogLevel } from "./_namespaces/ts";
// enable deprecation logging
declare const console: any;
if (typeof console !== "undefined") {
Debug.loggingHost = {
log(level, s) {
switch (level) {
case LogLevel.Error: return console.error(s);
case LogLevel.Warning: return console.warn(s);
case LogLevel.Info: return console.log(s);
case LogLevel.Verbose: return console.log(s);
}
}
};
}