diff --git a/types/eslint/eslint-tests.ts b/types/eslint/eslint-tests.ts index f654773e87..0344bc4cd1 100644 --- a/types/eslint/eslint-tests.ts +++ b/types/eslint/eslint-tests.ts @@ -349,6 +349,31 @@ rule = { } }); + context.report({ + message: 'foo', + node: AST, + suggest: [ + { + desc: 'foo', + fix: ruleFixer => { + return [ + ruleFixer.insertTextAfter(AST, 'foo'), + ruleFixer.insertTextAfter(TOKEN, 'foo') + ]; + }, + }, + { + messageId: 'foo', + fix: ruleFixer => { + return [ + ruleFixer.insertTextAfter(AST, 'foo'), + ruleFixer.insertTextAfter(TOKEN, 'foo') + ]; + }, + }, + ], + }); + return { onCodePathStart(codePath, node) {}, onCodePathEnd(codePath, node) {}, @@ -430,6 +455,9 @@ for (const msg of lintingResult) { msg.fatal = true; + msg.message = 'foo'; + msg.messageId = 'foo'; + msg.line = 0; msg.endLine = 0; msg.column = 0; @@ -441,6 +469,15 @@ for (const msg of lintingResult) { msg.fix.text = 'foo'; msg.fix.range = [0, 0]; } + + if (msg.suggestions) { + for (const suggestion of msg.suggestions) { + suggestion.desc = 'foo'; + suggestion.messageId = 'foo'; + suggestion.fix.text = 'foo'; + suggestion.fix.range = [0, 0]; + } + } } linter.verifyAndFix(SOURCE, {}); @@ -596,11 +633,25 @@ ruleTester.run('my-rule', rule, { invalid: [ { code: 'foo', errors: 1 }, + { code: 'foo', errors: 1, output: 'foo' }, { code: 'foo', errors: ['foo'] }, { code: 'foo', errors: [{ message: 'foo' }] }, { code: 'foo', errors: [{ message: 'foo', type: 'foo' }] }, { code: 'foo', errors: [{ message: 'foo', data: { foo: true } }] }, { code: 'foo', errors: [{ message: 'foo', line: 0 }] }, + { code: 'foo', errors: [{ + message: 'foo', + suggestions: [ + { + desc: 'foo', + output: 'foo', + }, + { + messageId: 'foo', + output: 'foo', + }, + ], + }] }, ] }); diff --git a/types/eslint/index.d.ts b/types/eslint/index.d.ts index 3f69cc9222..c143d3692d 100644 --- a/types/eslint/index.d.ts +++ b/types/eslint/index.d.ts @@ -1,9 +1,10 @@ -// Type definitions for eslint 6.1 +// Type definitions for eslint 6.8 // Project: https://eslint.org // Definitions by: Pierre-Marie Dartus // Jed Fox // Saad Quadri // Jason Kwok +// Brad Zacher // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 @@ -319,16 +320,24 @@ export namespace Rule { report(descriptor: ReportDescriptor): void; } + interface ReportDescriptorOptionsBase { + data?: { [key: string]: string }; + + fix?: null | ((fixer: RuleFixer) => null | Fix | IterableIterator | Fix[]); + } + + type SuggestionDescriptorMessage = { desc: string } | { messageId: string }; + type SuggestionReportDescriptor = SuggestionDescriptorMessage & ReportDescriptorOptionsBase; + + interface ReportDescriptorOptions extends ReportDescriptorOptionsBase { + suggest?: SuggestionReportDescriptor[] | null; + } + type ReportDescriptor = ReportDescriptorMessage & ReportDescriptorLocation & ReportDescriptorOptions; type ReportDescriptorMessage = { message: string } | { messageId: string }; type ReportDescriptorLocation = | { node: ESTree.Node } | { loc: AST.SourceLocation | { line: number; column: number } }; - interface ReportDescriptorOptions { - data?: { [key: string]: string }; - - fix?(fixer: RuleFixer): null | Fix | IterableIterator | Fix[]; - } interface RuleFixer { insertTextAfter(nodeOrToken: ESTree.Node | AST.Token, text: string): Fix; @@ -378,33 +387,45 @@ export class Linter { export namespace Linter { type Severity = 0 | 1 | 2; - type RuleLevel = Severity | 'off' | 'warn' | 'error'; + type RuleLevel = Severity | 'off' | 'warn' | 'error'; interface RuleLevelAndOptions extends Array { 0: RuleLevel; } + + type RuleEntry = RuleLevel | RuleLevelAndOptions; + + interface RulesRecord { + [rule: string]: RuleEntry; + } + interface HasRules { - rules?: { - [name: string]: RuleLevel | RuleLevelAndOptions; - }; + rules?: Partial; } - interface RuleOverride extends HasRules { + interface BaseConfig extends HasRules { + $schema?: string; + env?: { [name: string]: boolean }; extends?: string | string[]; - excludedFiles?: string[]; - files?: string[]; - } - - interface Config extends HasRules { + globals?: { [name: string]: boolean }; + noInlineConfig?: boolean; + overrides?: ConfigOverride[]; parser?: string; parserOptions?: ParserOptions; - settings?: { [name: string]: any }; - env?: { [name: string]: boolean }; - globals?: { [name: string]: boolean }; - extends?: string | string[]; - overrides?: RuleOverride[]; - processor?: string; plugins?: string[]; + processor?: string; + reportUnusedDisableDirectives?: boolean; + settings?: { [name: string]: any }; + } + + interface ConfigOverride extends BaseConfig { + excludedFiles?: string | string[]; + files: string | string[]; + } + + // https://github.com/eslint/eslint/blob/v6.8.0/conf/config-schema.js + interface Config extends BaseConfig { + ignorePatterns?: string | string[]; root?: boolean; } @@ -429,6 +450,12 @@ export namespace Linter { reportUnusedDisableDirectives?: boolean; } + interface LintSuggestion { + desc: string; + fix: Rule.Fix; + messageId?: string; + } + interface LintMessage { column: number; line: number; @@ -436,11 +463,13 @@ export namespace Linter { endLine?: number; ruleId: string | null; message: string; + messageId?: string; nodeType: string; fatal?: true; severity: Severity; fix?: Rule.Fix; source: string | null; + suggestions?: LintSuggestion[]; } interface FixOptions extends LintOptions { @@ -592,6 +621,13 @@ export namespace RuleTester { globals?: { [name: string]: boolean }; } + interface SuggestionOutput { + messageId?: string; + desc?: string; + data?: Record; + output: string; + } + interface InvalidTestCase extends ValidTestCase { errors: number | Array; output?: string | null; @@ -606,6 +642,7 @@ export namespace RuleTester { column?: number; endLine?: number; endColumn?: number; + suggestions?: SuggestionOutput[]; } } diff --git a/types/eslint/ts3.1/eslint-tests.ts b/types/eslint/ts3.1/eslint-tests.ts index d2531f09ed..7782ec922e 100644 --- a/types/eslint/ts3.1/eslint-tests.ts +++ b/types/eslint/ts3.1/eslint-tests.ts @@ -349,6 +349,31 @@ rule = { } }); + context.report({ + message: 'foo', + node: AST, + suggest: [ + { + desc: 'foo', + fix: ruleFixer => { + return [ + ruleFixer.insertTextAfter(AST, 'foo'), + ruleFixer.insertTextAfter(TOKEN, 'foo') + ]; + }, + }, + { + messageId: 'foo', + fix: ruleFixer => { + return [ + ruleFixer.insertTextAfter(AST, 'foo'), + ruleFixer.insertTextAfter(TOKEN, 'foo') + ]; + }, + }, + ], + }); + return { onCodePathStart(codePath, node) {}, onCodePathEnd(codePath, node) {}, @@ -430,6 +455,9 @@ for (const msg of lintingResult) { msg.fatal = true; + msg.message = 'foo'; + msg.messageId = 'foo'; + msg.line = 0; msg.endLine = 0; msg.column = 0; @@ -441,6 +469,15 @@ for (const msg of lintingResult) { msg.fix.text = 'foo'; msg.fix.range = [0, 0]; } + + if (msg.suggestions) { + for (const suggestion of msg.suggestions) { + suggestion.desc = 'foo'; + suggestion.messageId = 'foo'; + suggestion.fix.text = 'foo'; + suggestion.fix.range = [0, 0]; + } + } } linter.verifyAndFix(SOURCE, {}); @@ -596,11 +633,25 @@ ruleTester.run('my-rule', rule, { invalid: [ { code: 'foo', errors: 1 }, + { code: 'foo', errors: 1, output: 'foo' }, { code: 'foo', errors: ['foo'] }, { code: 'foo', errors: [{ message: 'foo' }] }, { code: 'foo', errors: [{ message: 'foo', type: 'foo' }] }, { code: 'foo', errors: [{ message: 'foo', data: { foo: true } }] }, { code: 'foo', errors: [{ message: 'foo', line: 0 }] }, + { code: 'foo', errors: [{ + message: 'foo', + suggestions: [ + { + desc: 'foo', + output: 'foo', + }, + { + messageId: 'foo', + output: 'foo', + }, + ], + }] }, ] }); diff --git a/types/eslint/ts3.1/index.d.ts b/types/eslint/ts3.1/index.d.ts index 5fe80264d3..7f1918fa8c 100644 --- a/types/eslint/ts3.1/index.d.ts +++ b/types/eslint/ts3.1/index.d.ts @@ -312,16 +312,24 @@ export namespace Rule { report(descriptor: ReportDescriptor): void; } + interface ReportDescriptorOptionsBase { + data?: { [key: string]: string }; + + fix?: null | ((fixer: RuleFixer) => null | Fix | IterableIterator | Fix[]); + } + + type SuggestionDescriptorMessage = { desc: string } | { messageId: string }; + type SuggestionReportDescriptor = SuggestionDescriptorMessage & ReportDescriptorOptionsBase; + + interface ReportDescriptorOptions extends ReportDescriptorOptionsBase { + suggest?: SuggestionReportDescriptor[] | null; + } + type ReportDescriptor = ReportDescriptorMessage & ReportDescriptorLocation & ReportDescriptorOptions; type ReportDescriptorMessage = { message: string } | { messageId: string }; type ReportDescriptorLocation = | { node: ESTree.Node } | { loc: AST.SourceLocation | { line: number; column: number } }; - interface ReportDescriptorOptions { - data?: { [key: string]: string }; - - fix?(fixer: RuleFixer): null | Fix | IterableIterator | Fix[]; - } interface RuleFixer { insertTextAfter(nodeOrToken: ESTree.Node | AST.Token, text: string): Fix; @@ -385,22 +393,29 @@ export namespace Linter { rules?: Partial; } - interface RuleOverride extends HasRules { + interface BaseConfig extends HasRules { + $schema?: string; + env?: { [name: string]: boolean }; extends?: string | string[]; - excludedFiles?: string[]; - files?: string[]; - } - - interface Config extends HasRules { + globals?: { [name: string]: boolean }; + noInlineConfig?: boolean; + overrides?: ConfigOverride[]; parser?: string; parserOptions?: ParserOptions; - settings?: { [name: string]: any }; - env?: { [name: string]: boolean }; - globals?: { [name: string]: boolean }; - extends?: string | string[]; - overrides?: RuleOverride[]; - processor?: string; plugins?: string[]; + processor?: string; + reportUnusedDisableDirectives?: boolean; + settings?: { [name: string]: any }; + } + + interface ConfigOverride extends BaseConfig { + excludedFiles?: string | string[]; + files: string | string[]; + } + + // https://github.com/eslint/eslint/blob/v6.8.0/conf/config-schema.js + interface Config extends BaseConfig { + ignorePatterns?: string | string[]; root?: boolean; } @@ -425,6 +440,12 @@ export namespace Linter { reportUnusedDisableDirectives?: boolean; } + interface LintSuggestion { + desc: string; + fix: Rule.Fix; + messageId?: string; + } + interface LintMessage { column: number; line: number; @@ -432,11 +453,13 @@ export namespace Linter { endLine?: number; ruleId: string | null; message: string; + messageId?: string; nodeType: string; fatal?: true; severity: Severity; fix?: Rule.Fix; source: string | null; + suggestions?: LintSuggestion[]; } interface FixOptions extends LintOptions { @@ -588,6 +611,13 @@ export namespace RuleTester { globals?: { [name: string]: boolean }; } + interface SuggestionOutput { + messageId?: string; + desc?: string; + data?: Record; + output: string; + } + interface InvalidTestCase extends ValidTestCase { errors: number | Array; output?: string | null; @@ -602,6 +632,7 @@ export namespace RuleTester { column?: number; endLine?: number; endColumn?: number; + suggestions?: SuggestionOutput[]; } }