Skip to content

Commit

Permalink
Fix SFC without a script tag show an error
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo930021 committed Mar 25, 2020
1 parent dcf66c4 commit 847ab4d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
4 changes: 4 additions & 0 deletions server/src/embeddedSupport/embeddedSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ export function getSingleTypeDocument(
}
}

if (type === 'script' && newContent.trim().length <= 0) {
newContent = 'export default {};';
}

return TextDocument.create(document.uri, langId, document.version, newContent);
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/services/typescriptService/preprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function parseVueScript(text: string): string {
const doc = TextDocument.create('test://test/test.vue', 'vue', 0, text);
const regions = getVueDocumentRegions(doc);
const script = regions.getSingleTypeDocument('script');
return script.getText() || 'export default {};';
return script.getText();
}

function parseVueScriptSrc(text: string): string | undefined {
Expand Down
12 changes: 12 additions & 0 deletions test/lsp/diagnostics/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,15 @@ export async function testDiagnostics(docUri: vscode.Uri, expectedDiagnostics: v
);
}
}

export async function testNoDiagnostics(docUri: vscode.Uri) {
// For diagnostics to show up
await sleep(2000);

const result = vscode.languages.getDiagnostics(docUri);

assert.ok(
result.length === 0,
`Should find no diagnostics for ${docUri.fsPath} but found:\n` + `${JSON.stringify(result)}`
);
}
17 changes: 17 additions & 0 deletions test/lsp/diagnostics/simple.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { activateLS, sleep, showFile, FILE_LOAD_SLEEP_TIME } from '../helper';
import { getDocUri } from '../util';
import { testNoDiagnostics } from './helper';

describe('Should find common diagnostics for all regions', () => {
const docUri = getDocUri('client/diagnostics/Parent.vue');

before('activate', async () => {
await activateLS();
await showFile(docUri);
await sleep(FILE_LOAD_SLEEP_TIME);
});

it('shows no diagnostics error for <script> region', async () => {
await testNoDiagnostics(docUri);
});
});
13 changes: 13 additions & 0 deletions test/lsp/fixture/client/diagnostics/Parent.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<template>
<div></div>
</template>

<script>
import SimpleChild from './SimpleChild.vue'
export default {
components: {
SimpleChild
}
}
</script>
3 changes: 3 additions & 0 deletions test/lsp/fixture/client/diagnostics/SimpleChild.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<h1>No script tag in this component</h1>
</template>

0 comments on commit 847ab4d

Please sign in to comment.