api.js 18.1 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
var ts = require("typescript");
var path = require("path");
var tsfmt = require('../../tsfmt.json');
var util = require('gulp-util');
function log(message) {
    var rest = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        rest[_i - 1] = arguments[_i];
    }
    util.log.apply(util, [util.colors.cyan('[monaco.d.ts]'), message].concat(rest));
}
var SRC = path.join(__dirname, '../../src');
var OUT_ROOT = path.join(__dirname, '../../');
var RECIPE_PATH = path.join(__dirname, './monaco.d.ts.recipe');
var DECLARATION_PATH = path.join(__dirname, '../../src/vs/monaco.d.ts');
var CURRENT_PROCESSING_RULE = '';
function logErr(message) {
    var rest = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        rest[_i - 1] = arguments[_i];
    }
    util.log(util.colors.red('[monaco.d.ts]'), 'WHILE HANDLING RULE: ', CURRENT_PROCESSING_RULE);
    util.log.apply(util, [util.colors.red('[monaco.d.ts]'), message].concat(rest));
}
function moduleIdToPath(out, moduleId) {
    if (/\.d\.ts/.test(moduleId)) {
        return path.join(SRC, moduleId);
    }
    return path.join(OUT_ROOT, out, moduleId) + '.d.ts';
}
var SOURCE_FILE_MAP = {};
function getSourceFile(out, inputFiles, moduleId) {
    if (!SOURCE_FILE_MAP[moduleId]) {
        var filePath = path.normalize(moduleIdToPath(out, moduleId));
        if (!inputFiles.hasOwnProperty(filePath)) {
            logErr('CANNOT FIND FILE ' + filePath + '. YOU MIGHT NEED TO RESTART gulp');
            return null;
        }
        var fileContents = inputFiles[filePath];
        var sourceFile = ts.createSourceFile(filePath, fileContents, ts.ScriptTarget.ES5);
        SOURCE_FILE_MAP[moduleId] = sourceFile;
    }
    return SOURCE_FILE_MAP[moduleId];
}
function isDeclaration(a) {
    return (a.kind === ts.SyntaxKind.InterfaceDeclaration
        || a.kind === ts.SyntaxKind.EnumDeclaration
        || a.kind === ts.SyntaxKind.ClassDeclaration
        || a.kind === ts.SyntaxKind.TypeAliasDeclaration
        || a.kind === ts.SyntaxKind.FunctionDeclaration
        || a.kind === ts.SyntaxKind.ModuleDeclaration);
}
function visitTopLevelDeclarations(sourceFile, visitor) {
    var stop = false;
    var visit = function (node) {
        if (stop) {
            return;
        }
        switch (node.kind) {
            case ts.SyntaxKind.InterfaceDeclaration:
            case ts.SyntaxKind.EnumDeclaration:
            case ts.SyntaxKind.ClassDeclaration:
            case ts.SyntaxKind.VariableStatement:
            case ts.SyntaxKind.TypeAliasDeclaration:
            case ts.SyntaxKind.FunctionDeclaration:
            case ts.SyntaxKind.ModuleDeclaration:
                stop = visitor(node);
        }
        // if (node.kind !== ts.SyntaxKind.SourceFile) {
        // 	if (getNodeText(sourceFile, node).indexOf('SymbolKind') >= 0) {
        // 		console.log('FOUND TEXT IN NODE: ' + ts.SyntaxKind[node.kind]);
        // 		console.log(getNodeText(sourceFile, node));
        // 	}
        // }
        if (stop) {
            return;
        }
        ts.forEachChild(node, visit);
    };
    visit(sourceFile);
}
function getAllTopLevelDeclarations(sourceFile) {
    var all = [];
    visitTopLevelDeclarations(sourceFile, function (node) {
        if (node.kind === ts.SyntaxKind.InterfaceDeclaration || node.kind === ts.SyntaxKind.ClassDeclaration || node.kind === ts.SyntaxKind.ModuleDeclaration) {
            var interfaceDeclaration = node;
            var triviaStart = interfaceDeclaration.pos;
            var triviaEnd = interfaceDeclaration.name.pos;
            var triviaText = getNodeText(sourceFile, { pos: triviaStart, end: triviaEnd });
            // // let nodeText = getNodeText(sourceFile, node);
            // if (getNodeText(sourceFile, node).indexOf('SymbolKind') >= 0) {
            // 	console.log('TRIVIA: ', triviaText);
            // }
            if (triviaText.indexOf('@internal') === -1) {
                all.push(node);
            }
        }
        else {
            var nodeText = getNodeText(sourceFile, node);
            if (nodeText.indexOf('@internal') === -1) {
                all.push(node);
            }
        }
        return false /*continue*/;
    });
    return all;
}
function getTopLevelDeclaration(sourceFile, typeName) {
    var result = null;
    visitTopLevelDeclarations(sourceFile, function (node) {
        if (isDeclaration(node)) {
            if (node.name.text === typeName) {
                result = node;
                return true /*stop*/;
            }
            return false /*continue*/;
        }
        // node is ts.VariableStatement
        if (getNodeText(sourceFile, node).indexOf(typeName) >= 0) {
            result = node;
            return true /*stop*/;
        }
        return false /*continue*/;
    });
    return result;
}
function getNodeText(sourceFile, node) {
    return sourceFile.getFullText().substring(node.pos, node.end);
}
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
function hasModifier(modifiers, kind) {
    if (modifiers) {
        for (var i = 0; i < modifiers.length; i++) {
            var mod = modifiers[i];
            if (mod.kind === kind) {
                return true;
            }
        }
    }
    return false;
}
function isStatic(member) {
    return hasModifier(member.modifiers, ts.SyntaxKind.StaticKeyword);
}
function isDefaultExport(declaration) {
    return (hasModifier(declaration.modifiers, ts.SyntaxKind.DefaultKeyword)
        && hasModifier(declaration.modifiers, ts.SyntaxKind.ExportKeyword));
}
function getMassagedTopLevelDeclarationText(sourceFile, declaration, importName, usage) {
J
Joao Moreno 已提交
156 157 158 159 160 161 162
    var result = getNodeText(sourceFile, declaration);
    // if (result.indexOf('MonacoWorker') >= 0) {
    // 	console.log('here!');
    // 	// console.log(ts.SyntaxKind[declaration.kind]);
    // }
    if (declaration.kind === ts.SyntaxKind.InterfaceDeclaration || declaration.kind === ts.SyntaxKind.ClassDeclaration) {
        var interfaceDeclaration = declaration;
163 164 165 166 167 168 169 170 171 172 173 174
        var staticTypeName_1 = (isDefaultExport(interfaceDeclaration)
            ? importName + ".default"
            : importName + "." + declaration.name.text);
        var instanceTypeName_1 = staticTypeName_1;
        var typeParametersCnt = (interfaceDeclaration.typeParameters ? interfaceDeclaration.typeParameters.length : 0);
        if (typeParametersCnt > 0) {
            var arr = [];
            for (var i = 0; i < typeParametersCnt; i++) {
                arr.push('any');
            }
            instanceTypeName_1 = instanceTypeName_1 + "<" + arr.join(',') + ">";
        }
J
Joao Moreno 已提交
175 176 177 178 179 180 181 182 183
        var members = interfaceDeclaration.members;
        members.forEach(function (member) {
            try {
                var memberText = getNodeText(sourceFile, member);
                if (memberText.indexOf('@internal') >= 0 || memberText.indexOf('private') >= 0) {
                    // console.log('BEFORE: ', result);
                    result = result.replace(memberText, '');
                    // console.log('AFTER: ', result);
                }
184 185 186 187 188 189 190 191 192
                else {
                    var memberName = member.name.text;
                    if (isStatic(member)) {
                        usage.push("a = " + staticTypeName_1 + "." + memberName + ";");
                    }
                    else {
                        usage.push("a = (<" + instanceTypeName_1 + ">b)." + memberName + ";");
                    }
                }
J
Joao Moreno 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
            }
            catch (err) {
                // life..
            }
        });
    }
    result = result.replace(/export default/g, 'export');
    result = result.replace(/export declare/g, 'export');
    return result;
}
function format(text) {
    // Parse the source text
    var sourceFile = ts.createSourceFile('file.ts', text, ts.ScriptTarget.Latest, /*setParentPointers*/ true);
    // Get the formatting edits on the input sources
    var edits = ts.formatting.formatDocument(sourceFile, getRuleProvider(tsfmt), tsfmt);
    // Apply the edits on the input code
    return applyEdits(text, edits);
    function getRuleProvider(options) {
        // Share this between multiple formatters using the same options.
        // This represents the bulk of the space the formatter uses.
M
Matt Bierner 已提交
213
        return ts.formatting.getFormatContext(options);
J
Joao Moreno 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    }
    function applyEdits(text, edits) {
        // Apply edits in reverse on the existing text
        var result = text;
        for (var i = edits.length - 1; i >= 0; i--) {
            var change = edits[i];
            var head = result.slice(0, change.span.start);
            var tail = result.slice(change.span.start + change.span.length);
            result = head + change.newText + tail;
        }
        return result;
    }
}
function createReplacer(data) {
    data = data || '';
    var rawDirectives = data.split(';');
    var directives = [];
    rawDirectives.forEach(function (rawDirective) {
        if (rawDirective.length === 0) {
            return;
        }
        var pieces = rawDirective.split('=>');
        var findStr = pieces[0];
        var replaceStr = pieces[1];
        findStr = findStr.replace(/[\-\\\{\}\*\+\?\|\^\$\.\,\[\]\(\)\#\s]/g, '\\$&');
        findStr = '\\b' + findStr + '\\b';
        directives.push([new RegExp(findStr, 'g'), replaceStr]);
    });
    return function (str) {
        for (var i = 0; i < directives.length; i++) {
            str = str.replace(directives[i][0], directives[i][1]);
        }
        return str;
    };
}
function generateDeclarationFile(out, inputFiles, recipe) {
J
Joao Moreno 已提交
250 251
    var endl = /\r\n/.test(recipe) ? '\r\n' : '\n';
    var lines = recipe.split(endl);
J
Joao Moreno 已提交
252
    var result = [];
253 254 255 256 257 258 259 260 261 262
    var usageCounter = 0;
    var usageImports = [];
    var usage = [];
    usage.push("var a;");
    usage.push("var b;");
    var generateUsageImport = function (moduleId) {
        var importName = 'm' + (++usageCounter);
        usageImports.push("import * as " + importName + " from '" + moduleId.replace(/\.d\.ts$/, '') + "';");
        return importName;
    };
J
Joao Moreno 已提交
263 264 265 266 267 268 269 270 271
    lines.forEach(function (line) {
        var m1 = line.match(/^\s*#include\(([^;)]*)(;[^)]*)?\)\:(.*)$/);
        if (m1) {
            CURRENT_PROCESSING_RULE = line;
            var moduleId = m1[1];
            var sourceFile_1 = getSourceFile(out, inputFiles, moduleId);
            if (!sourceFile_1) {
                return;
            }
272
            var importName_1 = generateUsageImport(moduleId);
J
Joao Moreno 已提交
273 274 275 276 277 278 279 280 281 282 283 284
            var replacer_1 = createReplacer(m1[2]);
            var typeNames = m1[3].split(/,/);
            typeNames.forEach(function (typeName) {
                typeName = typeName.trim();
                if (typeName.length === 0) {
                    return;
                }
                var declaration = getTopLevelDeclaration(sourceFile_1, typeName);
                if (!declaration) {
                    logErr('Cannot find type ' + typeName);
                    return;
                }
285
                result.push(replacer_1(getMassagedTopLevelDeclarationText(sourceFile_1, declaration, importName_1, usage)));
J
Joao Moreno 已提交
286 287 288 289 290 291 292 293 294 295 296
            });
            return;
        }
        var m2 = line.match(/^\s*#includeAll\(([^;)]*)(;[^)]*)?\)\:(.*)$/);
        if (m2) {
            CURRENT_PROCESSING_RULE = line;
            var moduleId = m2[1];
            var sourceFile_2 = getSourceFile(out, inputFiles, moduleId);
            if (!sourceFile_2) {
                return;
            }
297
            var importName_2 = generateUsageImport(moduleId);
J
Joao Moreno 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
            var replacer_2 = createReplacer(m2[2]);
            var typeNames = m2[3].split(/,/);
            var typesToExcludeMap_1 = {};
            var typesToExcludeArr_1 = [];
            typeNames.forEach(function (typeName) {
                typeName = typeName.trim();
                if (typeName.length === 0) {
                    return;
                }
                typesToExcludeMap_1[typeName] = true;
                typesToExcludeArr_1.push(typeName);
            });
            getAllTopLevelDeclarations(sourceFile_2).forEach(function (declaration) {
                if (isDeclaration(declaration)) {
                    if (typesToExcludeMap_1[declaration.name.text]) {
                        return;
                    }
                }
                else {
                    // node is ts.VariableStatement
                    var nodeText = getNodeText(sourceFile_2, declaration);
                    for (var i = 0; i < typesToExcludeArr_1.length; i++) {
                        if (nodeText.indexOf(typesToExcludeArr_1[i]) >= 0) {
                            return;
                        }
                    }
                }
325
                result.push(replacer_2(getMassagedTopLevelDeclarationText(sourceFile_2, declaration, importName_2, usage)));
J
Joao Moreno 已提交
326 327 328 329 330
            });
            return;
        }
        result.push(line);
    });
J
Joao Moreno 已提交
331
    var resultTxt = result.join(endl);
J
Joao Moreno 已提交
332 333 334 335
    resultTxt = resultTxt.replace(/\bURI\b/g, 'Uri');
    resultTxt = resultTxt.replace(/\bEvent</g, 'IEvent<');
    resultTxt = resultTxt.replace(/\bTPromise</g, 'Promise<');
    resultTxt = format(resultTxt);
336 337 338 339
    return [
        resultTxt,
        usageImports.join('\n') + "\n\n" + usage.join('\n')
    ];
J
Joao Moreno 已提交
340
}
341
function getIncludesInRecipe() {
J
Joao Moreno 已提交
342 343 344 345 346 347 348
    var recipe = fs.readFileSync(RECIPE_PATH).toString();
    var lines = recipe.split(/\r\n|\n|\r/);
    var result = [];
    lines.forEach(function (line) {
        var m1 = line.match(/^\s*#include\(([^;)]*)(;[^)]*)?\)\:(.*)$/);
        if (m1) {
            var moduleId = m1[1];
349
            result.push(moduleId);
J
Joao Moreno 已提交
350 351 352 353 354
            return;
        }
        var m2 = line.match(/^\s*#includeAll\(([^;)]*)(;[^)]*)?\)\:(.*)$/);
        if (m2) {
            var moduleId = m2[1];
355
            result.push(moduleId);
J
Joao Moreno 已提交
356 357 358 359 360
            return;
        }
    });
    return result;
}
361 362 363
function getFilesToWatch(out) {
    return getIncludesInRecipe().map(function (moduleId) { return moduleIdToPath(out, moduleId); });
}
J
Joao Moreno 已提交
364 365 366 367 368
exports.getFilesToWatch = getFilesToWatch;
function run(out, inputFiles) {
    log('Starting monaco.d.ts generation');
    SOURCE_FILE_MAP = {};
    var recipe = fs.readFileSync(RECIPE_PATH).toString();
369
    var _a = generateDeclarationFile(out, inputFiles, recipe), result = _a[0], usageContent = _a[1];
J
Joao Moreno 已提交
370 371
    var currentContent = fs.readFileSync(DECLARATION_PATH).toString();
    log('Finished monaco.d.ts generation');
J
Joao Moreno 已提交
372 373 374
    var one = currentContent.replace(/\r\n/gm, '\n');
    var other = result.replace(/\r\n/gm, '\n');
    var isTheSame = one === other;
J
Joao Moreno 已提交
375 376
    return {
        content: result,
377
        usageContent: usageContent,
J
Joao Moreno 已提交
378
        filePath: DECLARATION_PATH,
J
Joao Moreno 已提交
379
        isTheSame: isTheSame
J
Joao Moreno 已提交
380 381 382 383 384 385 386
    };
}
exports.run = run;
function complainErrors() {
    logErr('Not running monaco.d.ts generation due to compile errors');
}
exports.complainErrors = complainErrors;
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
var TypeScriptLanguageServiceHost = /** @class */ (function () {
    function TypeScriptLanguageServiceHost(libs, files, compilerOptions) {
        this._libs = libs;
        this._files = files;
        this._compilerOptions = compilerOptions;
    }
    // --- language service host ---------------
    TypeScriptLanguageServiceHost.prototype.getCompilationSettings = function () {
        return this._compilerOptions;
    };
    TypeScriptLanguageServiceHost.prototype.getScriptFileNames = function () {
        return ([]
            .concat(Object.keys(this._libs))
            .concat(Object.keys(this._files)));
    };
    TypeScriptLanguageServiceHost.prototype.getScriptVersion = function (fileName) {
        return '1';
    };
    TypeScriptLanguageServiceHost.prototype.getProjectVersion = function () {
        return '1';
    };
    TypeScriptLanguageServiceHost.prototype.getScriptSnapshot = function (fileName) {
        if (this._files.hasOwnProperty(fileName)) {
            return ts.ScriptSnapshot.fromString(this._files[fileName]);
        }
        else if (this._libs.hasOwnProperty(fileName)) {
            return ts.ScriptSnapshot.fromString(this._libs[fileName]);
        }
        else {
            return ts.ScriptSnapshot.fromString('');
        }
    };
    TypeScriptLanguageServiceHost.prototype.getScriptKind = function (fileName) {
        return ts.ScriptKind.TS;
    };
    TypeScriptLanguageServiceHost.prototype.getCurrentDirectory = function () {
        return '';
    };
    TypeScriptLanguageServiceHost.prototype.getDefaultLibFileName = function (options) {
        return 'defaultLib:es5';
    };
    TypeScriptLanguageServiceHost.prototype.isDefaultLibFileName = function (fileName) {
        return fileName === this.getDefaultLibFileName(this._compilerOptions);
    };
    return TypeScriptLanguageServiceHost;
}());
function execute() {
    var OUTPUT_FILES = {};
    var SRC_FILES = {};
    var SRC_FILE_TO_EXPECTED_NAME = {};
    getIncludesInRecipe().forEach(function (moduleId) {
        if (/\.d\.ts$/.test(moduleId)) {
            var fileName_1 = path.join(SRC, moduleId);
            OUTPUT_FILES[moduleIdToPath('src', moduleId)] = fs.readFileSync(fileName_1).toString();
            return;
        }
        var fileName = path.join(SRC, moduleId) + '.ts';
        SRC_FILES[fileName] = fs.readFileSync(fileName).toString();
        SRC_FILE_TO_EXPECTED_NAME[fileName] = moduleIdToPath('src', moduleId);
    });
    var languageService = ts.createLanguageService(new TypeScriptLanguageServiceHost({}, SRC_FILES, {}));
    var t1 = Date.now();
    Object.keys(SRC_FILES).forEach(function (fileName) {
        var t = Date.now();
        var emitOutput = languageService.getEmitOutput(fileName, true);
        OUTPUT_FILES[SRC_FILE_TO_EXPECTED_NAME[fileName]] = emitOutput.outputFiles[0].text;
A
Alex Dima 已提交
453
        // console.log(`Generating .d.ts for ${fileName} took ${Date.now() - t} ms`);
454 455
    });
    console.log("Generating .d.ts took " + (Date.now() - t1) + " ms");
A
Alex Dima 已提交
456 457 458 459
    // console.log(result.filePath);
    // fs.writeFileSync(result.filePath, result.content.replace(/\r\n/gm, '\n'));
    // fs.writeFileSync(path.join(SRC, 'user.ts'), result.usageContent.replace(/\r\n/gm, '\n'));
    return run('src', OUTPUT_FILES);
460
}
A
Alex Dima 已提交
461
exports.execute = execute;