Add import assertions to type only imports and import types to force the resolution mode of the specifier (#47807)

* Add import assertions for type-only imports and import types to change resolver modes

* By popular request, only allow mode assertions on top-level type only imports

* Add specifier options parameter to specifier generation
This commit is contained in:
Wesley Wigham
2022-03-02 08:54:10 -08:00
committed by GitHub
parent ff1f7b15b7
commit ea0db9e031
60 changed files with 3302 additions and 314 deletions

View File

@@ -9,12 +9,12 @@ namespace ts.server {
let cache: ESMap<Path, ResolvedModuleSpecifierInfo> | undefined;
let currentKey: string | undefined;
const result: ModuleSpecifierCache = {
get(fromFileName, toFileName, preferences) {
if (!cache || currentKey !== key(fromFileName, preferences)) return undefined;
get(fromFileName, toFileName, preferences, options) {
if (!cache || currentKey !== key(fromFileName, preferences, options)) return undefined;
return cache.get(toFileName);
},
set(fromFileName, toFileName, preferences, modulePaths, moduleSpecifiers) {
ensureCache(fromFileName, preferences).set(toFileName, createInfo(modulePaths, moduleSpecifiers, /*isAutoImportable*/ true));
set(fromFileName, toFileName, preferences, options, modulePaths, moduleSpecifiers) {
ensureCache(fromFileName, preferences, options).set(toFileName, createInfo(modulePaths, moduleSpecifiers, /*isAutoImportable*/ true));
// If any module specifiers were generated based off paths in node_modules,
// a package.json file in that package was read and is an input to the cached.
@@ -36,8 +36,8 @@ namespace ts.server {
}
}
},
setModulePaths(fromFileName, toFileName, preferences, modulePaths) {
const cache = ensureCache(fromFileName, preferences);
setModulePaths(fromFileName, toFileName, preferences, options, modulePaths) {
const cache = ensureCache(fromFileName, preferences, options);
const info = cache.get(toFileName);
if (info) {
info.modulePaths = modulePaths;
@@ -46,8 +46,8 @@ namespace ts.server {
cache.set(toFileName, createInfo(modulePaths, /*moduleSpecifiers*/ undefined, /*isAutoImportable*/ undefined));
}
},
setIsAutoImportable(fromFileName, toFileName, preferences, isAutoImportable) {
const cache = ensureCache(fromFileName, preferences);
setIsAutoImportable(fromFileName, toFileName, preferences, options, isAutoImportable) {
const cache = ensureCache(fromFileName, preferences, options);
const info = cache.get(toFileName);
if (info) {
info.isAutoImportable = isAutoImportable;
@@ -71,8 +71,8 @@ namespace ts.server {
}
return result;
function ensureCache(fromFileName: Path, preferences: UserPreferences) {
const newKey = key(fromFileName, preferences);
function ensureCache(fromFileName: Path, preferences: UserPreferences, options: ModuleSpecifierOptions) {
const newKey = key(fromFileName, preferences, options);
if (cache && (currentKey !== newKey)) {
result.clear();
}
@@ -80,8 +80,8 @@ namespace ts.server {
return cache ||= new Map();
}
function key(fromFileName: Path, preferences: UserPreferences) {
return `${fromFileName},${preferences.importModuleSpecifierEnding},${preferences.importModuleSpecifierPreference}`;
function key(fromFileName: Path, preferences: UserPreferences, options: ModuleSpecifierOptions) {
return `${fromFileName},${preferences.importModuleSpecifierEnding},${preferences.importModuleSpecifierPreference},${options.overrideImportMode}`;
}
function createInfo(