-
Notifications
You must be signed in to change notification settings - Fork 149
feat: create recommended eslint configuration #468
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,12 @@ | ||||||||||||||||||||||||||
| // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||||||||||||||||||||||||||
| // See LICENSE in the project root for license information. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| import type { Linter } from 'eslint'; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const config: Linter.Config = { | ||||||||||||||||||||||||||
| rules: { | ||||||||||||||||||||||||||
| 'tsdoc/syntax': 'warn' | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| export default config; | ||||||||||||||||||||||||||
|
Comment on lines
+6
to
+12
|
||||||||||||||||||||||||||
| const config: Linter.Config = { | |
| rules: { | |
| 'tsdoc/syntax': 'warn' | |
| } | |
| }; | |
| export default config; | |
| export const recommended: Linter.Config = { | |
| rules: { | |
| 'tsdoc/syntax': 'warn' | |
| } | |
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import type { TSDocConfigFile } from '@microsoft/tsdoc-config'; | |
|
|
||
| import { Debug } from './Debug'; | ||
| import { ConfigCache } from './ConfigCache'; | ||
| import recommended from './configs/recommended'; | ||
|
|
||
| const tsdocMessageIds: { [x: string]: string } = {}; | ||
|
|
||
|
|
@@ -19,6 +20,7 @@ defaultTSDocConfiguration.allTsdocMessageIds.forEach((messageId: string) => { | |
|
|
||
| interface IPlugin { | ||
| rules: { [x: string]: eslint.Rule.RuleModule }; | ||
| configs: { [x: string]: eslint.Linter.Config }; | ||
| } | ||
|
Comment on lines
21
to
24
|
||
|
|
||
| function getRootDirectoryFromContext(context: TSESLint.RuleContext<string, unknown[]>): string | undefined { | ||
|
|
@@ -68,94 +70,3 @@ const plugin: IPlugin = { | |
| const tsConfigDir: string | undefined = getRootDirectoryFromContext( | ||
| context as unknown as TSESLint.RuleContext<string, unknown[]> | ||
| ); | ||
| Debug.log(`Linting: "${sourceFilePath}"`); | ||
|
|
||
| const tsdocConfiguration: TSDocConfiguration = new TSDocConfiguration(); | ||
|
|
||
| try { | ||
| const tsdocConfigFile: TSDocConfigFile = ConfigCache.getForSourceFile(sourceFilePath, tsConfigDir); | ||
| if (!tsdocConfigFile.fileNotFound) { | ||
| if (tsdocConfigFile.hasErrors) { | ||
| context.report({ | ||
| loc: { line: 1, column: 1 }, | ||
| messageId: 'error-loading-config-file', | ||
| data: { | ||
| details: tsdocConfigFile.getErrorSummary() | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| try { | ||
| tsdocConfigFile.configureParser(tsdocConfiguration); | ||
| } catch (e) { | ||
| context.report({ | ||
| loc: { line: 1, column: 1 }, | ||
| messageId: 'error-applying-config', | ||
| data: { | ||
| details: e.message | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } catch (e) { | ||
| context.report({ | ||
| loc: { line: 1, column: 1 }, | ||
| messageId: 'error-loading-config-file', | ||
| data: { | ||
| details: `Unexpected exception: ${e.message}` | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| const tsdocParser: TSDocParser = new TSDocParser(tsdocConfiguration); | ||
|
|
||
| const sourceCode: eslint.SourceCode = context.sourceCode ?? context.getSourceCode(); | ||
| function checkCommentBlocks(): void { | ||
| for (const comment of sourceCode.getAllComments()) { | ||
| if (comment.type !== 'Block') { | ||
| continue; | ||
| } | ||
| if (!comment.range) { | ||
| continue; | ||
| } | ||
|
|
||
| const textRange: TextRange = TextRange.fromStringRange( | ||
| sourceCode.text, | ||
| comment.range[0], | ||
| comment.range[1] | ||
| ); | ||
|
|
||
| // Smallest comment is "/***/" | ||
| if (textRange.length < 5) { | ||
| continue; | ||
| } | ||
| // Make sure it starts with "/**" | ||
| if (textRange.buffer[textRange.pos + 2] !== '*') { | ||
| continue; | ||
| } | ||
|
|
||
| const parserContext: ParserContext = tsdocParser.parseRange(textRange); | ||
| for (const message of parserContext.log.messages) { | ||
| context.report({ | ||
| loc: { | ||
| start: sourceCode.getLocFromIndex(message.textRange.pos), | ||
| end: sourceCode.getLocFromIndex(message.textRange.end) | ||
| }, | ||
| messageId: message.messageId, | ||
| data: { | ||
| unformattedText: message.unformattedText | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| Program: checkCommentBlocks | ||
| }; | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| export = plugin; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new
recommendedconfig is a public entrypoint (plugin:tsdoc/recommended) but there’s no test covering that it is exported and contains the expected rule settings. Consider adding a small unit test assertingplugin.configs.recommended.rules['tsdoc/syntax'] === 'warn'so this contract doesn’t regress.