Skip to content

Commit

Permalink
refactor: sfc ast
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jan 20, 2023
1 parent 617d003 commit af45741
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 40 deletions.
13 changes: 13 additions & 0 deletions .changeset/weak-horses-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@vue-macros/common': minor
'@vue-macros/reactivity-transform-vue2': patch
'unplugin-vue-define-options': patch
'@vue-macros/define-model': patch
'@vue-macros/define-props': patch
'@vue-macros/define-slots': patch
'@vue-macros/export-props': patch
'@vue-macros/hoist-static': patch
'@vue-macros/short-emits': patch
---

refactor sfc ast
8 changes: 4 additions & 4 deletions packages/common/src/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export type SFC = Omit<SFCDescriptor, 'script' | 'scriptSetup'> & {
script?: _SFCScriptBlock | null
scriptSetup?: _SFCScriptBlock | null
lang: string | undefined
get scriptAst(): Program | undefined
get setupAst(): Program | undefined
getScriptAst(): Program | undefined
getSetupAst(): Program | undefined
} & Pick<SFCParseResult, 'errors'>

export function parseSFC(code: string, id: string): SFC {
Expand All @@ -36,11 +36,11 @@ export function parseSFC(code: string, id: string): SFC {
...descriptor,
lang,
errors,
get setupAst() {
getSetupAst() {
if (!descriptor.scriptSetup) return
return babelParse(descriptor.scriptSetup.content, lang)
},
get scriptAst() {
getScriptAst() {
if (!descriptor.script) return
return babelParse(descriptor.script.content, lang)
},
Expand Down
11 changes: 5 additions & 6 deletions packages/define-model/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
MagicString,
REPO_ISSUE_URL,
WITH_DEFAULTS,
babelParse,
getTransformResult,
isCallOf,
parseSFC,
Expand Down Expand Up @@ -178,7 +177,7 @@ export function transformDefineModel(

function processVue2Script() {
if (!script) return
const scriptAst = babelParse(script.content, lang).body
const scriptAst = getScriptAst()!.body
if (scriptAst.length === 0) return

// process normal <script>
Expand Down Expand Up @@ -263,7 +262,7 @@ export function transformDefineModel(
return isQualifiedType(node.declaration)
}
}
for (const node of setupNodes) {
for (const node of setupAst) {
const qualified = isQualifiedType(node)
if (qualified) {
return qualified
Expand Down Expand Up @@ -506,19 +505,19 @@ export function transformDefineModel(
}

if (!code.includes(DEFINE_MODEL)) return
const { script, scriptSetup, lang, setupAst } = parseSFC(code, id)
const { script, scriptSetup, getSetupAst, getScriptAst } = parseSFC(code, id)
if (!scriptSetup) return

const setupOffset = scriptSetup.loc.start.offset
const setupContent = scriptSetup.content
const setupNodes = setupAst!.body
const setupAst = getSetupAst()!.body

const s = new MagicString(code)

if (version === 2) processVue2Script()

// process <script setup>
for (const node of setupNodes) {
for (const node of setupAst) {
if (node.type === 'ExpressionStatement') {
processDefinePropsOrEmits(node.expression)

Expand Down
4 changes: 3 additions & 1 deletion packages/define-options/src/core/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ export function transformDefineOptions(code: string, id: string) {

const sfc = parseSFC(code, id)
if (!sfc.scriptSetup) return
const { scriptSetup, setupAst, scriptAst } = sfc
const { scriptSetup, getSetupAst, getScriptAst } = sfc
const setupOffset = scriptSetup.loc.start.offset
const setupAst = getSetupAst()!

const nodes = filterMacro(setupAst!.body)
if (nodes.length === 0) {
return
} else if (nodes.length > 1)
throw new SyntaxError(`duplicate ${DEFINE_OPTIONS}() call`)

const scriptAst = getScriptAst()!
if (scriptAst) checkDefaultExport(scriptAst.body)

const setupBindings = getIdentifiers(setupAst!.body)
Expand Down
7 changes: 3 additions & 4 deletions packages/define-props/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
DEFINE_PROPS,
DEFINE_PROPS_DOLLAR,
MagicString,
babelParse,
getTransformResult,
isCallOf,
parseSFC,
Expand All @@ -13,14 +12,14 @@ import type { Node } from '@babel/types'
export function transfromDefineProps(code: string, id: string) {
if (!code.includes(DEFINE_PROPS_DOLLAR)) return

const { scriptSetup, lang } = parseSFC(code, id)
const { scriptSetup, getSetupAst } = parseSFC(code, id)
if (!scriptSetup) return

const offset = scriptSetup.loc.start.offset
const s = new MagicString(code)
const program = babelParse(scriptSetup.loc.source, lang)
const setupAst = getSetupAst()!

walkAST<Node>(program, {
walkAST<Node>(setupAst, {
enter(node) {
if (isCallOf(node, DEFINE_PROPS_DOLLAR)) {
s.overwriteNode(
Expand Down
6 changes: 3 additions & 3 deletions packages/define-slots/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {
export function transfromDefineSlots(code: string, id: string) {
if (!code.includes(DEFINE_SLOTS)) return

const { scriptSetup, setupAst } = parseSFC(code, id)
if (!scriptSetup || !setupAst) return
const { scriptSetup, getSetupAst } = parseSFC(code, id)
if (!scriptSetup) return

const s = new MagicString(code)

for (const stmt of setupAst.body) {
for (const stmt of getSetupAst()!.body) {
if (
stmt.type === 'ExpressionStatement' &&
isCallOf(stmt.expression, DEFINE_SLOTS)
Expand Down
7 changes: 3 additions & 4 deletions packages/export-props/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import {
import type { VariableDeclarator } from '@babel/types'

export function transfromExportProps(code: string, id: string) {
const sfc = parseSFC(code, id)
const { scriptSetup } = sfc
const { scriptSetup, getSetupAst } = parseSFC(code, id)
if (!scriptSetup) return

const offset = scriptSetup.loc.start.offset
const s = new MagicString(code)
const program = sfc.setupAst!

const props: Record<string, { type: string; defaultValue?: string }> = {}
let hasDefineProps = false
Expand All @@ -41,7 +39,8 @@ export function transfromExportProps(code: string, id: string) {
props[name] = { type, defaultValue }
}

for (const stmt of program.body) {
const setupAst = getSetupAst()!
for (const stmt of setupAst.body) {
if (
stmt.type === 'ExportNamedDeclaration' &&
stmt.declaration?.type === 'VariableDeclaration'
Expand Down
9 changes: 4 additions & 5 deletions packages/hoist-static/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
MagicString,
addNormalScript,
babelParse,
getTransformResult,
isStaticExpression,
parseSFC,
Expand All @@ -20,16 +19,16 @@ export function transformHoistStatic(code: string, id: string) {
s.removeNode(decl, { offset: setupOffset })
}

const ctx = parseSFC(code, id)
const { scriptSetup, lang } = ctx
const sfc = parseSFC(code, id)
const { scriptSetup, getSetupAst } = sfc
if (!scriptSetup) return

const setupOffset = scriptSetup.loc.start.offset
const setupOffsetEnd = scriptSetup.loc.end.offset
const s = new MagicString(code)
const program = babelParse(scriptSetup.loc.source, lang)
const program = getSetupAst()!

let normalScript = addNormalScript(ctx, s)
let normalScript = addNormalScript(sfc, s)
let scriptOffset: number | undefined

for (const stmt of program.body) {
Expand Down
14 changes: 7 additions & 7 deletions packages/reactivity-transform-vue2/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
DEFINE_PROPS,
MagicString,
babelParse,
getTransformResult,
isCallOf,
parseSFC,
Expand All @@ -15,23 +14,24 @@ export * from './helper'

export function transfromVueSFC(code: string, id: string) {
const s = new MagicString(code)
const { script, scriptSetup } = parseSFC(code, id)
const { script, scriptSetup, getScriptAst, getSetupAst } = parseSFC(code, id)

let refBindings: string[] | undefined
let propsDestructuredBindings: Record<string, { local: string }> | undefined

if (script && shouldTransform(script.content)) {
const ast = babelParse(script.content, script.lang)
const offset = script.loc.start.offset

const { importedHelpers, rootRefs } = transformAST(ast, s, offset)
const { importedHelpers, rootRefs } = transformAST(
getScriptAst()!,
s,
offset
)
refBindings = rootRefs
addHelpers(s, script.loc.start.offset, importedHelpers)
}

if (scriptSetup) {
const ast = babelParse(scriptSetup.content, scriptSetup.lang)

const ast = getSetupAst()!
for (const node of ast.body) {
processDefineProps(node)
}
Expand Down
10 changes: 4 additions & 6 deletions packages/short-emits/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
MagicString,
babelParse,
getTransformResult,
isTs,
parseSFC,
Expand All @@ -20,20 +19,19 @@ import type {
export function transformShortEmits(code: string, id: string) {
if (!code.includes('SE') && !code.includes('ShortEmits')) return

const ctx = parseSFC(code, id)
const { scriptSetup, lang } = ctx
const sfc = parseSFC(code, id)
const { scriptSetup, lang, getSetupAst } = sfc
if (!scriptSetup || !isTs(lang)) return

const offset = scriptSetup.loc.start.offset

const program = babelParse(scriptSetup.loc.source, lang)
const ast = getSetupAst()!

const nodes: {
def: TSType
type: TSTypeReference
}[] = []

walkAST<Node>(program, {
walkAST<Node>(ast, {
enter(node) {
if (
node.type === 'TSTypeReference' &&
Expand Down

0 comments on commit af45741

Please sign in to comment.