Last active
July 10, 2024 00:36
-
-
Save johnsoncodehk/dffafbdc8bf5a70bc0152d8eb575811f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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