@volar/typescript
Advanced tools
Comparing version 2.2.0-alpha.7 to 2.2.0-alpha.8
@@ -7,3 +7,33 @@ "use strict"; | ||
const decorateProgram_1 = require("./decorateProgram"); | ||
const arrayEqual = (a, b) => { | ||
if (a.length !== b.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < a.length; i++) { | ||
if (a[i] !== b[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
const objectEqual = (a, b) => { | ||
const keysA = Object.keys(a); | ||
const keysB = Object.keys(b); | ||
if (keysA.length !== keysB.length) { | ||
return false; | ||
} | ||
for (const key of keysA) { | ||
if (a[key] !== b[key]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
function proxyCreateProgram(ts, original, getLanguagePlugins, getLanguageId) { | ||
const sourceFileSnapshots = new Map(); | ||
const parsedSourceFiles = new WeakMap(); | ||
let lastOptions; | ||
let languagePlugins; | ||
let language; | ||
let moduleResolutionCache; | ||
return new Proxy(original, { | ||
@@ -13,65 +43,91 @@ apply: (target, thisArg, args) => { | ||
assert(!!options.host, '!!options.host'); | ||
const languagePlugins = getLanguagePlugins(ts, options); | ||
if (!lastOptions | ||
|| !languagePlugins | ||
|| !language | ||
|| !arrayEqual(options.rootNames, lastOptions.rootNames) | ||
|| !objectEqual(options.options, lastOptions.options)) { | ||
moduleResolutionCache = ts.createModuleResolutionCache(options.host.getCurrentDirectory(), options.host.getCanonicalFileName, options.options); | ||
lastOptions = options; | ||
languagePlugins = getLanguagePlugins(ts, options); | ||
language = (0, language_core_1.createLanguage)(languagePlugins, ts.sys.useCaseSensitiveFileNames, fileName => { | ||
if (!sourceFileSnapshots.has(fileName)) { | ||
const sourceFileText = originalHost.readFile(fileName); | ||
if (sourceFileText !== undefined) { | ||
sourceFileSnapshots.set(fileName, [undefined, { | ||
getChangeRange() { | ||
return undefined; | ||
}, | ||
getLength() { | ||
return sourceFileText.length; | ||
}, | ||
getText(start, end) { | ||
return sourceFileText.substring(start, end); | ||
}, | ||
}]); | ||
} | ||
else { | ||
sourceFileSnapshots.set(fileName, [undefined, undefined]); | ||
} | ||
} | ||
const snapshot = sourceFileSnapshots.get(fileName)?.[1]; | ||
if (snapshot) { | ||
language.scripts.set(fileName, getLanguageId(fileName), snapshot); | ||
} | ||
else { | ||
language.scripts.delete(fileName); | ||
} | ||
}); | ||
} | ||
const originalHost = options.host; | ||
const extensions = languagePlugins | ||
.map(plugin => plugin.typescript?.extraFileExtensions.map(({ extension }) => `.${extension}`) ?? []) | ||
.flat(); | ||
const sourceFileToSnapshotMap = new WeakMap(); | ||
const language = (0, language_core_1.createLanguage)(languagePlugins, ts.sys.useCaseSensitiveFileNames, fileName => { | ||
let snapshot; | ||
const sourceFile = originalHost.getSourceFile(fileName, 99); | ||
if (sourceFile) { | ||
snapshot = sourceFileToSnapshotMap.get(sourceFile); | ||
if (!snapshot) { | ||
snapshot = { | ||
getChangeRange() { | ||
return undefined; | ||
}, | ||
getLength() { | ||
return sourceFile.text.length; | ||
}, | ||
getText(start, end) { | ||
return sourceFile.text.substring(start, end); | ||
}, | ||
}; | ||
sourceFileToSnapshotMap.set(sourceFile, snapshot); | ||
options.host = { ...originalHost }; | ||
options.host.getSourceFile = (fileName, languageVersionOrOptions, onError, shouldCreateNewSourceFile) => { | ||
const originalSourceFile = originalHost.getSourceFile(fileName, languageVersionOrOptions, onError, shouldCreateNewSourceFile); | ||
if (!sourceFileSnapshots.has(fileName) | ||
|| sourceFileSnapshots.get(fileName)?.[0] !== originalSourceFile) { | ||
if (originalSourceFile) { | ||
sourceFileSnapshots.set(fileName, [originalSourceFile, { | ||
getChangeRange() { | ||
return undefined; | ||
}, | ||
getLength() { | ||
return originalSourceFile.text.length; | ||
}, | ||
getText(start, end) { | ||
return originalSourceFile.text.substring(start, end); | ||
}, | ||
}]); | ||
} | ||
else { | ||
sourceFileSnapshots.set(fileName, [undefined, undefined]); | ||
} | ||
} | ||
if (snapshot) { | ||
language.scripts.set(fileName, getLanguageId(fileName), snapshot); | ||
if (!originalSourceFile) { | ||
return; | ||
} | ||
else { | ||
language.scripts.delete(fileName); | ||
} | ||
}); | ||
const parsedSourceFiles = new WeakMap(); | ||
const originalHost = options.host; | ||
options.host = { ...originalHost }; | ||
options.host.getSourceFile = (fileName, languageVersionOrOptions, onError, shouldCreateNewSourceFile) => { | ||
const originalSourceFile = originalHost.getSourceFile(fileName, languageVersionOrOptions, onError, shouldCreateNewSourceFile); | ||
if (originalSourceFile && extensions.some(ext => fileName.endsWith(ext))) { | ||
let sourceFile2 = parsedSourceFiles.get(originalSourceFile); | ||
if (!sourceFile2) { | ||
const sourceScript = language.scripts.get(fileName); | ||
assert(!!sourceScript, '!!sourceScript'); | ||
let patchedText = originalSourceFile.text.split('\n').map(line => ' '.repeat(line.length)).join('\n'); | ||
let scriptKind = ts.ScriptKind.TS; | ||
if (sourceScript.generated?.languagePlugin.typescript) { | ||
const { getServiceScript, getExtraServiceScripts } = sourceScript.generated.languagePlugin.typescript; | ||
const serviceScript = getServiceScript(sourceScript.generated.root); | ||
if (serviceScript) { | ||
scriptKind = serviceScript.scriptKind; | ||
patchedText += serviceScript.code.snapshot.getText(0, serviceScript.code.snapshot.getLength()); | ||
} | ||
if (getExtraServiceScripts) { | ||
console.warn('getExtraServiceScripts() is not available in this use case.'); | ||
} | ||
if (!parsedSourceFiles.has(originalSourceFile)) { | ||
const sourceScript = language.scripts.get(fileName); | ||
assert(!!sourceScript, '!!sourceScript'); | ||
parsedSourceFiles.set(originalSourceFile, originalSourceFile); | ||
if (sourceScript.generated?.languagePlugin.typescript) { | ||
const { getServiceScript, getExtraServiceScripts } = sourceScript.generated.languagePlugin.typescript; | ||
const serviceScript = getServiceScript(sourceScript.generated.root); | ||
if (serviceScript) { | ||
let patchedText = originalSourceFile.text.split('\n').map(line => ' '.repeat(line.length)).join('\n'); | ||
let scriptKind = ts.ScriptKind.TS; | ||
scriptKind = serviceScript.scriptKind; | ||
patchedText += serviceScript.code.snapshot.getText(0, serviceScript.code.snapshot.getLength()); | ||
const parsedSourceFile = ts.createSourceFile(fileName, patchedText, languageVersionOrOptions, undefined, scriptKind); | ||
// @ts-expect-error | ||
parsedSourceFile.version = originalSourceFile.version; | ||
parsedSourceFiles.set(originalSourceFile, parsedSourceFile); | ||
} | ||
sourceFile2 = ts.createSourceFile(fileName, patchedText, 99, true, scriptKind); | ||
// @ts-expect-error | ||
sourceFile2.version = originalSourceFile.version; | ||
parsedSourceFiles.set(originalSourceFile, sourceFile2); | ||
if (getExtraServiceScripts) { | ||
console.warn('getExtraServiceScripts() is not available in this use case.'); | ||
} | ||
} | ||
return sourceFile2; | ||
} | ||
return originalSourceFile; | ||
return parsedSourceFiles.get(originalSourceFile); | ||
}; | ||
@@ -83,3 +139,2 @@ if (extensions.length) { | ||
const resolveModuleNames = originalHost.resolveModuleNames; | ||
const moduleResolutionCache = ts.createModuleResolutionCache(originalHost.getCurrentDirectory(), originalHost.getCanonicalFileName, options.options); | ||
options.host.resolveModuleNameLiterals = (moduleLiterals, containingFile, redirectedReference, compilerOptions, ...rest) => { | ||
@@ -86,0 +141,0 @@ if (resolveModuleNameLiterals && moduleLiterals.every(name => !extensions.some(ext => name.text.endsWith(ext)))) { |
{ | ||
"name": "@volar/typescript", | ||
"version": "2.2.0-alpha.7", | ||
"version": "2.2.0-alpha.8", | ||
"license": "MIT", | ||
@@ -15,3 +15,3 @@ "files": [ | ||
"dependencies": { | ||
"@volar/language-core": "2.2.0-alpha.7", | ||
"@volar/language-core": "2.2.0-alpha.8", | ||
"path-browserify": "^1.0.1" | ||
@@ -22,5 +22,5 @@ }, | ||
"@types/path-browserify": "latest", | ||
"@volar/language-service": "2.2.0-alpha.7" | ||
"@volar/language-service": "2.2.0-alpha.8" | ||
}, | ||
"gitHead": "c6a2483c541437bb770ca114653d21ce61ccf9bc" | ||
"gitHead": "a48e2549a5bf216ab655e4ef7e6d8c545d10d558" | ||
} |
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
174011
3737
+ Added@volar/language-core@2.2.0-alpha.8(transitive)
+ Added@volar/source-map@2.2.0-alpha.8(transitive)
- Removed@volar/language-core@2.2.0-alpha.7(transitive)
- Removed@volar/source-map@2.2.0-alpha.7(transitive)