-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(compiler): extract shared ast transform utils
Also improve referenced identifier check using isReferenced from @babel/types
- Loading branch information
Showing
5 changed files
with
105 additions
and
174 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
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,72 @@ | ||
import { | ||
Identifier, | ||
Node, | ||
isReferenced, | ||
Function, | ||
ObjectProperty | ||
} from '@babel/types' | ||
|
||
export function isReferencedIdentifier( | ||
id: Identifier, | ||
parent: Node | null, | ||
parentStack: Node[] | ||
) { | ||
if (!parent) { | ||
return true | ||
} | ||
|
||
// is a special keyword but parsed as identifier | ||
if (id.name === 'arguments') { | ||
return false | ||
} | ||
|
||
if (isReferenced(id, parent)) { | ||
return true | ||
} | ||
|
||
// babel's isReferenced check returns false for ids being assigned to, so we | ||
// need to cover those cases here | ||
switch (parent.type) { | ||
case 'AssignmentExpression': | ||
case 'AssignmentPattern': | ||
return true | ||
case 'ObjectPattern': | ||
case 'ArrayPattern': | ||
return isInDestructureAssignment(parent, parentStack) | ||
} | ||
|
||
return false | ||
} | ||
|
||
export function isInDestructureAssignment( | ||
parent: Node, | ||
parentStack: Node[] | ||
): boolean { | ||
if ( | ||
parent && | ||
(parent.type === 'ObjectProperty' || parent.type === 'ArrayPattern') | ||
) { | ||
let i = parentStack.length | ||
while (i--) { | ||
const p = parentStack[i] | ||
if (p.type === 'AssignmentExpression') { | ||
return true | ||
} else if (p.type !== 'ObjectProperty' && !p.type.endsWith('Pattern')) { | ||
break | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
export const isFunctionType = (node: Node): node is Function => { | ||
return /Function(?:Expression|Declaration)$|Method$/.test(node.type) | ||
} | ||
|
||
export const isStaticProperty = (node: Node): node is ObjectProperty => | ||
node && | ||
(node.type === 'ObjectProperty' || node.type === 'ObjectMethod') && | ||
!node.computed | ||
|
||
export const isStaticPropertyKey = (node: Node, parent: Node) => | ||
isStaticProperty(parent) && parent.key === node |
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