Skip to content

Commit

Permalink
feat: upgrade magic-string-ast
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Apr 17, 2024
1 parent 8b8ef7b commit 102a074
Show file tree
Hide file tree
Showing 40 changed files with 557 additions and 835 deletions.
27 changes: 27 additions & 0 deletions .changeset/afraid-windows-collect.md
@@ -0,0 +1,27 @@
---
"@vue-macros/reactivity-transform": patch
"@vue-macros/define-props-refs": patch
"@vue-macros/setup-component": patch
"unplugin-vue-define-options": patch
"@vue-macros/named-template": patch
"@vue-macros/better-define": patch
"@vue-macros/define-models": patch
"@vue-macros/define-render": patch
"@vue-macros/export-expose": patch
"@vue-macros/export-render": patch
"@vue-macros/jsx-directive": patch
"@vue-macros/define-props": patch
"@vue-macros/define-slots": patch
"@vue-macros/export-props": patch
"@vue-macros/hoist-static": patch
"@vue-macros/define-emit": patch
"@vue-macros/define-prop": patch
"@vue-macros/setup-block": patch
"@vue-macros/short-emits": patch
"@vue-macros/chain-call": patch
"@vue-macros/setup-sfc": patch
"@vue-macros/common": patch
"@vue-macros/api": patch
---

