Skip to content

Commit 66ba23f

Browse files
authored
fix: don't provides quickfix to add async to the script tag (#2737)
1 parent 52e80f2 commit 66ba23f

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,18 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
722722
);
723723
}
724724

725+
if (fix.fixName === 'fixAwaitInSyncFunction' && document.scriptInfo) {
726+
const scriptStartTagStart = document.scriptInfo.container.start;
727+
const scriptStartTagEnd = document.scriptInfo.start;
728+
const withinStartTag =
729+
document.offsetAt(originalRange.start) < scriptStartTagEnd &&
730+
document.offsetAt(originalRange.end) > scriptStartTagStart;
731+
732+
if (withinStartTag) {
733+
return undefined;
734+
}
735+
}
736+
725737
if (fix.fixName === 'fixMissingFunctionDeclaration') {
726738
const position = 'position' in fix ? fix.position : undefined;
727739
const checkRange = position

packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,91 @@ describe('CodeActionsProvider', function () {
880880
assert.deepStrictEqual(codeActions, []);
881881
});
882882

883+
it('provides quickfix to add async to a function', async () => {
884+
const { provider, document } = setup('fix-add-async.svelte');
885+
886+
const codeActions = await provider.getCodeActions(
887+
document,
888+
Range.create(Position.create(6, 8), Position.create(6, 9)),
889+
{
890+
diagnostics: [
891+
{
892+
code: 1308,
893+
message:
894+
"'await' expressions are only allowed within async functions and at the top levels of modules.",
895+
range: Range.create(Position.create(6, 8), Position.create(6, 13)),
896+
source: 'ts'
897+
}
898+
],
899+
only: [CodeActionKind.QuickFix]
900+
}
901+
);
902+
903+
assert.deepStrictEqual(codeActions, [
904+
{
905+
edit: {
906+
documentChanges: [
907+
{
908+
edits: [
909+
{
910+
newText: 'async ',
911+
range: {
912+
end: {
913+
character: 4,
914+
line: 5
915+
},
916+
start: {
917+
character: 4,
918+
line: 5
919+
}
920+
}
921+
}
922+
],
923+
textDocument: {
924+
uri: getUri('fix-add-async.svelte'),
925+
version: null
926+
}
927+
}
928+
]
929+
},
930+
kind: 'quickfix',
931+
title: 'Add async modifier to containing function'
932+
},
933+
{
934+
data: {
935+
fixId: 'fixAwaitInSyncFunction',
936+
fixName: 'fixAwaitInSyncFunction',
937+
uri: getUri('fix-add-async.svelte')
938+
},
939+
kind: 'quickfix',
940+
title: "Add all missing 'async' modifiers"
941+
}
942+
]);
943+
});
944+
945+
it("don't provides quickfix to add async to the script tag", async () => {
946+
const { provider, document } = setup('fix-add-async.svelte');
947+
948+
const codeActions = await provider.getCodeActions(
949+
document,
950+
Range.create(Position.create(2, 8), Position.create(2, 9)),
951+
{
952+
diagnostics: [
953+
{
954+
code: 1308,
955+
message:
956+
"'await' expressions are only allowed within async functions and at the top levels of modules.",
957+
range: Range.create(Position.create(2, 8), Position.create(2, 13)),
958+
source: 'ts'
959+
}
960+
],
961+
only: [CodeActionKind.QuickFix]
962+
}
963+
);
964+
965+
assert.deepStrictEqual(codeActions, []);
966+
});
967+
883968
it('provide quick fix to fix all errors when possible', async () => {
884969
const { provider, document } = setup('codeactions.svelte');
885970

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
{
3+
await Promise.resolve()
4+
}
5+
6+
function test() {
7+
await Promise.resolve()
8+
}
9+
</script>

0 commit comments

Comments
 (0)