Skip to content

Commit

Permalink
Extract isSameNode to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 16, 2020
1 parent ee6a915 commit fbff386
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
14 changes: 3 additions & 11 deletions rules/prevent-abbreviations.js
Expand Up @@ -6,6 +6,7 @@ const {defaultsDeep, upperFirst, lowerFirst} = require('lodash');
const getDocumentationUrl = require('./utils/get-documentation-url');
const avoidCapture = require('./utils/avoid-capture');
const cartesianProductSamples = require('./utils/cartesian-product-samples');
const isSameNode = require('./utils/is-same-node');

const isUpperCase = string => string === string.toUpperCase();
const isUpperFirst = string => isUpperCase(string[0]);
Expand Down Expand Up @@ -397,20 +398,11 @@ const shouldFix = variable => {
return !variableIdentifiers(variable).some(isExportedIdentifier);
};

const isIdentifierKeyOfNode = (identifier, node) =>
node.key === identifier ||
// In `babel-eslint` parent.key is not reference of identifier
// https://github.com/babel/babel-eslint/issues/809
(
node.key.type === identifier.type &&
node.key.name === identifier.name
);

const isShorthandPropertyIdentifier = identifier => {
return (
identifier.parent.type === 'Property' &&
identifier.parent.shorthand &&
isIdentifierKeyOfNode(identifier, identifier.parent)
isSameNode(identifier, identifier.parent.key)
);
};

Expand All @@ -419,7 +411,7 @@ const isAssignmentPatternShorthandPropertyIdentifier = identifier => {
identifier.parent.type === 'AssignmentPattern' &&
identifier.parent.left === identifier &&
identifier.parent.parent.type === 'Property' &&
isIdentifierKeyOfNode(identifier, identifier.parent.parent) &&
isSameNode(identifier, identifier.parent.parent.key) &&
identifier.parent.parent.value === identifier.parent &&
identifier.parent.parent.shorthand
);
Expand Down
9 changes: 9 additions & 0 deletions rules/utils/is-same-node.js
@@ -0,0 +1,9 @@

module.exports = (node1, node2) =>
node1 &&
(
node1 === node2 ||
// In `babel-eslint` parent.key is not reference of identifier, #444
// issue https://github.com/babel/babel-eslint/issues/809
(node1.range[0] === node2.range[0] && node1.range[1] === node2.range[1])
);
7 changes: 3 additions & 4 deletions rules/utils/is-shadowed.js
@@ -1,5 +1,7 @@
'use strict';

const isSameNode = require('./is-same-node');

/**
* Finds the eslint-scope reference in the given scope.
* @param {Object} scope The scope to search.
Expand All @@ -8,10 +10,7 @@
*/
function findReference(scope, node) {
const references = scope.references
.filter(reference =>
reference.identifier.range[0] === node.range[0] &&
reference.identifier.range[1] === node.range[1]
);
.filter(reference => isSameNode(reference.identifier, node));

if (references.length === 1) {
return references[0];
Expand Down

0 comments on commit fbff386

Please sign in to comment.