feat(eslint): update types to 6.8 (#43624)

* feat(eslint): update types to 6.8

includes:
- a few new config options
- correct of the config overrides
- support for 6.7's suggestions API

* add me as an author
This commit is contained in:
Brad Zacher 2020-04-06 11:59:02 -07:00 committed by GitHub
parent 56c1e21df1
commit e256f8a19e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 209 additions and 39 deletions

View File

@ -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',
},
],
}] },
]
});

View File

@ -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 <https://github.com/pmdartus>
// Jed Fox <https://github.com/j-f1>
// Saad Quadri <https://github.com/saadq>
// Jason Kwok <https://github.com/JasonHK>
// Brad Zacher <https://github.com/bradzacher>
// 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> | 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> | 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<any> {
0: RuleLevel;
}
type RuleEntry = RuleLevel | RuleLevelAndOptions;
interface RulesRecord {
[rule: string]: RuleEntry;
}
interface HasRules {
rules?: {
[name: string]: RuleLevel | RuleLevelAndOptions;
};
rules?: Partial<RulesRecord>;
}
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<string, any>;
output: string;
}
interface InvalidTestCase extends ValidTestCase {
errors: number | Array<TestCaseError | string>;
output?: string | null;
@ -606,6 +642,7 @@ export namespace RuleTester {
column?: number;
endLine?: number;
endColumn?: number;
suggestions?: SuggestionOutput[];
}
}

View File

@ -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',
},
],
}] },
]
});

View File

@ -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> | 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> | Fix[];
}
interface RuleFixer {
insertTextAfter(nodeOrToken: ESTree.Node | AST.Token, text: string): Fix;
@ -385,22 +393,29 @@ export namespace Linter {
rules?: Partial<Rules>;
}
interface RuleOverride<Rules extends RulesRecord = RulesRecord> extends HasRules<Rules> {
interface BaseConfig<Rules extends RulesRecord = RulesRecord> extends HasRules<Rules> {
$schema?: string;
env?: { [name: string]: boolean };
extends?: string | string[];
excludedFiles?: string[];
files?: string[];
}
interface Config<Rules extends RulesRecord = RulesRecord> extends HasRules<Rules> {
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<Rules extends RulesRecord = RulesRecord> extends BaseConfig<Rules> {
excludedFiles?: string | string[];
files: string | string[];
}
// https://github.com/eslint/eslint/blob/v6.8.0/conf/config-schema.js
interface Config<Rules extends RulesRecord = RulesRecord> extends BaseConfig<Rules> {
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<string, unknown>;
output: string;
}
interface InvalidTestCase extends ValidTestCase {
errors: number | Array<TestCaseError | string>;
output?: string | null;
@ -602,6 +632,7 @@ export namespace RuleTester {
column?: number;
endLine?: number;
endColumn?: number;
suggestions?: SuggestionOutput[];
}
}