Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A better fix for
no-instanceof-array
(#1020)
- Loading branch information
Showing
6 changed files
with
351 additions
and
49 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,13 @@ | ||
'use strict'; | ||
const {isParenthesized} = require('eslint-utils'); | ||
|
||
const getParenthesizedTimes = (node, sourceCode) => { | ||
let times = 0; | ||
while (isParenthesized(times + 1, node, sourceCode)) { | ||
times++; | ||
} | ||
|
||
return times; | ||
}; | ||
|
||
module.exports = getParenthesizedTimes; |
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,31 @@ | ||
'use strict'; | ||
const {isOpeningParenToken, isClosingParenToken} = require('eslint-utils'); | ||
const getParenthesizedTimes = require('./get-parenthesized-times'); | ||
|
||
function * replaceNodeOrTokenAndSpacesBefore(nodeOrToken, replacement, fixer, sourceCode) { | ||
const parenthesizedTimes = getParenthesizedTimes(nodeOrToken, sourceCode); | ||
|
||
if (parenthesizedTimes > 0) { | ||
let lastBefore = nodeOrToken; | ||
let lastAfter = nodeOrToken; | ||
for (let index = 0; index < parenthesizedTimes; index++) { | ||
const openingParenthesisToken = sourceCode.getTokenBefore(lastBefore, isOpeningParenToken); | ||
const closingParenthesisToken = sourceCode.getTokenAfter(lastAfter, isClosingParenToken); | ||
yield * replaceNodeOrTokenAndSpacesBefore(openingParenthesisToken, '', fixer, sourceCode); | ||
yield * replaceNodeOrTokenAndSpacesBefore(closingParenthesisToken, '', fixer, sourceCode); | ||
lastBefore = openingParenthesisToken; | ||
lastAfter = closingParenthesisToken; | ||
} | ||
} | ||
|
||
let [start, end] = nodeOrToken.range; | ||
|
||
const textBefore = sourceCode.text.slice(0, start); | ||
const [trailingSpaces] = textBefore.match(/\s*$/); | ||
const [lineBreak] = trailingSpaces.match(/(?:\r?\n|\r){0,1}/); | ||
start -= trailingSpaces.length; | ||
|
||
yield fixer.replaceTextRange([start, end], `${lineBreak}${replacement}`); | ||
} | ||
|
||
module.exports = replaceNodeOrTokenAndSpacesBefore; |
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.