-
-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Remove building out of scope AST Nodes from resolveToValue
BREAKING CHANGE: `resolveToValue` will not create a `MemberExpression` for targets ending in destructuring. It will now simply resolve to the `Identifier` inside the destructuring. Use new helper `isDestructuringAssignment` to further check this identifier.
- Loading branch information
Showing
8 changed files
with
86 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { parse } from '../../../tests/utils'; | ||
import isDestructuringAssignment from '../isDestructuringAssignment'; | ||
|
||
describe('isDestructuringAssignment', () => { | ||
it('detects destructuring', () => { | ||
const def = parse(` | ||
var { Component } = require('react'); | ||
`).get('body', 0, 'declarations', 0, 'id', 'properties', 0, 'key'); | ||
|
||
expect(isDestructuringAssignment(def, 'Component')).toBe(true); | ||
}); | ||
|
||
it('fails if name does not match', () => { | ||
const def = parse(` | ||
var { Component } = require('react'); | ||
`).get('body', 0, 'declarations', 0, 'id', 'properties', 0, 'key'); | ||
|
||
expect(isDestructuringAssignment(def, 'Component2')).toBe(false); | ||
}); | ||
|
||
it('detects destructuring with alias', () => { | ||
const def = parse(` | ||
var { Component: C } = require('react'); | ||
`).get('body', 0, 'declarations', 0, 'id', 'properties', 0, 'value'); | ||
|
||
expect(isDestructuringAssignment(def, 'Component')).toBe(true); | ||
}); | ||
|
||
it('fails if name does not match with alias', () => { | ||
const def = parse(` | ||
var { Component: C } = require('react'); | ||
`).get('body', 0, 'declarations', 0, 'id', 'properties', 0, 'value'); | ||
|
||
expect(isDestructuringAssignment(def, 'Component2')).toBe(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
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,18 @@ | ||
import { namedTypes as t } from 'ast-types'; | ||
import type { NodePath } from 'ast-types/lib/node-path'; | ||
|
||
/** | ||
* Checks if the input Identifier is part of a destructuring Assignment | ||
* and the name of the property key matches the input name | ||
*/ | ||
export default function isDestructuringAssignment( | ||
path: NodePath, | ||
name: string, | ||
): boolean { | ||
return ( | ||
t.Identifier.check(path.node) && | ||
t.Property.check(path.parentPath.node) && | ||
path.parentPath.node.key.name === name && | ||
t.ObjectPattern.check(path.parentPath.parentPath.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
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