Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): [ban-ts-comment] add descriptionFormat option#5026
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
273e750
a1686bb
88430b3
acf7b70
620bf20
a93f5b0
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,43 @@ | ||
import { AST_TOKEN_TYPES } from '@typescript-eslint/utils'; | ||
import * as util from '../util'; | ||
type DirectiveConfig = | ||
| boolean | ||
| 'allow-with-description' | ||
| { descriptionFormat: string }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Hmm, it would be nice to have | ||
interface Options { | ||
'ts-expect-error'?:DirectiveConfig; | ||
'ts-ignore'?:DirectiveConfig; | ||
'ts-nocheck'?:DirectiveConfig; | ||
'ts-check'?:DirectiveConfig; | ||
minimumDescriptionLength?: number; | ||
} | ||
const directiveConfigSchema = { | ||
oneOf: [ | ||
{ | ||
type: 'boolean', | ||
default: true, | ||
}, | ||
{ | ||
enum: ['allow-with-description'], | ||
}, | ||
{ | ||
type: 'object', | ||
properties: { | ||
descriptionFormat: { type: 'string' }, | ||
}, | ||
}, | ||
], | ||
}; | ||
export const defaultMinimumDescriptionLength = 3; | ||
type MessageIds = | ||
| 'tsDirectiveComment' | ||
| 'tsDirectiveCommentRequiresDescription' | ||
| 'tsDirectiveCommentDescriptionNotMatchPattern'; | ||
export default util.createRule<[Options], MessageIds>({ | ||
name: 'ban-ts-comment', | ||
@@ -29,55 +53,17 @@ export default util.createRule<[Options], MessageIds>({ | ||
'Do not use "@ts-{{directive}}" because it alters compilation errors.', | ||
tsDirectiveCommentRequiresDescription: | ||
'Include a description after the "@ts-{{directive}}" directive to explain why the @ts-{{directive}} is necessary. The description must be {{minimumDescriptionLength}} characters or longer.', | ||
tsDirectiveCommentDescriptionNotMatchPattern: | ||
'The description for the "@ts-{{directive}}" directive must match the {{format}} format.', | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
'ts-expect-error': directiveConfigSchema, | ||
'ts-ignore': directiveConfigSchema, | ||
'ts-nocheck': directiveConfigSchema, | ||
'ts-check': directiveConfigSchema, | ||
minimumDescriptionLength: { | ||
type: 'number', | ||
default: defaultMinimumDescriptionLength, | ||
@@ -99,25 +85,42 @@ export default util.createRule<[Options], MessageIds>({ | ||
create(context, [options]) { | ||
/* | ||
The regex used are taken from the ones used in the official TypeScript repo - | ||
https://github.com/microsoft/TypeScript/blob/408c760fae66080104bc85c449282c2d207dfe8e/src/compiler/scanner.ts#L288-L296 | ||
bradzacher marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
*/ | ||
const commentDirectiveRegExSingleLine = | ||
/^\/*\s*@ts-(?<directive>expect-error|ignore|check|nocheck)(?<description>.*)/; | ||
const commentDirectiveRegExMultiLine = | ||
/^\s*(?:\/|\*)*\s*@ts-(?<directive>expect-error|ignore|check|nocheck)(?<description>.*)/; | ||
const sourceCode = context.getSourceCode(); | ||
const descriptionFormats = new Map<string, RegExp>(); | ||
for (const directive of [ | ||
'ts-expect-error', | ||
'ts-ignore', | ||
'ts-nocheck', | ||
'ts-check', | ||
] as const) { | ||
const option = options[directive]; | ||
if (typeof option === 'object' && option.descriptionFormat) { | ||
descriptionFormats.set(directive, new RegExp(option.descriptionFormat)); | ||
} | ||
} | ||
return { | ||
Program(): void { | ||
const comments = sourceCode.getAllComments(); | ||
comments.forEach(comment => { | ||
const regExp = | ||
comment.type === AST_TOKEN_TYPES.Line | ||
? commentDirectiveRegExSingleLine | ||
: commentDirectiveRegExMultiLine; | ||
const match = regExp.exec(comment.value); | ||
if (!match) { | ||
return; | ||
} | ||
const{ directive, description } = match.groups!; | ||
const fullDirective = `ts-${directive}` as keyof Options; | ||
@@ -130,16 +133,26 @@ export default util.createRule<[Options], MessageIds>({ | ||
}); | ||
} | ||
if ( | ||
option === 'allow-with-description' || | ||
(typeof option === 'object' && option.descriptionFormat) | ||
) { | ||
const { | ||
minimumDescriptionLength = defaultMinimumDescriptionLength, | ||
} = options; | ||
const format = descriptionFormats.get(fullDirective); | ||
if (description.trim().length < minimumDescriptionLength) { | ||
context.report({ | ||
data: { directive, minimumDescriptionLength }, | ||
node: comment, | ||
messageId: 'tsDirectiveCommentRequiresDescription', | ||
}); | ||
} else if (format && !format.test(description)) { | ||
context.report({ | ||
data: { directive, format: format.source }, | ||
node: comment, | ||
messageId: 'tsDirectiveCommentDescriptionNotMatchPattern', | ||
}); | ||
} | ||
} | ||
}); | ||
Uh oh!
There was an error while loading.Please reload this page.