New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(eslint-plugin): support eslint@5 #2917
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
---|---|---|
|
@@ -13,43 +13,6 @@ type Options = [ | |
}, | ||
]; | ||
|
||
// https://github.com/lodash/lodash/blob/86a852fe763935bb64c12589df5391fd7d3bb14d/escapeRegExp.js | ||
const reRegExpChar = /[\\^$.*+?()[\]{}|]/g; | ||
const reHasRegExpChar = RegExp(reRegExpChar.source); | ||
function escapeRegExp(str: string): string { | ||
return str && reHasRegExpChar.test(str) | ||
? str.replace(reRegExpChar, '\\$&') | ||
: str || ''; | ||
} | ||
|
||
function getNameLocationInGlobalDirectiveComment( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my IDE "complained" about duplicate code, so I found this and just removed it |
||
sourceCode: TSESLint.SourceCode, | ||
comment: TSESTree.Comment, | ||
name: string, | ||
): TSESTree.SourceLocation { | ||
const namePattern = new RegExp( | ||
`[\\s,]${escapeRegExp(name)}(?:$|[\\s,:])`, | ||
'gu', | ||
); | ||
|
||
// To ignore the first text "global". | ||
namePattern.lastIndex = comment.value.indexOf('global') + 6; | ||
|
||
// Search a given variable name. | ||
const match = namePattern.exec(comment.value); | ||
|
||
// Convert the index to loc. | ||
const start = sourceCode.getLocFromIndex( | ||
comment.range[0] + '/*'.length + (match ? match.index + 1 : 0), | ||
); | ||
const end = { | ||
line: start.line, | ||
column: start.column + (match ? name.length : 1), | ||
}; | ||
|
||
return { start, end }; | ||
} | ||
|
||
export default util.createRule<Options, MessageIds>({ | ||
name: 'no-redeclare', | ||
meta: { | ||
|
@@ -129,7 +92,7 @@ export default util.createRule<Options, MessageIds>({ | |
yield { | ||
type: 'comment', | ||
node: comment, | ||
loc: getNameLocationInGlobalDirectiveComment( | ||
loc: util.getNameLocationInGlobalDirectiveComment( | ||
sourceCode, | ||
comment, | ||
variable.name, | ||
|
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 |
---|---|---|
@@ -1,2 +1,43 @@ | ||
import type { TSESLint, TSESTree } from '@typescript-eslint/experimental-utils'; | ||
import escapeRegExp from 'lodash/escapeRegExp'; | ||
|
||
// deeply re-export, for convenience | ||
export * from '@typescript-eslint/experimental-utils/dist/ast-utils'; | ||
|
||
// The following is copied from `eslint`'s source code since it doesn't exist in eslint@5. | ||
// https://github.com/eslint/eslint/blob/145aec1ab9052fbca96a44d04927c595951b1536/lib/rules/utils/ast-utils.js#L1751-L1779 | ||
// Could be export { getNameLocationInGlobalDirectiveComment } from 'eslint/lib/rules/utils/ast-utils' | ||
/** | ||
* Get the `loc` object of a given name in a `/*globals` directive comment. | ||
* @param {SourceCode} sourceCode The source code to convert index to loc. | ||
* @param {Comment} comment The `/*globals` directive comment which include the name. | ||
* @param {string} name The name to find. | ||
* @returns {SourceLocation} The `loc` object. | ||
*/ | ||
export function getNameLocationInGlobalDirectiveComment( | ||
sourceCode: TSESLint.SourceCode, | ||
comment: TSESTree.Comment, | ||
name: string, | ||
): TSESTree.SourceLocation { | ||
const namePattern = new RegExp( | ||
`[\\s,]${escapeRegExp(name)}(?:$|[\\s,:])`, | ||
'gu', | ||
); | ||
|
||
// To ignore the first text "global". | ||
namePattern.lastIndex = comment.value.indexOf('global') + 6; | ||
|
||
// Search a given variable name. | ||
const match = namePattern.exec(comment.value); | ||
|
||
// Convert the index to loc. | ||
const start = sourceCode.getLocFromIndex( | ||
comment.range[0] + '/*'.length + (match ? match.index + 1 : 0), | ||
); | ||
const end = { | ||
line: start.line, | ||
column: start.column + (match ? name.length : 1), | ||
}; | ||
|
||
return { start, end }; | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@typescript-eslint/eslint-plugin-tslint
and@typescript-eslint/typescript-estree
already depend on it, so shouldn't make a difference