Skip to content

More robust no typos fix #1375

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

Merged
merged 6 commits into from
Aug 21, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 0 additions & 4 deletions lib/rules/no-typos.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ module.exports = {
},

MemberExpression: function(node) {
if (node.parent.type !== 'AssignmentExpression') {
return;
}

const relatedComponent = utils.getRelatedComponent(node);

if (
Expand Down
10 changes: 9 additions & 1 deletion lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,15 @@ function componentRule(rule, context) {
* @returns {Boolean} True if the node is explicitly declared as a descendant of a React Component, false if not
*/
isExplicitComponent: function(node) {
const comment = sourceCode.getJSDocComment(node);
let comment;
// Sometimes the passed node may not have been parsed yet by eslint, and this function call crashes.
// Can be removed when eslint sets "parent" property for all nodes on initial AST traversal: https://github.com/eslint/eslint-scope/issues/27
Copy link
Member

Choose a reason for hiding this comment

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

What about changing the places calling this method to be ":exit"?

Copy link
Contributor Author

@jseminck jseminck Aug 16, 2017

Choose a reason for hiding this comment

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

It does not help. The relatedComponent (in the case below it is class MyComponent) is not a child in the AST of the MemberExpression "MyComponent.PROPTYPES" - so using :exit does not work.

The reason is just that class MyComponent is declared after MyComponent.PROPTYPES and therefore eslint has not yet visited it, and therefore it does not have the parent property yet (which getJSDocComment() expects to exist).

      MyComponent.PROPTYPES = {}
      /** @extends React.Component */
      class MyComponent extends BaseComponent {}

They have a task to add parent property to all nodes on initial traversal, that would fix our issue.

// Remove try/catch when https://github.com/eslint/eslint-scope/issues/27 is implemented.
try {
comment = sourceCode.getJSDocComment(node);
} catch (e) {
comment = null;
}

if (comment === null) {
return false;
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/rules/no-typos.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ ruleTester.run('no-typos', rule, {
'}'
].join('\n'),
parserOptions: parserOptions
}, {
// PropTypes declared on a component that is detected through JSDoc comments and is
// declared AFTER the PropTypes assignment does not work.
code: `
MyComponent.PROPTYPES = {}
/** @extends React.Component */
class MyComponent extends BaseComponent {}
`,
parserOptions: parserOptions
}, {
// https://github.com/yannickcr/eslint-plugin-react/issues/1353
code: `
Expand Down Expand Up @@ -435,6 +444,21 @@ ruleTester.run('no-typos', rule, {
].join('\n'),
parserOptions: parserOptions,
errors: [{message: ERROR_MESSAGE}]
}, {
code: [
'Component.defaultprops = {}',
'class Component extends React.Component {}'
].join('\n'),
parserOptions: parserOptions,
errors: [{message: ERROR_MESSAGE}]
}, {
code: `
/** @extends React.Component */
class MyComponent extends BaseComponent {}
MyComponent.PROPTYPES = {}
`,
parserOptions: parserOptions,
errors: [{message: ERROR_MESSAGE}]
}, {
code: [
'class Hello extends React.Component {',
Expand Down