Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#73] Account for SpreadElement AST Nodes #76

Merged
merged 1 commit into from
Mar 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions __tests__/src/getPropValue-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,15 @@ describe('getPropValue', () => {

assert.deepEqual(expected, actual);
});

it('should evaluate to a correct representation of an array with spread elements', () => {
const prop = extractProp('<div foo={[...this.props.params, bar]} />');

const expected = [undefined, 'bar'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there no way to represent this.props.params here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb I honestly can't think of one. The values of the array would be spread in, or no values would be spread in at all. I have to return something that can be evaluated in code. An empty array doesn't make sense, since spreading values into an array wouldn't produce an array (it would produce more values in the containing array). The best I could come up with is undefined. I'm up for alternatives if you have them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just wondering if maybe a special object indicating what was spread; that way a props-related rule could know that the "params" prop was used here (thinking of how this can be used in eslint-plugin-react)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb I don't think we'll need it in the eslint-plugin-jsx-a11y project. Once we encounter a spread, we can't statically analyze any further.

const actual = getPropValue(prop);

assert.deepEqual(expected, actual);
});
});

it('should return an empty array provided an empty array in props', () => {
Expand Down
11 changes: 11 additions & 0 deletions src/values/expressions/SpreadElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Extractor function for a SpreadElement type value node.
* We can't statically evaluate an array spread, so just return
* undefined.
*
* @param - value - AST Value object with type `SpreadElement`
* @returns - An prototypeless object.
*/
export default function extractValueFromSpreadElement() {
return undefined;
}
3 changes: 3 additions & 0 deletions src/values/expressions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import NewExpression from './NewExpression';
import UpdateExpression from './UpdateExpression';
import ArrayExpression from './ArrayExpression';
import BindExpression from './BindExpression';
import SpreadElement from './SpreadElement';

// Composition map of types to their extractor functions.
const TYPES = {
Expand All @@ -38,6 +39,7 @@ const TYPES = {
UpdateExpression,
ArrayExpression,
BindExpression,
SpreadElement,
};

const noop = () => null;
Expand Down Expand Up @@ -79,6 +81,7 @@ const LITERAL_TYPES = Object.assign({}, TYPES, {
return extractedVal.filter(val => val !== null);
},
BindExpression: noop,
SpreadElement: noop,
});

const errorMessage = expression =>
Expand Down