Skip to content

Commit

Permalink
fix(eslint-plugin): support eslint@5 (#2917)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Jan 4, 2021
1 parent 4c6ec93 commit f606846
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 54 deletions.
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",
"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(
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;
}

0 comments on commit f606846

Please sign in to comment.