@volar/typescript
Advanced tools
Comparing version 2.4.0-alpha.18 to 2.4.0-alpha.19
@@ -11,1 +11,12 @@ import type * as ts from 'typescript'; | ||
}, _getLanguagePlugins: typeof getLanguagePlugins): void; | ||
/** | ||
* Replaces the code of typescript to add support for additional extensions and language plugins. | ||
* | ||
* @param tsc - The original code of typescript. | ||
* @param proxyApiPath - The path to the proxy API. | ||
* @param extraSupportedExtensions - An array of additional supported extensions. | ||
* @param extraExtensionsToRemove - An array of extensions to remove. | ||
* @param getLanguagePluginsFile - The file to get language plugins from. | ||
* @returns The modified typescript code. | ||
*/ | ||
export declare function transformTscContent(tsc: string, proxyApiPath: string, extraSupportedExtensions: string[], extraExtensionsToRemove: string[], getLanguagePluginsFile?: string): string; |
@@ -5,2 +5,3 @@ "use strict"; | ||
exports.runTsc = runTsc; | ||
exports.transformTscContent = transformTscContent; | ||
const fs = require("fs"); | ||
@@ -26,34 +27,3 @@ let getLanguagePlugins = () => []; | ||
} | ||
const needPatchExtenstions = extraSupportedExtensions.filter(ext => !extraExtensionsToRemove.includes(ext)); | ||
// Add allow extensions | ||
if (extraSupportedExtensions.length) { | ||
const extsText = extraSupportedExtensions.map(ext => `"${ext}"`).join(', '); | ||
tsc = replace(tsc, /supportedTSExtensions = .*(?=;)/, s => s + `.map((group, i) => i === 0 ? group.splice(0, 0, ${extsText}) && group : group)`); | ||
tsc = replace(tsc, /supportedJSExtensions = .*(?=;)/, s => s + `.map((group, i) => i === 0 ? group.splice(0, 0, ${extsText}) && group : group)`); | ||
tsc = replace(tsc, /allSupportedExtensions = .*(?=;)/, s => s + `.map((group, i) => i === 0 ? group.splice(0, 0, ${extsText}) && group : group)`); | ||
} | ||
// Use to emit basename.xxx to basename.d.ts instead of basename.xxx.d.ts | ||
if (extraExtensionsToRemove.length) { | ||
const extsText = extraExtensionsToRemove.map(ext => `"${ext}"`).join(', '); | ||
tsc = replace(tsc, /extensionsToRemove = .*(?=;)/, s => s + `.concat([${extsText}])`); | ||
} | ||
// Support for basename.xxx to basename.xxx.d.ts | ||
if (needPatchExtenstions.length) { | ||
const extsText = needPatchExtenstions.map(ext => `"${ext}"`).join(', '); | ||
tsc = replace(tsc, /function changeExtension\(/, s => `function changeExtension(path, newExtension) { | ||
return [${extsText}].some(ext => path.endsWith(ext)) | ||
? path + newExtension | ||
: _changeExtension(path, newExtension) | ||
}\n` + s.replace('changeExtension', '_changeExtension')); | ||
} | ||
// proxy createProgram | ||
tsc = replace(tsc, /function createProgram\(.+\) {/, s => `var createProgram = require(${JSON.stringify(proxyApiPath)}).proxyCreateProgram(` | ||
+ [ | ||
`new Proxy({}, { get(_target, p, _receiver) { return eval(p); } } )`, | ||
`_createProgram`, | ||
`require(${JSON.stringify(__filename)}).getLanguagePlugins`, | ||
].join(', ') | ||
+ `);\n` | ||
+ s.replace('createProgram', '_createProgram')); | ||
return tsc; | ||
return transformTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraExtensionsToRemove); | ||
} | ||
@@ -70,2 +40,46 @@ return readFileSync(...args); | ||
} | ||
/** | ||
* Replaces the code of typescript to add support for additional extensions and language plugins. | ||
* | ||
* @param tsc - The original code of typescript. | ||
* @param proxyApiPath - The path to the proxy API. | ||
* @param extraSupportedExtensions - An array of additional supported extensions. | ||
* @param extraExtensionsToRemove - An array of extensions to remove. | ||
* @param getLanguagePluginsFile - The file to get language plugins from. | ||
* @returns The modified typescript code. | ||
*/ | ||
function transformTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraExtensionsToRemove, getLanguagePluginsFile = __filename) { | ||
const neededPatchExtenstions = extraSupportedExtensions.filter(ext => !extraExtensionsToRemove.includes(ext)); | ||
// Add allow extensions | ||
if (extraSupportedExtensions.length) { | ||
const extsText = extraSupportedExtensions.map(ext => `"${ext}"`).join(', '); | ||
tsc = replace(tsc, /supportedTSExtensions = .*(?=;)/, s => s + `.map((group, i) => i === 0 ? group.splice(0, 0, ${extsText}) && group : group)`); | ||
tsc = replace(tsc, /supportedJSExtensions = .*(?=;)/, s => s + `.map((group, i) => i === 0 ? group.splice(0, 0, ${extsText}) && group : group)`); | ||
tsc = replace(tsc, /allSupportedExtensions = .*(?=;)/, s => s + `.map((group, i) => i === 0 ? group.splice(0, 0, ${extsText}) && group : group)`); | ||
} | ||
// Use to emit basename.xxx to basename.d.ts instead of basename.xxx.d.ts | ||
if (extraExtensionsToRemove.length) { | ||
const extsText = extraExtensionsToRemove.map(ext => `"${ext}"`).join(', '); | ||
tsc = replace(tsc, /extensionsToRemove = .*(?=;)/, s => s + `.concat([${extsText}])`); | ||
} | ||
// Support for basename.xxx to basename.xxx.d.ts | ||
if (neededPatchExtenstions.length) { | ||
const extsText = neededPatchExtenstions.map(ext => `"${ext}"`).join(', '); | ||
tsc = replace(tsc, /function changeExtension\(/, s => `function changeExtension(path, newExtension) { | ||
return [${extsText}].some(ext => path.endsWith(ext)) | ||
? path + newExtension | ||
: _changeExtension(path, newExtension) | ||
}\n` + s.replace('changeExtension', '_changeExtension')); | ||
} | ||
// proxy createProgram | ||
tsc = replace(tsc, /function createProgram\(.+\) {/, s => `var createProgram = require(${JSON.stringify(proxyApiPath)}).proxyCreateProgram(` | ||
+ [ | ||
`new Proxy({}, { get(_target, p, _receiver) { return eval(p); } } )`, | ||
`_createProgram`, | ||
`require(${JSON.stringify(getLanguagePluginsFile)}).getLanguagePlugins`, | ||
].join(', ') | ||
+ `);\n` | ||
+ s.replace('createProgram', '_createProgram')); | ||
return tsc; | ||
} | ||
function replace(text, ...[search, replace]) { | ||
@@ -72,0 +86,0 @@ const before = text; |
{ | ||
"name": "@volar/typescript", | ||
"version": "2.4.0-alpha.18", | ||
"version": "2.4.0-alpha.19", | ||
"license": "MIT", | ||
@@ -15,3 +15,3 @@ "files": [ | ||
"dependencies": { | ||
"@volar/language-core": "2.4.0-alpha.18", | ||
"@volar/language-core": "2.4.0-alpha.19", | ||
"path-browserify": "^1.0.1", | ||
@@ -23,5 +23,5 @@ "vscode-uri": "^3.0.8" | ||
"@types/path-browserify": "latest", | ||
"@volar/language-service": "2.4.0-alpha.18" | ||
"@volar/language-service": "2.4.0-alpha.19" | ||
}, | ||
"gitHead": "ea2e22ca17116b4274d661a014cff040c1d206cb" | ||
"gitHead": "cbb14a44f72c365c1e8d52eff9580fb4e9765f15" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
203514
4331
108
+ Added@volar/language-core@2.4.0-alpha.19(transitive)
+ Added@volar/source-map@2.4.0-alpha.19(transitive)
- Removed@volar/language-core@2.4.0-alpha.18(transitive)
- Removed@volar/source-map@2.4.0-alpha.18(transitive)