-
-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
502 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
/** | ||
Find the index of the question mark in a ConditionalExpression node | ||
@param {import('estree').ConditionalExpression} node | ||
@param {import('eslint').SourceCode} sourceCode | ||
@returns {number} | ||
*/ | ||
function findIndexOfQuestionMarkInConditionalExpression(node, sourceCode) { | ||
const testAfterComments = sourceCode.getCommentsAfter(node.test); | ||
const consequentBeforeComments = sourceCode.getCommentsBefore(node.consequent); | ||
|
||
let start = node.test.range[1]; | ||
let end = node.consequent.range[0]; | ||
|
||
if (testAfterComments.length > 0) { | ||
const lastComment = testAfterComments.at(-1); | ||
|
||
start = lastComment.range[1]; | ||
} | ||
|
||
if (consequentBeforeComments.length > 0) { | ||
const firstComment = consequentBeforeComments.at(0); | ||
|
||
end = firstComment.range[0]; | ||
} | ||
|
||
return start + sourceCode.getText().slice(start, end).indexOf('?'); | ||
} | ||
|
||
module.exports = { | ||
findIndexOfQuestionMarkInConditionalExpression, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict'; | ||
|
||
const ignoreKeys = new Set(['loc', 'start', 'end', 'parent', 'range']); | ||
|
||
/** | ||
Compare two AST nodes for equality by structure and value. | ||
Different from `deepStrictEqual` in that it ignores `loc`, `start`, `end`, `parent`, and `range` properties. | ||
Different from `isSameReference` in that `isSameReference` just checks if the nodes are the same reference, but cannot compare two identical data. | ||
eg.: | ||
```js | ||
const node1 = { foo: bar } | ||
const node2 = { foo: bar } | ||
``` | ||
isNodesEqual(node1, node2) => true | ||
isSameReference(node1, node2) => false | ||
@param {import('estree').Node} node1 - The first AST node. | ||
@param {import('estree').Node} node2 - The second AST node. | ||
@returns {boolean} - True if the nodes are structurally and value-wise equal, false otherwise. | ||
*/ | ||
function isNodesEqual(node1, node2) { | ||
if (node1 === node2) { | ||
return true; | ||
} | ||
|
||
if (typeof node1 === 'string' || typeof node1 === 'boolean' || typeof node1 === 'number') { | ||
return node1 === node2; | ||
} | ||
|
||
// If one of them is null or undefined, they are not equal | ||
if (!node1 || !node2) { | ||
return false; | ||
} | ||
|
||
// If they are of different types, they are not equal | ||
if (node1.type !== node2.type) { | ||
return false; | ||
} | ||
|
||
if (node1.type === 'Literal') { | ||
return node1.value === node2.value; | ||
} | ||
|
||
// Compare properties recursively | ||
for (const key in node1) { | ||
if (Object.hasOwn(node1, key) && !ignoreKeys.has(key) && !isNodesEqual(node1[key], node2[key])) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
module.exports = isNodesEqual; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.