Skip to content

Commit

Permalink
[Fix] TSNonNullExpression: handle ThisExpressions
Browse files Browse the repository at this point in the history
Fixes #108
  • Loading branch information
ljharb committed Dec 16, 2020
1 parent 57463fa commit 3352ed4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Unreleased
==================
- [New] add support for fragment syntax (`<>`) (#108)
- [Fix] `TSNonNullExpression`: handle `ThisExpression`s (#108)

3.1.0 / 2020-10-13
==================
Expand Down
7 changes: 7 additions & 0 deletions __tests__/src/getPropLiteralValue-babelparser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,5 +518,12 @@ describe('getLiteralPropValue', () => {

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

it('should work with a this.props value', () => {
const prop = extractProp('<a href={this.props.href!}>Download</a>');
const expected = null;
const actual = getLiteralPropValue(prop);
assert.equal(actual, expected);
});
});
});
7 changes: 7 additions & 0 deletions __tests__/src/getPropValue-babelparser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1174,5 +1174,12 @@ describe('getPropValue', () => {
const actual = getPropValue(prop);
assert.equal(actual, expected);
});

it('should work with a this.props value', () => {
const prop = extractProp('<a foo={this.props.href!}>Download</a>');
const expected = 'this.props.href!';
const actual = getPropValue(prop);
assert.equal(actual, expected);
});
});
});
6 changes: 6 additions & 0 deletions src/values/expressions/TSNonNullExpression.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const extractValueFromThisExpression = require('./ThisExpression').default;

/**
* Extractor function for a TSNonNullExpression type value node.
* A TSNonNullExpression is accessing a TypeScript Non-Null Assertion
Expand All @@ -18,6 +20,10 @@ export default function extractValueFromTSNonNullExpression(value) {
return name;
}

if (value.type === 'ThisExpression') {
return extractValueFromThisExpression();
}

// does not contains properties & is not parenthesized
if (value.type === 'TSNonNullExpression' && (!value.extra || value.extra.parenthesized === false)) {
const { expression } = value;
Expand Down

0 comments on commit 3352ed4

Please sign in to comment.