upgrade magic-string-ast
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -60,7 +60,7 @@
"tsx": "^4.7.2",
"typescript": "~5.4.5",
"unocss": "^0.59.2",
"unplugin-macros": "^0.11.0",
"unplugin-macros": "^0.12.1",
"unplugin-raw": "^0.1.1",
"vite": "^5.2.8",
"vitest": "^1.5.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/vue/analyze.ts
@@ -1,7 +1,7 @@
import {
DEFINE_EMITS,
DEFINE_PROPS,
type MagicString,
type MagicStringAST,
type SFC,
WITH_DEFAULTS,
babelParse,
Expand All @@ -26,7 +26,7 @@ export interface AnalyzeResult {
}

export async function analyzeSFC(
s: MagicString,
s: MagicStringAST,
sfc: SFC,
): Promise<AnalyzeResult> {
if (!sfc.scriptSetup) throw new Error('Only <script setup> is supported')
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/vue/emits.ts
@@ -1,5 +1,5 @@
import {
type MagicString,
type MagicStringAST,
type SFC,
babelParse,
isStaticExpression,
Expand Down Expand Up @@ -45,7 +45,7 @@ export async function handleTSEmitsDefinition({
declId,
statement,
}: {
s: MagicString
s: MagicStringAST
file: TSFile
sfc: SFC
offset: number
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/vue/props.ts
@@ -1,5 +1,5 @@
import {
type MagicString,
type MagicStringAST,
type SFC,
babelParse,
isStaticObjectKey,
Expand Down Expand Up @@ -93,7 +93,7 @@ export async function handleTSPropsDefinition({
statement,
declId,
}: {
s: MagicString
s: MagicStringAST
file: TSFile
sfc: SFC
offset: number
Expand Down
4 changes: 2 additions & 2 deletions packages/api/tests/analyze-sfc.test.ts
@@ -1,11 +1,11 @@
import { MagicString, parseSFC } from '@vue-macros/common'
import { MagicStringAST, parseSFC } from '@vue-macros/common'
import { describe, expect, test } from 'vitest'
import { DefinitionKind, analyzeSFC } from '../src/vue'
import { hideAstLocation, snapshot } from './_util'

async function complie(code: string) {
const str = `<script setup lang="ts">\n${code}</script>`
const s = new MagicString(str)
const s = new MagicStringAST(str)
const sfc = parseSFC(str, 'test.vue')
return { ...(await analyzeSFC(s, sfc)), s }
}
Expand Down
4 changes: 2 additions & 2 deletions packages/better-define/src/core/index.ts
@@ -1,6 +1,6 @@
import {
DEFINE_EMITS,
MagicString,
MagicStringAST,
escapeKey,
generateTransform,
importHelperFn,
Expand All @@ -18,7 +18,7 @@ export async function transformBetterDefine(
id: string,
isProduction = false,
) {
const s = new MagicString(code)
const s = new MagicStringAST(code)
const sfc = parseSFC(code, id)
if (!sfc.scriptSetup) return

Expand Down
4 changes: 2 additions & 2 deletions packages/chain-call/src/core/index.ts
@@ -1,6 +1,6 @@
import {
DEFINE_PROPS,
MagicString,
MagicStringAST,
WITH_DEFAULTS,
generateTransform,
isCallOf,
Expand All @@ -17,7 +17,7 @@ export function transformChainCall(code: string, id: string) {
const { scriptSetup, getSetupAst, offset } = parseSFC(code, id)
if (!scriptSetup) return

const s = new MagicString(code)
const s = new MagicStringAST(code)
const setupAst = getSetupAst()!

walkAST<Node>(setupAst, {
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Expand Up @@ -58,7 +58,7 @@
"@vue/compiler-sfc": "^3.4.21",
"ast-kit": "^0.12.1",
"local-pkg": "^0.5.0",
"magic-string-ast": "^0.3.0"
"magic-string-ast": "^0.5.0"
},
"devDependencies": {
"@babel/parser": "^7.24.4",
Expand Down
6 changes: 3 additions & 3 deletions packages/common/src/ast.ts
@@ -1,6 +1,6 @@
import { walkIdentifiers } from '@vue/compiler-sfc'
import { isFunctionType, isLiteralType, resolveObjectKey } from 'ast-kit'
import type { MagicStringBase } from 'magic-string-ast'
import type { MagicString } from 'magic-string-ast'
import type * as t from '@babel/types'

export function checkInvalidScopeReference(
Expand Down Expand Up @@ -145,10 +145,10 @@ export function resolveObjectExpression(node: t.ObjectExpression) {
return maps
}

const importedMap = new WeakMap<MagicStringBase, Set<string>>()
const importedMap = new WeakMap<MagicString, Set<string>>()
export const HELPER_PREFIX = '__MACROS_'
export function importHelperFn(
s: MagicStringBase,
s: MagicString,
offset: number,
local: string,
from = 'vue',
Expand Down
10 changes: 7 additions & 3 deletions packages/common/src/vue.ts
Expand Up @@ -7,7 +7,7 @@ import {
import { babelParse, getLang, resolveString } from 'ast-kit'
import { REGEX_VUE_SFC } from './constants'
import type { Node, Program } from '@babel/types'
import type { MagicString, MagicStringBase } from 'magic-string-ast'
import type { MagicString, MagicStringAST } from 'magic-string-ast'

export type SFCScriptBlock = Omit<
SFCScriptBlockMixed,
Expand Down Expand Up @@ -86,7 +86,7 @@ export function getFileCodeAndLang(
}
}

export function addNormalScript({ script, lang }: SFC, s: MagicStringBase) {
export function addNormalScript({ script, lang }: SFC, s: MagicString) {
return {
start() {
if (script) return script.loc.end.offset
Expand All @@ -102,7 +102,11 @@ export function addNormalScript({ script, lang }: SFC, s: MagicStringBase) {
}
}

export function removeMacroImport(node: Node, s: MagicString, offset: number) {
export function removeMacroImport(
node: Node,
s: MagicStringAST,
offset: number,
) {
if (
node.type === 'ImportDeclaration' &&
node.attributes?.some(
Expand Down
4 changes: 2 additions & 2 deletions packages/define-emit/src/core/index.ts
@@ -1,7 +1,7 @@
import {
DEFINE_EMIT,
HELPER_PREFIX,
MagicString,
MagicStringAST,
escapeKey,
generateTransform,
isCallOf,
Expand All @@ -24,7 +24,7 @@ export async function transformDefineEmit(code: string, id: string) {
if (!scriptSetup) return

const offset = scriptSetup.loc.start.offset
const s = new MagicString(code)
const s = new MagicStringAST(code)
const setupAst = getSetupAst()!
const emits: Emit[] = []

Expand Down
4 changes: 2 additions & 2 deletions packages/define-models/src/core/index.ts
Expand Up @@ -6,7 +6,7 @@ import {
DEFINE_OPTIONS,
DEFINE_PROPS,
HELPER_PREFIX,
MagicString,
MagicStringAST,
REPO_ISSUE_URL,
WITH_DEFAULTS,
generateTransform,
Expand Down Expand Up @@ -464,7 +464,7 @@ export function transformDefineModels(
const setupContent = scriptSetup.content
const setupAst = getSetupAst()!.body

const s = new MagicString(code)
const s = new MagicStringAST(code)

if (version === 2) processVue2Script()

Expand Down
4 changes: 2 additions & 2 deletions packages/define-options/src/core/index.ts
@@ -1,6 +1,6 @@
import {
DEFINE_OPTIONS,
MagicString,
MagicStringAST,
addNormalScript,
checkInvalidScopeReference,
generateTransform,
Expand Down Expand Up @@ -33,7 +33,7 @@ export function transformDefineOptions(code: string, id: string) {

const setupBindings = getRootScopeIds(setupAst)

const s = new MagicString(code)
const s = new MagicStringAST(code)

const [node] = nodes
const [arg] = node.arguments
Expand Down
4 changes: 2 additions & 2 deletions packages/define-prop/src/core/index.ts
Expand Up @@ -2,7 +2,7 @@ import {
DEFINE_PROP,
DEFINE_PROPS,
HELPER_PREFIX,
MagicString,
MagicStringAST,
generateTransform,
importHelperFn,
isCallOf,
Expand Down Expand Up @@ -36,7 +36,7 @@ export async function transformDefineProp(
const setupAst = sfc.getSetupAst()!

const offset = sfc.scriptSetup.loc.start.offset
const s = new MagicString(code)
const s = new MagicStringAST(code)

const { walkCall, genRuntimeProps } = (
edition === 'kevinEdition' ? kevinEdition : johnsonEdition
Expand Down
4 changes: 2 additions & 2 deletions packages/define-prop/src/core/utils.ts
@@ -1,8 +1,8 @@
import type { MagicString } from '@vue-macros/common'
import type { MagicStringAST } from '@vue-macros/common'
import type * as t from '@babel/types'

export type Impl = (ctx: {
s: MagicString
s: MagicStringAST
offset: number
resolveTSType: (type: t.TSType) => Promise<string[] | undefined>
}) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/define-props-refs/src/core/index.ts
Expand Up @@ -2,7 +2,7 @@ import {
DEFINE_PROPS,
DEFINE_PROPS_REFS,
HELPER_PREFIX,
MagicString,
MagicStringAST,
WITH_DEFAULTS,
generateTransform,
importHelperFn,
Expand All @@ -20,7 +20,7 @@ export function transformDefinePropsRefs(code: string, id: string) {
if (!scriptSetup) return

const offset = scriptSetup.loc.start.offset
const s = new MagicString(code)
const s = new MagicStringAST(code)
const setupAst = getSetupAst()!

walkAST<Node>(setupAst, {
Expand Down
4 changes: 2 additions & 2 deletions packages/define-props/src/core/index.ts
@@ -1,7 +1,7 @@
import {
DEFINE_PROPS,
DEFINE_PROPS_DOLLAR,
MagicString,
MagicStringAST,
generateTransform,
isCallOf,
parseSFC,
Expand All @@ -16,7 +16,7 @@ export function transformDefineProps(code: string, id: string) {
if (!scriptSetup) return

const offset = scriptSetup.loc.start.offset
const s = new MagicString(code)
const s = new MagicStringAST(code)
const setupAst = getSetupAst()!

walkAST<Node>(setupAst, {
Expand Down
4 changes: 2 additions & 2 deletions packages/define-render/src/core/index.ts
@@ -1,6 +1,6 @@
import {
DEFINE_RENDER,
MagicString,
MagicStringAST,
babelParse,
generateTransform,
getLang,
Expand Down Expand Up @@ -39,7 +39,7 @@ export function transformDefineRender(code: string, id: string) {
})
if (nodes.length === 0) return

const s = new MagicString(code)
const s = new MagicStringAST(code)

for (const { parent, node, arg } of nodes) {
// check parent
Expand Down
4 changes: 2 additions & 2 deletions packages/define-slots/src/core/index.ts
@@ -1,6 +1,6 @@
import {
DEFINE_SLOTS,
MagicString,
MagicStringAST,
generateTransform,
isCallOf,
parseSFC,
Expand All @@ -12,7 +12,7 @@ export function transformDefineSlots(code: string, id: string) {
const { scriptSetup, getSetupAst } = parseSFC(code, id)
if (!scriptSetup) return

const s = new MagicString(code)
const s = new MagicStringAST(code)

for (const stmt of getSetupAst()!.body) {
if (
Expand Down
4 changes: 2 additions & 2 deletions packages/export-expose/src/core/index.ts
@@ -1,6 +1,6 @@
import {
HELPER_PREFIX,
MagicString,
MagicStringAST,
generateTransform,
parseSFC,
} from '@vue-macros/common'
Expand All @@ -12,7 +12,7 @@ export function transformExportExpose(code: string, id: string) {
const { scriptSetup, getSetupAst } = parseSFC(code, id)
if (!scriptSetup) return

const s = new MagicString(code)
const s = new MagicStringAST(code)
const nodes = getSetupAst()!.body
const offset = scriptSetup.loc.start.offset

Expand Down
4 changes: 2 additions & 2 deletions packages/export-props/src/core/index.ts
@@ -1,6 +1,6 @@
import {
DEFINE_PROPS,
MagicString,
MagicStringAST,
WITH_DEFAULTS,
generateTransform,
isCallOf,
Expand All @@ -13,7 +13,7 @@ export function transformExportProps(code: string, id: string) {
if (!scriptSetup) return

const offset = scriptSetup.loc.start.offset
const s = new MagicString(code)
const s = new MagicStringAST(code)

const props: Record<string, { type: string; defaultValue?: string }> =
Object.create(null)
Expand Down
4 changes: 2 additions & 2 deletions packages/export-render/src/core/index.ts
@@ -1,10 +1,10 @@
import { MagicString, generateTransform, parseSFC } from '@vue-macros/common'
import { MagicStringAST, generateTransform, parseSFC } from '@vue-macros/common'

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

const s = new MagicString(code)
const s = new MagicStringAST(code)
const nodes = getSetupAst()!.body
const offset = scriptSetup.loc.start.offset

Expand Down

0 comments on commit 102a074

Please sign in to comment.