Skip to content

Commit

Permalink
Fix require-default-props quoted defaultProps detection (fix #1201)
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed May 17, 2017
1 parent 62daf82 commit 45e3b9f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/rules/require-default-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ var Components = require('../util/Components');
var variableUtil = require('../util/variable');
var annotations = require('../util/annotations');

const QUOTES_REGEX = /^["']|["']$/g;

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
Expand All @@ -25,6 +27,8 @@ module.exports = {

create: Components.detect(function(context, components, utils) {

var sourceCode = context.getSourceCode();

/**
* Get properties name
* @param {Object} node - Property.
Expand Down Expand Up @@ -142,7 +146,7 @@ module.exports = {

return props.map(function(property) {
return {
name: property.key.name || property.key.value,
name: sourceCode.getText(property.key).replace(QUOTES_REGEX, ''),
isRequired: isRequiredPropType(property.value),
node: property
};
Expand Down Expand Up @@ -222,7 +226,7 @@ module.exports = {
}

return objectExpression.properties.map(function(property) {
return property.key.name;
return sourceCode.getText(property.key).replace(QUOTES_REGEX, '');
});
}

Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/require-default-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,21 @@ ruleTester.run('require-default-props', rule, {
'}'
].join('\n'),
parser: 'babel-eslint'
},
// make sure defaultProps are correctly detected with quoted properties
{
code: [
'function Hello(props) {',
' return <div>Hello {props.bar}</div>;',
'}',
'Hello.propTypes = {',
' bar: PropTypes.string',
'};',
'Hello.defaultProps = {',
' "bar": "bar"',
'};'
].join('\n'),
parser: 'babel-eslint'
}
],

Expand Down

0 comments on commit 45e3b9f

Please sign in to comment.