mirror of
https://github.com/elastic/kibana.git
synced 2025-06-27 18:51:07 -04:00
Clean up imports of ESLint types (#162436)
This commit is contained in:
parent
70b337a135
commit
5f70b4b8eb
13 changed files with 30 additions and 30 deletions
|
@ -6,7 +6,7 @@
|
||||||
* Side Public License, v 1.
|
* Side Public License, v 1.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type Eslint from 'eslint';
|
import type { AST } from 'eslint';
|
||||||
|
|
||||||
const ESLINT_DISABLE_RE = /^eslint-disable(?:-next-line|-line)?(?<rulesBlock>.*)/;
|
const ESLINT_DISABLE_RE = /^eslint-disable(?:-next-line|-line)?(?<rulesBlock>.*)/;
|
||||||
|
|
||||||
|
@ -17,16 +17,16 @@ export enum ESLINT_DISABLE_VALUE {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ParsedEslintDisableComment {
|
export interface ParsedEslintDisableComment {
|
||||||
type: Eslint.AST.Program['comments'][0]['type'];
|
type: AST.Program['comments'][0]['type'];
|
||||||
range: Eslint.AST.Program['comments'][0]['range'];
|
range: AST.Program['comments'][0]['range'];
|
||||||
loc: Eslint.AST.Program['comments'][0]['loc'];
|
loc: AST.Program['comments'][0]['loc'];
|
||||||
value: Eslint.AST.Program['comments'][0]['value'];
|
value: AST.Program['comments'][0]['value'];
|
||||||
disableValueType: ESLINT_DISABLE_VALUE;
|
disableValueType: ESLINT_DISABLE_VALUE;
|
||||||
rules: string[];
|
rules: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseEslintDisableComment(
|
export function parseEslintDisableComment(
|
||||||
comment: Eslint.AST.Program['comments'][0]
|
comment: AST.Program['comments'][0]
|
||||||
): ParsedEslintDisableComment | undefined {
|
): ParsedEslintDisableComment | undefined {
|
||||||
const commentVal = comment.value.trim();
|
const commentVal = comment.value.trim();
|
||||||
const nakedESLintRegexResult = commentVal.match(ESLINT_DISABLE_RE);
|
const nakedESLintRegexResult = commentVal.match(ESLINT_DISABLE_RE);
|
||||||
|
|
|
@ -6,12 +6,12 @@
|
||||||
* Side Public License, v 1.
|
* Side Public License, v 1.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type Eslint from 'eslint';
|
import type { AST } from 'eslint';
|
||||||
import { ESLINT_DISABLE_VALUE, ParsedEslintDisableComment } from './regex';
|
import { ESLINT_DISABLE_VALUE, ParsedEslintDisableComment } from './regex';
|
||||||
|
|
||||||
export function getReportLocFromComment(
|
export function getReportLocFromComment(
|
||||||
comment: ParsedEslintDisableComment
|
comment: ParsedEslintDisableComment
|
||||||
): Eslint.AST.SourceLocation | undefined {
|
): AST.SourceLocation | undefined {
|
||||||
const cStart = comment?.loc?.start;
|
const cStart = comment?.loc?.start;
|
||||||
const cEnd = comment?.loc?.end;
|
const cEnd = comment?.loc?.end;
|
||||||
const cStartLine = comment?.loc?.start?.line;
|
const cStartLine = comment?.loc?.start?.line;
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
* Side Public License, v 1.
|
* Side Public License, v 1.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Eslint from 'eslint';
|
import type { Rule, AST } from 'eslint';
|
||||||
import { getReportLocFromComment, parseEslintDisableComment } from '../helpers';
|
import { getReportLocFromComment, parseEslintDisableComment } from '../helpers';
|
||||||
|
|
||||||
export const NAKED_DISABLE_MSG_ID = 'no-naked-eslint-disable';
|
export const NAKED_DISABLE_MSG_ID = 'no-naked-eslint-disable';
|
||||||
|
@ -15,7 +15,7 @@ const messages = {
|
||||||
'Using a naked eslint disable is not allowed. Please specify the specific rules to disable.',
|
'Using a naked eslint disable is not allowed. Please specify the specific rules to disable.',
|
||||||
};
|
};
|
||||||
|
|
||||||
const meta: Eslint.Rule.RuleMetaData = {
|
const meta: Rule.RuleMetaData = {
|
||||||
type: 'problem',
|
type: 'problem',
|
||||||
fixable: 'code',
|
fixable: 'code',
|
||||||
docs: {
|
docs: {
|
||||||
|
@ -25,7 +25,7 @@ const meta: Eslint.Rule.RuleMetaData = {
|
||||||
messages,
|
messages,
|
||||||
};
|
};
|
||||||
|
|
||||||
const create = (context: Eslint.Rule.RuleContext): Eslint.Rule.RuleListener => {
|
const create = (context: Rule.RuleContext): Rule.RuleListener => {
|
||||||
return {
|
return {
|
||||||
Program(node) {
|
Program(node) {
|
||||||
const nodeComments = node.comments || [];
|
const nodeComments = node.comments || [];
|
||||||
|
@ -56,7 +56,7 @@ const create = (context: Eslint.Rule.RuleContext): Eslint.Rule.RuleListener => {
|
||||||
loc: reportLoc,
|
loc: reportLoc,
|
||||||
messageId: NAKED_DISABLE_MSG_ID,
|
messageId: NAKED_DISABLE_MSG_ID,
|
||||||
fix(fixer) {
|
fix(fixer) {
|
||||||
return fixer.removeRange(parsedEslintDisable.range as Eslint.AST.Range);
|
return fixer.removeRange(parsedEslintDisable.range as AST.Range);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -64,7 +64,7 @@ const create = (context: Eslint.Rule.RuleContext): Eslint.Rule.RuleListener => {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const NoNakedESLintDisableRule: Eslint.Rule.RuleModule = {
|
export const NoNakedESLintDisableRule: Rule.RuleModule = {
|
||||||
meta,
|
meta,
|
||||||
create,
|
create,
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
* Side Public License, v 1.
|
* Side Public License, v 1.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Eslint from 'eslint';
|
import type { Rule, AST } from 'eslint';
|
||||||
import { PROTECTED_RULES, getReportLocFromComment, parseEslintDisableComment } from '../helpers';
|
import { PROTECTED_RULES, getReportLocFromComment, parseEslintDisableComment } from '../helpers';
|
||||||
|
|
||||||
export const PROTECTED_DISABLE_MSG_ID = 'no-protected-eslint-disable';
|
export const PROTECTED_DISABLE_MSG_ID = 'no-protected-eslint-disable';
|
||||||
|
@ -15,7 +15,7 @@ const messages = {
|
||||||
"The rule '{{ disabledRuleName }}' is protected and disabling it is not allowed. Please remove it from the statement.",
|
"The rule '{{ disabledRuleName }}' is protected and disabling it is not allowed. Please remove it from the statement.",
|
||||||
};
|
};
|
||||||
|
|
||||||
const meta: Eslint.Rule.RuleMetaData = {
|
const meta: Rule.RuleMetaData = {
|
||||||
type: 'problem',
|
type: 'problem',
|
||||||
fixable: 'code',
|
fixable: 'code',
|
||||||
docs: {
|
docs: {
|
||||||
|
@ -24,7 +24,7 @@ const meta: Eslint.Rule.RuleMetaData = {
|
||||||
messages,
|
messages,
|
||||||
};
|
};
|
||||||
|
|
||||||
const create = (context: Eslint.Rule.RuleContext): Eslint.Rule.RuleListener => {
|
const create = (context: Rule.RuleContext): Rule.RuleListener => {
|
||||||
return {
|
return {
|
||||||
Program(node) {
|
Program(node) {
|
||||||
const nodeComments = node.comments || [];
|
const nodeComments = node.comments || [];
|
||||||
|
@ -68,7 +68,7 @@ const create = (context: Eslint.Rule.RuleContext): Eslint.Rule.RuleListener => {
|
||||||
fix(fixer) {
|
fix(fixer) {
|
||||||
// if we only have a single disabled rule and that is protected, we can remove the entire comment
|
// if we only have a single disabled rule and that is protected, we can remove the entire comment
|
||||||
if (disabledRules.length === 1) {
|
if (disabledRules.length === 1) {
|
||||||
return fixer.removeRange(parsedEslintDisable.range as Eslint.AST.Range);
|
return fixer.removeRange(parsedEslintDisable.range as AST.Range);
|
||||||
}
|
}
|
||||||
|
|
||||||
// it's impossible to fix as we don't have a range
|
// it's impossible to fix as we don't have a range
|
||||||
|
@ -80,7 +80,7 @@ const create = (context: Eslint.Rule.RuleContext): Eslint.Rule.RuleListener => {
|
||||||
const fixedComment = ` ${parsedEslintDisable.disableValueType} ${remainingRules.join(
|
const fixedComment = ` ${parsedEslintDisable.disableValueType} ${remainingRules.join(
|
||||||
', '
|
', '
|
||||||
)}${parsedEslintDisable.type === 'Block' ? ' ' : ''}`;
|
)}${parsedEslintDisable.type === 'Block' ? ' ' : ''}`;
|
||||||
const rangeToFix: Eslint.AST.Range =
|
const rangeToFix: AST.Range =
|
||||||
parsedEslintDisable.type === 'Line'
|
parsedEslintDisable.type === 'Line'
|
||||||
? [parsedEslintDisable.range[0] + 2, parsedEslintDisable.range[1]]
|
? [parsedEslintDisable.range[0] + 2, parsedEslintDisable.range[1]]
|
||||||
: [parsedEslintDisable.range[0] + 2, parsedEslintDisable.range[1] - 2];
|
: [parsedEslintDisable.range[0] + 2, parsedEslintDisable.range[1] - 2];
|
||||||
|
@ -93,7 +93,7 @@ const create = (context: Eslint.Rule.RuleContext): Eslint.Rule.RuleListener => {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const NoProtectedESLintDisableRule: Eslint.Rule.RuleModule = {
|
export const NoProtectedESLintDisableRule: Rule.RuleModule = {
|
||||||
meta,
|
meta,
|
||||||
create,
|
create,
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
import { ImportResolver } from '@kbn/import-resolver';
|
import { ImportResolver } from '@kbn/import-resolver';
|
||||||
import { REPO_ROOT } from '@kbn/repo-info';
|
import { REPO_ROOT } from '@kbn/repo-info';
|
||||||
import { Rule } from 'eslint';
|
import type { Rule } from 'eslint';
|
||||||
import { RUNNING_IN_EDITOR } from './helpers/running_in_editor';
|
import { RUNNING_IN_EDITOR } from './helpers/running_in_editor';
|
||||||
|
|
||||||
let importResolverCache: ImportResolver | undefined;
|
let importResolverCache: ImportResolver | undefined;
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
* Side Public License, v 1.
|
* Side Public License, v 1.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Eslint from 'eslint';
|
import type { Rule } from 'eslint';
|
||||||
import { SomeNode } from './visit_all_import_statements';
|
import { SomeNode } from './visit_all_import_statements';
|
||||||
|
|
||||||
interface ReportOptions {
|
interface ReportOptions {
|
||||||
|
@ -18,7 +18,7 @@ interface ReportOptions {
|
||||||
/**
|
/**
|
||||||
* Simple wrapper around context.report so that the types work better with typescript-estree
|
* Simple wrapper around context.report so that the types work better with typescript-estree
|
||||||
*/
|
*/
|
||||||
export function report(context: Eslint.Rule.RuleContext, options: ReportOptions) {
|
export function report(context: Rule.RuleContext, options: ReportOptions) {
|
||||||
context.report({
|
context.report({
|
||||||
node: options.node as any,
|
node: options.node as any,
|
||||||
message: options.message,
|
message: options.message,
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
* Side Public License, v 1.
|
* Side Public License, v 1.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Rule } from 'eslint';
|
import type { Rule } from 'eslint';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the path of the sourcefile being linted
|
* Get the path of the sourcefile being linted
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
* Side Public License, v 1.
|
* Side Public License, v 1.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Rule } from 'eslint';
|
import type { Rule } from 'eslint';
|
||||||
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree';
|
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree';
|
||||||
import * as T from '@babel/types';
|
import * as T from '@babel/types';
|
||||||
import { ImportType } from '@kbn/import-resolver';
|
import { ImportType } from '@kbn/import-resolver';
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
* Side Public License, v 1.
|
* Side Public License, v 1.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Rule, AST } from 'eslint';
|
import type { Rule, AST } from 'eslint';
|
||||||
import * as T from '@babel/types';
|
import * as T from '@babel/types';
|
||||||
import { TSESTree } from '@typescript-eslint/typescript-estree';
|
import { TSESTree } from '@typescript-eslint/typescript-estree';
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ import Path from 'path';
|
||||||
|
|
||||||
import { TSESTree } from '@typescript-eslint/typescript-estree';
|
import { TSESTree } from '@typescript-eslint/typescript-estree';
|
||||||
import * as Bt from '@babel/types';
|
import * as Bt from '@babel/types';
|
||||||
import { Rule } from 'eslint';
|
import type { Rule } from 'eslint';
|
||||||
import ESTree from 'estree';
|
import ESTree from 'estree';
|
||||||
import { ModuleType } from '@kbn/repo-source-classifier';
|
import { ModuleType } from '@kbn/repo-source-classifier';
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Path from 'path';
|
import Path from 'path';
|
||||||
import { Rule } from 'eslint';
|
import type { Rule } from 'eslint';
|
||||||
|
|
||||||
import { report } from '../helpers/report';
|
import { report } from '../helpers/report';
|
||||||
import { getSourcePath } from '../helpers/source';
|
import { getSourcePath } from '../helpers/source';
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
* Side Public License, v 1.
|
* Side Public License, v 1.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Rule, Scope, AST } from 'eslint';
|
import type { Rule, Scope, AST } from 'eslint';
|
||||||
import type { Comment } from 'estree';
|
import type { Comment } from 'estree';
|
||||||
import * as T from '@babel/types';
|
import * as T from '@babel/types';
|
||||||
import { TSESTree } from '@typescript-eslint/typescript-estree';
|
import { TSESTree } from '@typescript-eslint/typescript-estree';
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
import Path from 'path';
|
import Path from 'path';
|
||||||
|
|
||||||
import Eslint from 'eslint';
|
import type { Rule } from 'eslint';
|
||||||
import { getRelativeImportReq, getPackageRelativeImportReq } from '@kbn/import-resolver';
|
import { getRelativeImportReq, getPackageRelativeImportReq } from '@kbn/import-resolver';
|
||||||
|
|
||||||
import { report } from '../helpers/report';
|
import { report } from '../helpers/report';
|
||||||
|
@ -16,7 +16,7 @@ import { visitAllImportStatements } from '../helpers/visit_all_import_statements
|
||||||
import { getSourcePath } from '../helpers/source';
|
import { getSourcePath } from '../helpers/source';
|
||||||
import { getImportResolver } from '../get_import_resolver';
|
import { getImportResolver } from '../get_import_resolver';
|
||||||
|
|
||||||
export const UniformImportsRule: Eslint.Rule.RuleModule = {
|
export const UniformImportsRule: Rule.RuleModule = {
|
||||||
meta: {
|
meta: {
|
||||||
fixable: 'code',
|
fixable: 'code',
|
||||||
docs: {
|
docs: {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue