Skip to content
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 1 commit into from Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/eslint-plugin/package.json
Expand Up @@ -46,6 +46,7 @@
"@typescript-eslint/scope-manager": "4.12.0",
"debug": "^4.1.1",
"functional-red-black-tree": "^1.0.1",
"lodash": "^4.17.15",
Copy link
Contributor Author

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

"regexpp": "^3.0.0",
"semver": "^7.3.2",
"tsutils": "^3.17.1"
Expand Down
39 changes: 1 addition & 38 deletions packages/eslint-plugin/src/rules/no-redeclare.ts
Expand Up @@ -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(
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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: {
Expand Down Expand Up @@ -129,7 +92,7 @@ export default util.createRule<Options, MessageIds>({
yield {
type: 'comment',
node: comment,
loc: getNameLocationInGlobalDirectiveComment(
loc: util.getNameLocationInGlobalDirectiveComment(
sourceCode,
comment,
variable.name,
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin/src/rules/no-unused-vars.ts
Expand Up @@ -4,7 +4,6 @@ import {
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { PatternVisitor } from '@typescript-eslint/scope-manager';
import { getNameLocationInGlobalDirectiveComment } from 'eslint/lib/rules/utils/ast-utils';
import * as util from '../util';

export type MessageIds = 'unusedVar';
Expand Down Expand Up @@ -388,7 +387,7 @@ export default util.createRule<Options, MessageIds>({

context.report({
node: programNode,
loc: getNameLocationInGlobalDirectiveComment(
loc: util.getNameLocationInGlobalDirectiveComment(
sourceCode,
directiveComment,
unusedVar.name,
Expand Down
41 changes: 41 additions & 0 deletions packages/eslint-plugin/src/util/astUtils.ts
@@ -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 };
}
14 changes: 0 additions & 14 deletions packages/eslint-plugin/typings/eslint-rules.d.ts
Expand Up @@ -830,17 +830,3 @@ declare module 'eslint/lib/rules/prefer-const' {
>;
export = rule;
}

declare module 'eslint/lib/rules/utils/ast-utils' {
import { TSESLint, TSESTree } from '@typescript-eslint/experimental-utils';

const utils: {
getNameLocationInGlobalDirectiveComment(
sourceCode: TSESLint.SourceCode,
comment: TSESTree.Comment,
name: string,
): TSESTree.SourceLocation;
};

export = utils;
}