Skip to content

Commit

Permalink
Refactor: replace isSameNode with hasSameRange (#1036)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 19, 2021
1 parent cda72bd commit ca2f54f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
7 changes: 7 additions & 0 deletions rules/utils/has-same-range.js
@@ -0,0 +1,7 @@
'use strict';

module.exports = (node1, node2) =>
node1 &&
node2 &&
node1.range[0] === node2.range[0] &&
node1.range[1] === node2.range[1];
@@ -1,11 +1,16 @@
'use strict';

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

module.exports = identifier =>
identifier.parent.type === 'AssignmentPattern' &&
identifier.parent.left === identifier &&
identifier.parent.parent.type === 'Property' &&
isSameNode(identifier, identifier.parent.parent.key) &&
(
identifier === identifier.parent.parent.key ||
// In `babel-eslint` parent.key is not reference of identifier, #444
// issue https://github.com/babel/babel-eslint/issues/809
hasSameRange(identifier, identifier.parent.parent.key)
) &&
identifier.parent.parent.value === identifier.parent &&
identifier.parent.parent.shorthand;
11 changes: 0 additions & 11 deletions rules/utils/is-same-node.js

This file was deleted.

4 changes: 1 addition & 3 deletions rules/utils/is-shadowed.js
@@ -1,7 +1,5 @@
'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 @@ -10,7 +8,7 @@ const isSameNode = require('./is-same-node');
*/
function findReference(scope, node) {
const references = scope.references
.filter(reference => isSameNode(reference.identifier, node));
.filter(reference => reference.identifier === node);

if (references.length === 1) {
return references[0];
Expand Down
9 changes: 7 additions & 2 deletions rules/utils/is-shorthand-property-identifier.js
@@ -1,8 +1,13 @@
'use strict';

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

module.exports = identifier =>
identifier.parent.type === 'Property' &&
identifier.parent.shorthand &&
isSameNode(identifier, identifier.parent.key);
(
identifier === identifier.parent.key ||
// In `babel-eslint` parent.key is not reference of identifier, #444
// issue https://github.com/babel/babel-eslint/issues/809
hasSameRange(identifier, identifier.parent.key)
);

0 comments on commit ca2f54f

Please sign in to comment.