Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable parsing of script setup tag in SFC #477

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions __tests__/core/__fixtures__/ComponentScriptSetup.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<template>
<div>Hello world</div>
</template>

<script setup>
/**
* @vue-event {String} foo - Emit foo event
*/
const emit = defineEmits(['foo'])
</script>
10 changes: 10 additions & 0 deletions __tests__/core/__snapshots__/vueScriptSetupExtractor.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`core.extractVueScript extract script 1`] = `
"
/**
* @vue-event {String} foo - Emit foo event
*/
const emit = defineEmits(['foo'])
"
`;
10 changes: 10 additions & 0 deletions __tests__/core/vueScriptSetupExtractor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('path');
const extractVueScript = require('../../lib/core/vueScriptExtractor');

describe('core.extractVueScript', () => {
const filename = path.join(__dirname, '__fixtures__', 'ComponentScriptSetup.vue');

test('extract script', () => {
expect(extractVueScript(filename)).toMatchSnapshot();
});
});
4 changes: 4 additions & 0 deletions lib/core/seekExportDefaultLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ module.exports = function seekExportDefaultLine(source, filename) {
if (inScript && /export\s+default/.test(line)) {
return i + 1; // index starts at 0, but file's lines start at 1
}

if (inScript && /<script\ssetup>/.test(line)) {
return i;
}
}

return 0;
Expand Down
4 changes: 2 additions & 2 deletions lib/core/vueScriptExtractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const compiler = require('@vue/compiler-sfc');
module.exports = function extractVueScript(filename) {
const source = fs.readFileSync(filename, 'utf8');
const parsedComponent = compiler.parse(source);
const scriptContent = parsedComponent.descriptor.script ? parsedComponent.descriptor.script.content : '';

let scriptContent = parsedComponent.descriptor.script ? parsedComponent.descriptor.script.content : null;
scriptContent ??= parsedComponent.descriptor.scriptSetup ? parsedComponent.descriptor.scriptSetup.content : '';
return scriptContent;
};