Skip to content

Commit

Permalink
[fix] enhance behaviour of spread element
Browse files Browse the repository at this point in the history
  • Loading branch information
Siubaak committed Feb 2, 2025
1 parent b7ce7e6 commit a764bdf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/evaluate/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function* ObjectExpression(node: acorn.ObjectExpression, scope: Scope) {
for (let i = 0; i < node.properties.length; i++) {
const property = node.properties[i]
if (property.type === 'SpreadElement') {
assign(object, yield* SpreadElement(property, scope))
assign(object, yield* SpreadElement(property, scope, { spreadProps: true }))
} else {
let key: string
const propKey = property.key
Expand Down Expand Up @@ -542,19 +542,19 @@ export interface SuperOptions {
getProto?: boolean
}

export function* Super(
node: acorn.Super,
scope: Scope,
options: SuperOptions = {},
) {
export function* Super(node: acorn.Super, scope: Scope, options: SuperOptions = {}) {
const { getProto = false } = options
const superClass = scope.find(SUPER).get()
return getProto ? superClass.prototype: superClass
}

export function* SpreadElement(node: acorn.SpreadElement, scope: Scope) {
export interface SpreadOptions {
spreadProps?: boolean
}

export function* SpreadElement(node: acorn.SpreadElement, scope: Scope, options: SpreadOptions = {}) {
const result = yield* evaluate(node.argument, scope)
return typeof result === 'string' ? [...result] : result;
return options.spreadProps ? result : [...result]
}

export function* ChainExpression(node: acorn.ChainExpression, scope: Scope) {
Expand Down
4 changes: 3 additions & 1 deletion tests/expression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,20 @@ describe('testing src/expression.ts', () => {
const arr = [1, 2]
exports.a = [...arr]
exports.b = [...[1, 2, 3]]
f(...arr)
function f(m, n) {
exports.c = m
exports.d = n
exports.f = [...arguments]
}
`)

expect(interpreter.exports.a).toEqual([1, 2])
expect(interpreter.exports.b).toEqual([1, 2, 3])
expect(interpreter.exports.c).toBe(1)
expect(interpreter.exports.d).toBe(2)
expect(interpreter.exports.f).toEqual([1, 2])
})

it('should parse regular expression normally', () => {
Expand Down

0 comments on commit a764bdf

Please sign in to comment.