Skip to content

Instantly share code, notes, and snippets.

@johnsoncodehk
Last active July 10, 2024 00:36
Show Gist options
  • Save johnsoncodehk/dffafbdc8bf5a70bc0152d8eb575811f to your computer and use it in GitHub Desktop.
Save johnsoncodehk/dffafbdc8bf5a70bc0152d8eb575811f to your computer and use it in GitHub Desktop.
import type { SFCParseResult, VueLanguagePlugin } from '../types';
import { parse } from '../utils/parseSfc';
const jsxWrapper = ['<script setup lang="jsx">\n', '\n</script>'];
const tsxWrapper = ['<script setup lang="tsx">\n', '\n</script>'];
const plugin: VueLanguagePlugin = _ctx => {
return {
version: 2.1,
isValidFile(fileName) {
return fileName.endsWith('.setup.jsx') || fileName.endsWith('.setup.tsx');
},
parseSFC2(fileName, _languageId, content) {
if (fileName.endsWith('.setup.jsx')) {
return patchSFC(parse(`${jsxWrapper[0]}${content}${jsxWrapper[1]}`));
}
if (fileName.endsWith('.setup.tsx')) {
return patchSFC(parse(`${tsxWrapper[0]}${content}${tsxWrapper[1]}`));
}
},
};
};
export default plugin;
function patchSFC(sfc: SFCParseResult) {
sfc.descriptor.scriptSetup!.loc.start.column -= jsxWrapper[0].length;
sfc.descriptor.scriptSetup!.loc.start.offset -= jsxWrapper[0].length;
sfc.descriptor.scriptSetup!.loc.end.offset -= jsxWrapper[0].length;
if (sfc.descriptor.scriptSetup!.loc.end.line === sfc.descriptor.scriptSetup!.loc.start.line) {
sfc.descriptor.scriptSetup!.loc.end.column -= jsxWrapper[0].length;
}
return sfc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment