Skip to content

Commit

Permalink
[Bug] Fix #46
Browse files Browse the repository at this point in the history
  • Loading branch information
Siubaak committed May 4, 2020
1 parent 525507c commit e0b4c23
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/evaluate/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ export function* VariableDeclarator(
scope: Scope,
options: VariableDeclaratorOptions & VariableDeclarationOptions = {},
) {
const { kind = 'let', hoist = false, onlyBlock = false, feed } = options
const { kind = 'var', hoist = false, onlyBlock = false, feed } = options
if (hoist) {
if (onlyBlock || kind === 'var') {
if (node.id.type === 'Identifier') {
scope[onlyBlock ? kind : 'var'](node.id.name, onlyBlock ? DEADZONE : undefined)
scope[kind](node.id.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined)
} else {
yield* pattern(node.id, scope, { kind, hoist, onlyBlock })
}
Expand Down
4 changes: 2 additions & 2 deletions src/evaluate/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ export function createFunc(
for (let i = 0; i < params.length; i++) {
const param = params[i]
if (param.type === 'Identifier') {
subScope.let(param.name, args[i])
subScope.var(param.name, args[i])
} else if (param.type === 'RestElement') {
yield* RestElement(param, subScope, { kind: 'let', feed: args.slice(i) })
yield* RestElement(param, subScope, { kind: 'var', feed: args.slice(i) })
} else {
yield* pattern(param, subScope, { feed: args[i] })
}
Expand Down
14 changes: 7 additions & 7 deletions src/evaluate/pattern.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NOINIT, DEADZONE } from '../share/const'
import { VarKind } from '../scope/variable'
import { Identifier } from './identifier'
import { DEADZONE } from '../share/const'
import { assign } from '../share/util'
import { pattern } from './helper'
import * as estree from 'estree'
Expand All @@ -15,7 +15,7 @@ export interface PatternOptions {
}

export function* ObjectPattern(node: estree.ObjectPattern, scope: Scope, options: PatternOptions = {}) {
const { kind = 'let', hoist = false, onlyBlock = false, feed = {} } = options
const { kind = 'var', hoist = false, onlyBlock = false, feed = {} } = options
const fedKeys: string[] = []
for (let i = 0; i < node.properties.length; i++) {
const property = node.properties[i]
Expand All @@ -24,7 +24,7 @@ export function* ObjectPattern(node: estree.ObjectPattern, scope: Scope, options
if (property.type === 'Property') {
const value = property.value
if (value.type === 'Identifier') {
scope[onlyBlock ? kind : 'var'](value.name, onlyBlock ? DEADZONE : undefined)
scope[kind](value.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined)
} else {
yield* pattern(value, scope, { kind, hoist, onlyBlock })
}
Expand Down Expand Up @@ -64,7 +64,7 @@ export function* ArrayPattern(node: estree.ArrayPattern, scope: Scope, options:
if (hoist) {
if (onlyBlock || kind === 'var') {
if (element.type === 'Identifier') {
scope[onlyBlock ? kind : 'var'](element.name, onlyBlock ? DEADZONE : undefined)
scope[kind](element.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined)
} else {
yield* pattern(element, scope, { kind, hoist, onlyBlock })
}
Expand Down Expand Up @@ -96,7 +96,7 @@ export function* RestElement(node: estree.RestElement, scope: Scope, options: Pa
if (hoist) {
if (onlyBlock || kind === 'var') {
if (arg.type === 'Identifier') {
scope[onlyBlock ? kind : 'var'](arg.name, onlyBlock ? DEADZONE : undefined)
scope[kind](arg.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined)
} else {
yield* pattern(arg, scope, { kind, hoist, onlyBlock })
}
Expand All @@ -116,12 +116,12 @@ export function* RestElement(node: estree.RestElement, scope: Scope, options: Pa
}

export function* AssignmentPattern(node: estree.AssignmentPattern, scope: Scope, options: PatternOptions = {}) {
const { kind = 'let', hoist = false, onlyBlock = false, feed = yield* evaluate(node.right, scope) } = options
const { kind = 'var', hoist = false, onlyBlock = false, feed = yield* evaluate(node.right, scope) } = options
const left = node.left
if (hoist) {
if (onlyBlock || kind === 'var') {
if (left.type === 'Identifier') {
scope[onlyBlock ? kind : 'var'](left.name, onlyBlock ? DEADZONE : undefined)
scope[kind](left.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined)
} else {
yield* pattern(left, scope, { kind, hoist, onlyBlock })
}
Expand Down
2 changes: 1 addition & 1 deletion src/evaluate/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function* TryStatement(node: estree.TryStatement, scope: Scope) {
if (param) {
if (param.type === 'Identifier') {
const name = param.name
subScope.let(name, err)
subScope.var(name, err)
} else {
yield* pattern(param, scope, { feed: err })
}
Expand Down
12 changes: 12 additions & 0 deletions tests/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ describe('testing src/index.ts', () => {

expect(interpreter.exports.x).toBeTruthy()
})
it('should redeclare param inside function by var', () => {
const interpreter = new Sval()
interpreter.run(`
a(1)
function a(b) {
var b = b + 1
exports.b = b
}
`)

expect(interpreter.exports.b).toBe(2)
})

it('should yield generator normally', () => {
const interpreter = new Sval()
Expand Down

0 comments on commit e0b4c23

Please sign in to comment.