Skip to content

Commit

Permalink
refactor: switch to magic-string-ast
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Mar 23, 2023
1 parent 0d1f03d commit ec64836
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 65 deletions.
6 changes: 2 additions & 4 deletions packages/api/src/vue/props.ts
Expand Up @@ -209,11 +209,9 @@ export async function handleTSPropsDefinition({
case 'ObjectMethod':
return `${
defaultValue.kind !== 'method' ? `${defaultValue.kind} ` : ''
}${defaultValue.async ? `async ` : ''}${key}(${s.sliceNodes(
}${defaultValue.async ? `async ` : ''}${key}(${s.sliceNode(
defaultValue.params,
{
offset,
}
{ offset }
)}) ${s.sliceNode(defaultValue.body, { offset })}`
case 'ObjectProperty':
return `${key}: ${s.sliceNode(defaultValue.value, { offset })}`
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Expand Up @@ -55,7 +55,7 @@
"@rollup/pluginutils": "^5.0.2",
"@vue/compiler-sfc": "^3.2.47",
"local-pkg": "^0.4.3",
"magic-string": "^0.30.0"
"magic-string-ast": "^0.1.2"
},
"devDependencies": {
"@babel/parser": "^7.21.3",
Expand Down
3 changes: 2 additions & 1 deletion packages/common/src/index.ts
@@ -1,8 +1,9 @@
export * from 'magic-string-ast'

export * from './ast'
export * from './constants'
export * from './dep'
export * from './lang'
export * from './magic-string'
export * from './types'
export * from './unplugin'
export * from './vue'
42 changes: 0 additions & 42 deletions packages/common/src/magic-string.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/common/src/unplugin.ts
@@ -1,9 +1,9 @@
import { createFilter as createRollupFilter } from '@rollup/pluginutils'
import type MagicString from 'magic-string'
import type { MagicStringBase } from 'magic-string-ast'
import type { FilterPattern } from '@rollup/pluginutils'

export function getTransformResult(
s: MagicString | undefined,
s: MagicStringBase | undefined,
id: string
): { code: string; map: any } | undefined {
if (s?.hasChanged()) {
Expand Down
8 changes: 4 additions & 4 deletions packages/common/src/vue.ts
Expand Up @@ -3,7 +3,7 @@ import { babelParse } from './ast'
import { getLang } from './lang'
import { REGEX_VUE_SFC } from './constants'
import type { Program } from '@babel/types'
import type { MagicString } from './magic-string'
import type { MagicStringBase } from 'magic-string-ast'
import type {
SFCDescriptor,
SFCParseResult,
Expand Down Expand Up @@ -62,7 +62,7 @@ export function getFileCodeAndLang(
}
}

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

const imported = new WeakMap<MagicString, Set<string>>()
const imported = new WeakMap<MagicStringBase, Set<string>>()
export const HELPER_PREFIX = '__MACROS_'
export function importHelperFn(
s: MagicString,
s: MagicStringBase,
offset: number,
name: string,
from: string
Expand Down
2 changes: 1 addition & 1 deletion packages/define-model/package.json
Expand Up @@ -96,7 +96,7 @@
},
"dependencies": {
"@vue-macros/common": "workspace:~",
"ast-walker-scope": "^0.4.0",
"ast-walker-scope": "^0.4.1",
"unplugin": "^1.3.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/define-options/package.json
Expand Up @@ -88,7 +88,7 @@
},
"dependencies": {
"@vue-macros/common": "workspace:~",
"ast-walker-scope": "^0.4.0",
"ast-walker-scope": "^0.4.1",
"unplugin": "^1.3.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/short-emits/src/core/index.ts
Expand Up @@ -50,7 +50,7 @@ export function transformShortEmits(code: string, id: string) {
const s = new MagicString(code)

function stringifyParams(params: Array<Identifier | RestElement>) {
return params.length > 0 ? s.sliceNodes(params, { offset }) : ''
return params.length > 0 ? s.sliceNode(params, { offset }) : ''
}

for (const { def, type } of nodes) {
Expand Down
23 changes: 15 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ec64836

Please sign in to comment.