Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(espower): extract and export traversal rules
- Loading branch information
Showing
5 changed files
with
93 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
var estraverse = require('estraverse'), | ||
syntax = estraverse.Syntax; | ||
|
||
module.exports = [ | ||
syntax.Identifier, | ||
syntax.MemberExpression, | ||
syntax.CallExpression, | ||
syntax.UnaryExpression, | ||
syntax.BinaryExpression, | ||
syntax.LogicalExpression, | ||
syntax.AssignmentExpression, | ||
syntax.ObjectExpression, | ||
syntax.NewExpression, | ||
syntax.ArrayExpression, | ||
syntax.ConditionalExpression, | ||
syntax.UpdateExpression, | ||
syntax.Property | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
var estraverse = require('estraverse'), | ||
syntax = estraverse.Syntax; | ||
|
||
module.exports = function toBeCaptured (currentNode) { | ||
switch(currentNode.type) { | ||
case syntax.Identifier: | ||
case syntax.MemberExpression: | ||
case syntax.CallExpression: | ||
case syntax.UnaryExpression: | ||
case syntax.BinaryExpression: | ||
case syntax.LogicalExpression: | ||
case syntax.AssignmentExpression: | ||
case syntax.UpdateExpression: | ||
case syntax.NewExpression: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
|
||
var estraverse = require('estraverse'), | ||
syntax = estraverse.Syntax, | ||
supportedNodeTypes = require('./supported-node-types'); | ||
|
||
function isLeftHandSideOfAssignment(parentNode, currentPath) { | ||
// Do not instrument left due to 'Invalid left-hand side in assignment' | ||
return parentNode.type === syntax.AssignmentExpression && currentPath === 'left'; | ||
} | ||
|
||
function isObjectLiteralKey(parentNode, currentPath) { | ||
// Do not instrument Object literal key | ||
return parentNode.type === syntax.Property && parentNode.kind === 'init' && currentPath === 'key'; | ||
} | ||
|
||
function isUpdateExpression(parentNode) { | ||
// Just wrap UpdateExpression, not digging in. | ||
return parentNode.type === syntax.UpdateExpression; | ||
} | ||
|
||
function isCallExpressionWithNonComputedMemberExpression(currentNode, parentNode, currentPath) { | ||
// Do not instrument non-computed property of MemberExpression within CallExpression. | ||
return currentNode.type === syntax.Identifier && parentNode.type === syntax.MemberExpression && !parentNode.computed && currentPath === 'property'; | ||
} | ||
|
||
function isTypeOfOrDeleteUnaryExpression(currentNode, parentNode, currentPath) { | ||
// 'typeof Identifier' or 'delete Identifier' is not instrumented | ||
return currentNode.type === syntax.Identifier && parentNode.type === syntax.UnaryExpression && (parentNode.operator === 'typeof' || parentNode.operator === 'delete') && currentPath === 'argument'; | ||
} | ||
|
||
function isSupportedNodeType (node) { | ||
return supportedNodeTypes.indexOf(node.type) !== -1; | ||
} | ||
|
||
module.exports = function toBeSkipped (currentNode, parentNode, currentPath) { | ||
return !isSupportedNodeType(currentNode) || | ||
isLeftHandSideOfAssignment(parentNode, currentPath) || | ||
isObjectLiteralKey(parentNode, currentPath) || | ||
isUpdateExpression(parentNode) || | ||
isCallExpressionWithNonComputedMemberExpression(currentNode, parentNode, currentPath) || | ||
isTypeOfOrDeleteUnaryExpression(currentNode, parentNode, currentPath); | ||
}; |