Skip to content

Commit

Permalink
[New] add AssignmentExpression
Browse files Browse the repository at this point in the history
Fixes #42
  • Loading branch information
ljharb committed Oct 14, 2020
1 parent 3f636f5 commit 4576cde
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
20 changes: 20 additions & 0 deletions __tests__/src/getPropValue-babelparser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,26 @@ describe('getPropValue', () => {
});
});

describe('AssignmentExpression', () => {
it('should recognize and extract assignment', () => {
const prop = extractProp('<div foo={foo = bar} />');

const expected = 'foo = bar';
const actual = getPropValue(prop);

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

it('should recognize and extract combination assignments', () => {
const prop = extractProp('<div foo={foo += bar} />');

const expected = 'foo += bar';
const actual = getPropValue(prop);

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

describeIfNotBabylon('Typescript', () => {
beforeEach(() => {
changePlugins((pls) => [...pls, 'typescript']);
Expand Down
13 changes: 13 additions & 0 deletions src/values/expressions/AssignmentExpression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Extractor function for a AssignmentExpression type value node.
* An assignment expression looks like `x = y` or `x += y` in expression position.
* This will return the assignment as the value.
*
* @param - value - AST Value object with type `AssignmentExpression`
* @returns - The extracted value converted to correct type.
*/
export default function extractValueFromAssignmentExpression(value) {
// eslint-disable-next-line global-require
const getValue = require('./index.js').default;
return `${getValue(value.left)} ${value.operator} ${getValue(value.right)}`;
}
2 changes: 2 additions & 0 deletions src/values/expressions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import SpreadElement from './SpreadElement';
import TypeCastExpression from './TypeCastExpression';
import SequenceExpression from './SequenceExpression';
import TSNonNullExpression from './TSNonNullExpression';
import AssignmentExpression from './AssignmentExpression';

// Composition map of types to their extractor functions.
const TYPES = {
Expand Down Expand Up @@ -52,6 +53,7 @@ const TYPES = {
TypeCastExpression,
SequenceExpression,
TSNonNullExpression,
AssignmentExpression,
};

const noop = () => null;
Expand Down

0 comments on commit 4576cde

Please sign in to comment.