Skip to content

Commit

Permalink
Remove babel-eslint workarounds for ClassProperty
Browse files Browse the repository at this point in the history
Since the oldest supported version of `babel-eslint` (6.x) has the
key property on ClassProperty nodes, the workarounds aren't needed.
  • Loading branch information
jomasti committed Nov 9, 2017
1 parent f36b0fa commit 4e21d41
Show file tree
Hide file tree
Showing 11 changed files with 6 additions and 79 deletions.
8 changes: 1 addition & 7 deletions lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,12 @@ module.exports = {
* @returns {Boolean} True if node is `propTypes` declaration, false if not.
*/
function isPropTypesDeclaration(node) {
// Special case for class properties
// (babel-eslint does not expose property name so we have to rely on tokens)
if (node && node.type === 'ClassProperty') {
const tokens = context.getFirstTokens(node, 2);
if (tokens[0].value === 'propTypes' || (tokens[1] && tokens[1].value === 'propTypes')) {
return true;
}
// Flow support
if (node.typeAnnotation && node.key.name === 'props') {
return true;
}
return false;
node = node.key;
}

return Boolean(
Expand Down
5 changes: 0 additions & 5 deletions lib/rules/default-props-match-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ module.exports = {
return node.key.name;
} else if (node.type === 'MemberExpression') {
return node.property.name;
// Special case for class properties
// (babel-eslint@5 does not expose property name so we have to rely on tokens)
} else if (node.type === 'ClassProperty') {
const tokens = context.getFirstTokens(node, 2);
return tokens[1] && tokens[1].type === 'Identifier' ? tokens[1].value : tokens[0].value;
}
return '';
}
Expand Down
12 changes: 1 addition & 11 deletions lib/rules/display-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ module.exports = {
},

create: Components.detect((context, components, utils) => {
const sourceCode = context.getSourceCode();
const config = context.options[0] || {};
const ignoreTranspilerName = config.ignoreTranspilerName || false;

Expand All @@ -44,17 +43,8 @@ module.exports = {
*/
function isDisplayNameDeclaration(node) {
switch (node.type) {
// Special case for class properties
// (babel-eslint does not expose property name so we have to rely on tokens)
case 'ClassProperty':
const tokens = sourceCode.getFirstTokens(node, 2);
if (
tokens[0].value === 'displayName' ||
(tokens[1] && tokens[1].value === 'displayName')
) {
return true;
}
return false;
return node.key && node.key.name === 'displayName';
case 'Identifier':
return node.name === 'displayName';
case 'Literal':
Expand Down
8 changes: 1 addition & 7 deletions lib/rules/forbid-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,8 @@ module.exports = {
* @returns {Boolean} True if node is `propTypes` declaration, false if not.
*/
function isPropTypesDeclaration(node) {
// Special case for class properties
// (babel-eslint does not expose property name so we have to rely on tokens)
if (node.type === 'ClassProperty') {
const tokens = context.getFirstTokens(node, 2);
if (tokens[0].value === 'propTypes' || (tokens[1] && tokens[1].value === 'propTypes')) {
return true;
}
return false;
node = node.key;
}

return Boolean(
Expand Down
5 changes: 0 additions & 5 deletions lib/rules/no-redundant-should-component-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ module.exports = {
function getPropertyName(node) {
if (node.key) {
return node.key.name;
} else if (node.type === 'ClassProperty') {
// Special case for class properties
// (babel-eslint does not expose property name so we have to rely on tokens)
const tokens = context.getFirstTokens(node, 2);
return tokens[1] && tokens[1].type === 'Identifier' ? tokens[1].value : tokens[0].value;
}
return '';
}
Expand Down
11 changes: 1 addition & 10 deletions lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,8 @@ module.exports = {
* @returns {Boolean} True if we are declaring a prop, false if not.
*/
function isPropTypesDeclaration(node) {
// Special case for class properties
// (babel-eslint does not expose property name so we have to rely on tokens)
if (node && node.type === 'ClassProperty') {
const tokens = context.getFirstTokens(node, 2);
if (
tokens[0].value === 'propTypes' ||
(tokens[1] && tokens[1].value === 'propTypes')
) {
return true;
}
return false;
node = node.key;
}

return Boolean(
Expand Down
7 changes: 0 additions & 7 deletions lib/rules/prefer-stateless-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ module.exports = {
* @returns {String} Property name.
*/
function getPropertyName(node) {
// Special case for class properties
// (babel-eslint does not expose property name so we have to rely on tokens)
if (node.type === 'ClassProperty') {
const tokens = context.getFirstTokens(node, 2);
return tokens[1] && tokens[1].type === 'Identifier' ? tokens[1].value : tokens[0].value;
}

return node.key.name;
}

Expand Down
11 changes: 1 addition & 10 deletions lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,8 @@ module.exports = {
* @returns {Boolean} True if we are declaring a prop, false if not.
*/
function isPropTypesDeclaration(node) {
// Special case for class properties
// (babel-eslint does not expose property name so we have to rely on tokens)
if (node && node.type === 'ClassProperty') {
const tokens = context.getFirstTokens(node, 2);
if (
tokens[0].value === 'propTypes' ||
(tokens[1] && tokens[1].value === 'propTypes')
) {
return true;
}
return false;
node = node.key;
}

return Boolean(
Expand Down
5 changes: 0 additions & 5 deletions lib/rules/require-default-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ module.exports = {
return node.key.name;
} else if (node.type === 'MemberExpression') {
return node.property.name;
// Special case for class properties
// (babel-eslint@5 does not expose property name so we have to rely on tokens)
} else if (node.type === 'ClassProperty') {
const tokens = context.getFirstTokens(node, 2);
return tokens[1] && tokens[1].type === 'Identifier' ? tokens[1].value : tokens[0].value;
}
return '';
}
Expand Down
7 changes: 0 additions & 7 deletions lib/rules/sort-comp.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,6 @@ module.exports = {
* @returns {String} Property name.
*/
function getPropertyName(node) {
// Special case for class properties
// (babel-eslint does not expose property name so we have to rely on tokens)
if (node.type === 'ClassProperty') {
const tokens = context.getFirstTokens(node, 2);
return tokens[1] && tokens[1].type === 'Identifier' ? tokens[1].value : tokens[0].value;
}

if (node.kind === 'get') {
return 'getter functions';
}
Expand Down
6 changes: 1 addition & 5 deletions lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,8 @@ module.exports = {
* @returns {Boolean} True if node is `propTypes` declaration, false if not.
*/
function isPropTypesDeclaration(node) {
// Special case for class properties
// (babel-eslint does not expose property name so we have to rely on tokens)
if (node.type === 'ClassProperty') {
const tokens = context.getFirstTokens(node, 2);
return (tokens[0] && tokens[0].value === 'propTypes') ||
(tokens[1] && tokens[1].value === 'propTypes');
node = node.key;
}

return Boolean(
Expand Down

0 comments on commit 4e21d41

Please sign in to comment.