importTypescript.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import path = require('path');
  6. import fs = require('fs');
  7. import child_process = require('child_process');
  8. import { REPO_ROOT } from './utils';
  9. const generatedNote = `//
  10. // **NOTE**: Do not edit directly! This file is generated using \`npm run import-typescript\`
  11. //
  12. `;
  13. const TYPESCRIPT_LIB_SOURCE = path.join(REPO_ROOT, 'node_modules/typescript/lib');
  14. const TYPESCRIPT_LIB_DESTINATION = path.join(REPO_ROOT, 'src/language/typescript/lib');
  15. (function () {
  16. try {
  17. fs.statSync(TYPESCRIPT_LIB_DESTINATION);
  18. } catch (err) {
  19. fs.mkdirSync(TYPESCRIPT_LIB_DESTINATION);
  20. }
  21. importLibs();
  22. const npmLsOutput = JSON.parse(
  23. child_process.execSync('npm ls typescript --depth=0 --json=true', { cwd: REPO_ROOT }).toString()
  24. );
  25. const typeScriptDependencyVersion = npmLsOutput.dependencies.typescript.version;
  26. fs.writeFileSync(
  27. path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServicesMetadata.ts'),
  28. `${generatedNote}
  29. export const typescriptVersion = "${typeScriptDependencyVersion}";\n`
  30. );
  31. let tsServices = fs.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescript.js')).toString();
  32. tsServices = tsServices
  33. .replace(
  34. 'const path = matchedStar ? subst.replace("*", matchedStar) : subst;',
  35. 'const path = matchedStar ? subst.replace("*", matchedStar) : subst; // CodeQL [SM02383] This is a false positive, the code is from the TypeScript compiler'
  36. )
  37. .replace(
  38. 'return key.replace("*", matchedStar);',
  39. 'return key.replace("*", matchedStar); // CodeQL [SM02383] This is a false positive, the code is from the TypeScript compiler'
  40. );
  41. // The output from this build will only be accessible via ESM; rather than removing
  42. // references to require/module, define them as dummy variables that bundlers will ignore.
  43. // The TS code can figure out that it's not running under Node even with these defined.
  44. tsServices =
  45. `
  46. /* MONACOCHANGE */
  47. var require = undefined;
  48. var module = { exports: {} };
  49. /* END MONACOCHANGE */
  50. ` + tsServices;
  51. const tsServices_esm =
  52. generatedNote +
  53. tsServices +
  54. `
  55. // MONACOCHANGE
  56. export var createClassifier = ts.createClassifier;
  57. export var createLanguageService = ts.createLanguageService;
  58. export var displayPartsToString = ts.displayPartsToString;
  59. export var EndOfLineState = ts.EndOfLineState;
  60. export var flattenDiagnosticMessageText = ts.flattenDiagnosticMessageText;
  61. export var IndentStyle = ts.IndentStyle;
  62. export var ScriptKind = ts.ScriptKind;
  63. export var ScriptTarget = ts.ScriptTarget;
  64. export var TokenClass = ts.TokenClass;
  65. export var typescript = ts;
  66. // END MONACOCHANGE
  67. `;
  68. fs.writeFileSync(
  69. path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.js'),
  70. stripSourceMaps(tsServices_esm)
  71. );
  72. let dtsServices = fs.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescript.d.ts')).toString();
  73. fs.writeFileSync(
  74. path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.d.ts'),
  75. generatedNote + dtsServices
  76. );
  77. })();
  78. function importLibs() {
  79. function readLibFile(name) {
  80. const srcPath = path.join(TYPESCRIPT_LIB_SOURCE, name);
  81. return fs.readFileSync(srcPath).toString();
  82. }
  83. let strLibResult = `/*---------------------------------------------------------------------------------------------
  84. * Copyright (c) Microsoft Corporation. All rights reserved.
  85. * Licensed under the MIT License. See License.txt in the project root for license information.
  86. *--------------------------------------------------------------------------------------------*/
  87. ${generatedNote}
  88. /** Contains all the lib files */
  89. export const libFileMap: Record<string, string> = {}
  90. `;
  91. let strIndexResult = `/*---------------------------------------------------------------------------------------------
  92. * Copyright (c) Microsoft Corporation. All rights reserved.
  93. * Licensed under the MIT License. See License.txt in the project root for license information.
  94. *--------------------------------------------------------------------------------------------*/
  95. ${generatedNote}
  96. /** Contains all the lib files */
  97. export const libFileSet: Record<string, boolean> = {}
  98. `;
  99. const dtsFiles = fs.readdirSync(TYPESCRIPT_LIB_SOURCE).filter((f) => f.includes('lib.'));
  100. while (dtsFiles.length > 0) {
  101. const name = dtsFiles.shift();
  102. const output = readLibFile(name).replace(/\r\n/g, '\n');
  103. strLibResult += `libFileMap['${name}'] = "${escapeText(output)}";\n`;
  104. strIndexResult += `libFileSet['${name}'] = true;\n`;
  105. }
  106. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'lib.ts'), strLibResult);
  107. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'lib.index.ts'), strIndexResult);
  108. }
  109. /**
  110. * Escape text such that it can be used in a javascript string enclosed by double quotes (")
  111. */
  112. function escapeText(text) {
  113. // See http://www.javascriptkit.com/jsref/escapesequence.shtml
  114. const _backspace = '\b'.charCodeAt(0);
  115. const _formFeed = '\f'.charCodeAt(0);
  116. const _newLine = '\n'.charCodeAt(0);
  117. const _nullChar = 0;
  118. const _carriageReturn = '\r'.charCodeAt(0);
  119. const _tab = '\t'.charCodeAt(0);
  120. const _verticalTab = '\v'.charCodeAt(0);
  121. const _backslash = '\\'.charCodeAt(0);
  122. const _doubleQuote = '"'.charCodeAt(0);
  123. const len = text.length;
  124. let startPos = 0;
  125. let chrCode;
  126. let replaceWith = null;
  127. let resultPieces = [];
  128. for (let i = 0; i < len; i++) {
  129. chrCode = text.charCodeAt(i);
  130. switch (chrCode) {
  131. case _backspace:
  132. replaceWith = '\\b';
  133. break;
  134. case _formFeed:
  135. replaceWith = '\\f';
  136. break;
  137. case _newLine:
  138. replaceWith = '\\n';
  139. break;
  140. case _nullChar:
  141. replaceWith = '\\0';
  142. break;
  143. case _carriageReturn:
  144. replaceWith = '\\r';
  145. break;
  146. case _tab:
  147. replaceWith = '\\t';
  148. break;
  149. case _verticalTab:
  150. replaceWith = '\\v';
  151. break;
  152. case _backslash:
  153. replaceWith = '\\\\';
  154. break;
  155. case _doubleQuote:
  156. replaceWith = '\\"';
  157. break;
  158. }
  159. if (replaceWith !== null) {
  160. resultPieces.push(text.substring(startPos, i));
  161. resultPieces.push(replaceWith);
  162. startPos = i + 1;
  163. replaceWith = null;
  164. }
  165. }
  166. resultPieces.push(text.substring(startPos, len));
  167. return resultPieces.join('');
  168. }
  169. function stripSourceMaps(str) {
  170. return str.replace(/\/\/# sourceMappingURL[^\n]+/gm, '');
  171. }