Skip to content

Commit

Permalink
(fix) Component auto Import when the import specifier config is js (#…
Browse files Browse the repository at this point in the history
…1242)

Related to #1187
TypeScript throws a debug assertion error if the importModuleSpecifierEnding config is 'js' and there's an unknown file extension (like '.svelte').

Although it's probably better to fix this on the TypeScript side, that it would probably take a while. So this is a workaround for now. Not all cases are fixed, but at least it's less likely to happen.
  • Loading branch information
jasonlyu123 authored Nov 16, 2021
1 parent 1f7efec commit e642d9e
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {

const name = node.tagName.getText();
const suffixedName = name + '__SvelteComponent_';
const errorPreventingUserPreferences =
this.completionProvider.fixUserPreferencesForSvelteComponentImport(userPreferences);

const toFix = (c: ts.CompletionEntry) =>
lang
.getCompletionEntryDetails(
Expand All @@ -336,7 +339,7 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
c.name,
{},
c.source,
userPreferences,
errorPreventingUserPreferences,
c.data
)
?.codeActions?.map((a) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,24 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
return completionItem;
}

/**
* TypeScript throws a debug assertion error if the importModuleSpecifierEnding config is
* 'js' and there's an unknown file extension - which is the case for `.svelte`. Therefore
* rewrite the importModuleSpecifierEnding for this case to silence the error.
*/
fixUserPreferencesForSvelteComponentImport(
userPreferences: ts.UserPreferences
): ts.UserPreferences {
if (userPreferences.importModuleSpecifierEnding === 'js') {
return {
...userPreferences,
importModuleSpecifierEnding: 'index'
};
}

return userPreferences;
}

async resolveCompletion(
document: Document,
completionItem: AppCompletionItem<CompletionEntryWithIdentifer>,
Expand All @@ -409,14 +427,17 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
}

const fragment = await tsDoc.getFragment();
const errorPreventingUserPreferences = comp.source?.endsWith('.svelte')
? this.fixUserPreferencesForSvelteComponentImport(userPreferences)
: userPreferences;

const detail = lang.getCompletionEntryDetails(
filePath,
fragment.offsetAt(fragment.getGeneratedPosition(comp.position)),
comp.name,
{},
comp.source,
userPreferences,
errorPreventingUserPreferences,
comp.data
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,80 @@ describe('ts user preferences', () => {
const item = completions?.items.find((item) => item.label === 'definition');
assert.strictEqual(item, undefined, 'Expected no auto import suggestions');
});

const expectedComponentImportEdit = "import Imports from '~/imports.svelte';";

function setupImportModuleSpecifierEndingJs() {
const { docManager, document } = setup('module-specifier-js.svelte');
const lsAndTsDocResolver = createLSAndTSDocResolver(docManager, {
preferences: {
importModuleSpecifier: 'non-relative',
importModuleSpecifierEnding: 'js',
quoteStyle: 'single'
}
});

return { document, lsAndTsDocResolver };
}

it('provides auto import for svelte component when importModuleSpecifierEnding is js', async () => {
const { document, lsAndTsDocResolver } = setupImportModuleSpecifierEndingJs();

const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver);

const completions = await completionProvider.getCompletions(
document,
Position.create(4, 8)
);

const item = completions?.items.find((item) => item.label === 'Imports');
const { additionalTextEdits } = await completionProvider.resolveCompletion(document, item!);
assert.strictEqual(additionalTextEdits![0].newText.trim(), expectedComponentImportEdit);
});

it('provides auto import for context="module" export when importModuleSpecifierEnding is js', async () => {
const { document, lsAndTsDocResolver } = setupImportModuleSpecifierEndingJs();

const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver);

const completions = await completionProvider.getCompletions(
document,
Position.create(1, 6)
);

const item = completions?.items.find((item) => item.label === 'hi');
const { additionalTextEdits } = await completionProvider.resolveCompletion(document, item!);
assert.strictEqual(
additionalTextEdits![0].newText.trim(),
"import { hi } from '~/with-context-module.svelte';"
);
});

it('provides import code action for svelte component when importModuleSpecifierEnding is js', async () => {
const range = Range.create(Position.create(4, 1), Position.create(4, 8));
const { document, lsAndTsDocResolver } = setupImportModuleSpecifierEndingJs();

const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver);
const codeActionProvider = new CodeActionsProviderImpl(
lsAndTsDocResolver,
completionProvider
);

const codeAction = await codeActionProvider.getCodeActions(document, range, {
diagnostics: [
Diagnostic.create(
range,
"Cannot find name 'Imports'",
DiagnosticSeverity.Error,
2304,
'ts'
)
]
});

const documentChange = codeAction[0].edit?.documentChanges?.[0] as
| TextDocumentEdit
| undefined;
assert.strictEqual(documentChange?.edits[0].newText.trim(), expectedComponentImportEdit);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script lang="ts">
hi
</script>

<Imports></Imports>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script lang="ts" context="module">
export function hi() {}
</script>

0 comments on commit e642d9e

Please sign in to comment.