Skip to content

Commit

Permalink
refactor(common): reuse resolveObjectKey from ast-kit
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jul 9, 2023
1 parent b2e3248 commit c107e44
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 62 deletions.
10 changes: 10 additions & 0 deletions .changeset/breezy-vans-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@vue-macros/common': minor
'@vue-macros/reactivity-transform': patch
'@vue-macros/define-models': patch
'@vue-macros/short-emits': patch
'@vue-macros/test-utils': patch
'@vue-macros/api': patch
---

refactor: reuse resolveObjectKey from ast-kit
2 changes: 1 addition & 1 deletion packages/api/src/ts/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export function resolveTypeElements(

const tryGetKey = (element: TSMethodSignature | TSPropertySignature) => {
try {
return resolveObjectKey(element.key, element.computed, false)
return resolveObjectKey(element)
} catch {}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@
"@babel/types": "^7.22.5",
"@rollup/pluginutils": "^5.0.2",
"@vue/compiler-sfc": "^3.3.4",
"ast-kit": "^0.6.6",
"ast-kit": "^0.6.7",
"local-pkg": "^0.4.3",
"magic-string-ast": "^0.1.2"
"magic-string-ast": "^0.1.3"
},
"devDependencies": {
"@babel/parser": "^7.22.7"
Expand Down
44 changes: 8 additions & 36 deletions packages/common/src/ast.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { walkIdentifiers } from '@vue/compiler-sfc'
import {
type Node,
type ObjectExpression,
type ObjectMethod,
type ObjectProperty,
} from '@babel/types'
import { type MagicStringBase } from 'magic-string-ast'
import { isFunctionType, isLiteralType } from 'ast-kit'
import { isFunctionType, isLiteralType, resolveObjectKey } from 'ast-kit'
import type * as t from '@babel/types'

export function checkInvalidScopeReference(
node: Node | undefined,
node: t.Node | undefined,
method: string,
setupBindings: string[]
) {
Expand All @@ -25,7 +20,7 @@ export function checkInvalidScopeReference(
}

export function isStaticExpression(
node: Node,
node: t.Node,
options: Partial<
Record<
'object' | 'fn' | 'objectMethod' | 'array' | 'unary' | 'regex',
Expand Down Expand Up @@ -117,7 +112,7 @@ export function isStaticExpression(
return false
}

export function isStaticObjectKey(node: ObjectExpression): boolean {
export function isStaticObjectKey(node: t.ObjectExpression): boolean {
return node.properties.every((prop) => {
if (prop.type === 'SpreadElement') {
return (
Expand All @@ -132,46 +127,23 @@ export function isStaticObjectKey(node: ObjectExpression): boolean {
/**
* @param node must be a static expression, SpreadElement is not supported
*/
export function resolveObjectExpression(node: ObjectExpression) {
const maps: Record<string | number, ObjectMethod | ObjectProperty> = {}
export function resolveObjectExpression(node: t.ObjectExpression) {
const maps: Record<string | number, t.ObjectMethod | t.ObjectProperty> = {}
for (const property of node.properties) {
if (property.type === 'SpreadElement') {
if (property.argument.type !== 'ObjectExpression')
// not supported
return undefined
Object.assign(maps, resolveObjectExpression(property.argument)!)
} else {
const key = resolveObjectKey(property.key, property.computed, false)
const key = resolveObjectKey(property)
maps[key] = property
}
}

return maps
}

export function resolveObjectKey(
node: Node,
computed?: boolean,
raw?: true
): string
export function resolveObjectKey(
node: Node,
computed: boolean | undefined,
raw: false
): string | number
export function resolveObjectKey(node: Node, computed = false, raw = true) {
switch (node.type) {
case 'StringLiteral':
case 'NumericLiteral':
return raw ? node.extra!.raw : node.value
case 'Identifier':
if (!computed) return raw ? `'${node.name}'` : node.name
// break omitted intentionally
default:
throw new SyntaxError(`Unexpected node type: ${node.type}`)
}
}

const importedMap = new WeakMap<MagicStringBase, Set<string>>()
export const HELPER_PREFIX = '__MACROS_'
export function importHelperFn(
Expand Down
2 changes: 1 addition & 1 deletion packages/define-models/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ export function transformDefineModels(
for (const p of modelDestructureDecl.properties) {
if (p.type !== 'ObjectProperty') continue
try {
const key = resolveObjectKey(p.key, p.computed, false)
const key = resolveObjectKey(p)
if (p.value.type !== 'Identifier') continue
aliasMap[p.value.name] = key
} catch {}
Expand Down
2 changes: 1 addition & 1 deletion packages/reactivity-transform/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function transformVueSFC(code: string, id: string) {
propsDestructuredBindings = {}
for (const prop of decl.id.properties) {
if (prop.type === 'ObjectProperty') {
const propKey = resolveObjectKey(prop.key, prop.computed, false)
const propKey = resolveObjectKey(prop)

if (!propKey) {
throw new SyntaxError(
Expand Down
2 changes: 1 addition & 1 deletion packages/short-emits/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function transformShortEmits(code: string, id: string) {
if (!isTypeOf(member, ['TSPropertySignature', 'TSMethodSignature']))
continue

const key = resolveObjectKey(member.key, member.computed)
const key = resolveObjectKey(member, true)
let params = ''

switch (member.type) {
Expand Down
12 changes: 6 additions & 6 deletions packages/short-emits/tests/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ exports[`fixtures > ./fixtures/basic.vue 1`] = `
const emit = defineEmits<
{
// TSPropertySignature
(evt: 'input', ...args: []): void
(evt: \\"input\\", ...args: []): void
(evt: 'update:modelValue', ...args: [val: string]): void
(evt: 'update:color', ...args: [val: number]): void
(evt: 'change', value: boolean): void
(evt: \\"change\\", value: boolean): void
// TSMethodSignature
(evt: 'update'): void
(evt: \\"update\\"): void
(evt: 'foo\\"\\\\'', value: boolean): void
Expand All @@ -35,13 +35,13 @@ import { defineEmits } from '../../macros' assert { type: 'macro' }
const emit = defineEmits<{
// TSPropertySignature
(evt: 'input', ...args: []): void
(evt: \\"input\\", ...args: []): void
(evt: 'update:modelValue', ...args: [val: string]): void
(evt: 'update:color', ...args: [val: number]): void
(evt: 'change', value: boolean): void
(evt: \\"change\\", value: boolean): void
// TSMethodSignature
(evt: 'update'): void
(evt: \\"update\\"): void
(evt: 'foo\\"\\\\'', value: boolean): void
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"fast-glob": "^3.3.0",
"rollup": "^3.26.2",
"rollup-plugin-esbuild": "^5.0.0",
"unplugin-vue": "^4.2.4"
"unplugin-vue": "^4.2.5"
},
"devDependencies": {
"@rollup/plugin-json": "^6.0.0",
Expand Down
26 changes: 13 additions & 13 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 c107e44

Please sign in to comment.