Skip to content

Commit

Permalink
fixed: issue with object expressions scope
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaGuarini committed Jan 6, 2019
1 parent 9a0bb0d commit bba08ad
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/generators/template/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
isBuiltinAPI,
isExpressionStatement,
isIdentifier,
isObjectExpression,
isRaw,
isSequenceExpression,
isThisExpression
} from '../../utils/ast-nodes-checks'
Expand Down Expand Up @@ -80,7 +80,7 @@ export function createExpressionSourcemap(expression, sourceFile, sourceCode) {
* @returns {boolean} true if it's a global api variable
*/
export function isGlobal({ scope, node }) {
return isBuiltinAPI(node) || isBrowserAPI(node) || scope.lookup(getName(node))
return isRaw(node) || isBuiltinAPI(node) || isBrowserAPI(node) || scope.lookup(getName(node))
}

/**
Expand Down Expand Up @@ -118,14 +118,15 @@ function updateNodeScope(path) {
* @param { types.NodePath } path - containing the current node visited
* @returns { boolean } return always false because we want to check only the first node object
*/
function updateMemberExpressions(path) {
function visitMemberExpression(path) {
if (!isGlobal({ node: path.node.object, scope: path.scope })) {
replacePathScope(path, isThisExpression(path.node.object) ? path.node.property : path.node)
}

return false
}


/**
* Objects properties should be handled a bit differently from the Identifier
* @param { types.NodePath } path - containing the current node visited
Expand All @@ -136,7 +137,7 @@ function visitProperty(path) {

if (isIdentifier(value)) {
updateNodeScope(path.get('value'))
} else if (isObjectExpression(value)) {
} else {
this.traverse(path.get('value'))
}

Expand All @@ -163,7 +164,7 @@ export function updateNodesScope(ast) {

types.visit(ast, {
visitIdentifier: updateNodeScope,
visitMemberExpression: updateMemberExpressions,
visitMemberExpression,
visitProperty,
visitThisExpression,
visitClassExpression: ignorePath
Expand Down
3 changes: 2 additions & 1 deletion src/utils/ast-nodes-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export const isSequenceExpression = namedTypes.SequenceExpression.check
export const isBinaryExpression = namedTypes.BinaryExpression.check

export const isBrowserAPI = ({name}) => browserAPIs.includes(name)
export const isBuiltinAPI = ({name}) => builtinAPIs.includes(name)
export const isBuiltinAPI = ({name}) => builtinAPIs.includes(name)
export const isRaw = (node) => node && node.raw
1 change: 1 addition & 0 deletions test/generators/template.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ describe('Generators - Template', () => {

it('objects', () => {
expect(renderExpr('{ foo: bar, buz: baz }')).to.be.equal('{ foo: scope.bar, buz: scope.baz }')
expect(renderExpr('{ foo: i%2 }')).to.be.equal('{ foo: scope.i%2 }')
expect(renderExpr('{ foo: { foo: bar, buz: baz }, buz: baz }')).to.be.equal('{ foo: { foo: scope.bar, buz: scope.baz }, buz: scope.baz }')
})

Expand Down

0 comments on commit bba08ad

Please sign in to comment.