-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[eslint-plugin-azure-sdk] New rule to make sure TSdoc comments for private members don't use internal tags #18761
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
006d96a
worked on linter private member internal tag rule
hamsa-s 932c893
Merge remote-tracking branch 'upstream/main' into linter
hamsa-s fef3e95
fixed linter errors in packages and removed exportedSettings
hamsa-s 9c2c42a
Merge remote-tracking branch 'upstream/main' into linter
hamsa-s 3f9ce71
fixed formatting in files with linter errors
hamsa-s 3df2496
removed excluding code
hamsa-s 40950a7
removed interfaces and modules check
hamsa-s 83d5aec
Merge remote-tracking branch 'upstream/main' into linter
hamsa-s 134301f
checking for all syntax nodes with accessibility modifier
hamsa-s 4f0cb96
Merge remote-tracking branch 'upstream/main' into linter
hamsa-s 341e3b8
fixed sorted imports
hamsa-s fc27d94
Changed to warning
hamsa-s 22aafa7
style changes
hamsa-s 231b76b
Merge remote-tracking branch 'upstream/main' into linter
hamsa-s 70a40ca
fixed import sorting
hamsa-s 95536a7
Fixed package.json
hamsa-s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
common/tools/eslint-plugin-azure-sdk/src/rules/ts-doc-internal-private-member.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| /** | ||
| * @file Rule to check if a private class member is tagged with @internal | ||
| * @author Hamsa Shankar | ||
| */ | ||
|
|
||
| import { ParserServices, TSESTree } from "@typescript-eslint/experimental-utils"; | ||
| import { ParserWeakMapESTreeToTSNode } from "@typescript-eslint/typescript-estree/dist/parser-options"; | ||
| import { Rule } from "eslint"; | ||
| import { Node } from "estree"; | ||
| import { readFileSync } from "fs"; | ||
| import { sync as globSync } from "glob"; | ||
| import { relative } from "path"; | ||
| import { Modifier, SyntaxKind } from "typescript"; | ||
| import { getRuleMetaData } from "../utils"; | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Rule Definition | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| /** | ||
| * Helper method for reporting on a node | ||
| * @param node the Node being operated on | ||
| * @param context the ESLint runtime context | ||
| * @param converter a converter from TSESTree Nodes to TSNodes | ||
| * @param typeChecker the TypeScript TypeChecker | ||
| * @throws if the Node passes throught the initial checks and has an internal tag | ||
| */ | ||
| const reportInternal = ( | ||
| node: Node, | ||
| context: Rule.RuleContext, | ||
| converter: ParserWeakMapESTreeToTSNode, | ||
| ): void => { | ||
| const tsNode = converter.get(node as TSESTree.Node) as any; | ||
|
|
||
| const modifiers = converter.get(node as TSESTree.Node).modifiers; | ||
hamsa-s marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // if type is internal and has a TSDoc and is a private member | ||
| if (tsNode.jsDoc !== undefined | ||
| && modifiers !== undefined && modifiers.some( | ||
| (modifier: Modifier): boolean => modifier.kind === SyntaxKind.PrivateKeyword) | ||
| ) { | ||
hamsa-s marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // fetch all tags | ||
| let TSDocTags: string[] = []; | ||
| tsNode.jsDoc.forEach((TSDocComment: any): void => { | ||
| TSDocTags = TSDocTags.concat( | ||
| TSDocComment.tags !== undefined | ||
| ? TSDocComment.tags.map((TSDocTag: any): string => TSDocTag.tagName.escapedText) | ||
jeremymeng marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| : [] | ||
| ); | ||
| }); | ||
|
|
||
| // see if any tags match internal | ||
| if (TSDocTags.some((TSDocTag: string): boolean => /(internal)/.test(TSDocTag))) { | ||
| context.report({ | ||
| node: node, | ||
| message: "private class members should not include an @internal tag" | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Determine whether this rule should examine a given file | ||
| * @param fileName the filename of the file in question | ||
| * @param exclude the list of files excluded by TypeDoc (other than those in node_modues) | ||
| * @returns false if not in src or is excluded by TypeDoc | ||
| */ | ||
| const shouldExamineFile = (fileName: string, exclude: string[]): boolean => { | ||
| if (!/src/.test(fileName)) { | ||
| return false; | ||
| } | ||
| const relativePath = relative("", fileName).replace(/\\/g, "/"); | ||
| return !exclude.includes(relativePath); | ||
| }; | ||
|
|
||
| let exclude: string[] = []; | ||
| try { | ||
| const typeDocText = readFileSync("typedoc.json", "utf8"); | ||
| const typeDoc = JSON.parse(typeDocText); | ||
|
|
||
| // if typeDoc.exclude exists, add all files matching the glob patterns to exclude | ||
| if (typeDoc.exclude !== undefined) { | ||
| typeDoc.exclude.forEach((excludedGlob: string): void => { | ||
| exclude = exclude.concat( | ||
| globSync(excludedGlob).filter( | ||
| (excludeFile: string): boolean => !/node_modules/.test(excludeFile) | ||
| ) | ||
| ); | ||
| }); | ||
| } | ||
| } catch (err) { | ||
| exclude = []; | ||
| } | ||
deyaaeldeen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
deyaaeldeen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| export = { | ||
| meta: getRuleMetaData( | ||
| "ts-doc-internal-private-member", | ||
| "requires TSDoc comments to not include an '@internal' tag if the object is private" | ||
| ), | ||
| create: (context: Rule.RuleContext): Rule.RuleListener => { | ||
| const fileName = context.getFilename(); | ||
|
|
||
| const parserServices = context.parserServices as ParserServices; | ||
| if ( | ||
| parserServices.program === undefined || | ||
| parserServices.esTreeNodeToTSNodeMap === undefined | ||
| ) { | ||
| return {}; | ||
| } | ||
|
|
||
| const converter = parserServices.esTreeNodeToTSNodeMap; | ||
|
|
||
| return shouldExamineFile(fileName, exclude) | ||
deyaaeldeen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ? { | ||
| // callback functions | ||
|
|
||
| // container declarations | ||
| ":matches(TSInterfaceDeclaration, ClassDeclaration, TSModuleDeclaration)": ( | ||
| node: Node | ||
| ): void => reportInternal(node, context, converter), | ||
|
|
||
| // functions | ||
| ":function": (node: Node): void => { | ||
| reportInternal(node, context, converter); | ||
| } | ||
| } | ||
| : {}; | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
common/tools/eslint-plugin-azure-sdk/tests/rules/ts-doc-internal-private-member.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| /** | ||
| * @file Testing the ts-doc-internal-private-member rule. | ||
| * @author Hamsa Shankar | ||
| */ | ||
|
|
||
| import rule from "../../src/rules/ts-doc-internal-private-member"; | ||
| import { RuleTester } from "eslint"; | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Tests | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| parser: require.resolve("@typescript-eslint/parser"), | ||
| parserOptions: { | ||
| createDefaultProgram: true, | ||
| project: "./tsconfig.json" | ||
| }, | ||
| settings: { | ||
| exported: [] | ||
| } | ||
| }); | ||
|
|
||
| ruleTester.run("ts-doc-internal-private-member", rule, { | ||
| valid: [ | ||
| // class | ||
| { | ||
| code: ` | ||
| /** | ||
| * Other documentation | ||
| * @internal | ||
| */ | ||
| class ExampleClass {}`, | ||
| filename: "src/test.ts" | ||
| }, | ||
| { | ||
| code: ` | ||
| /** | ||
| * Other documentation | ||
| */ | ||
| private class ExampleClass {}`, | ||
| filename: "src/test.ts" | ||
| }, | ||
| // interface | ||
| { | ||
| code: ` | ||
| /** | ||
| * Other documentation | ||
| * @internal | ||
| * @hidden | ||
| */ | ||
| interface ExampleInterface {}`, | ||
| filename: "src/test.ts" | ||
| }, | ||
| { | ||
| code: ` | ||
| /** | ||
| * Other documentation | ||
| */ | ||
| private interface ExampleInterface {}`, | ||
| filename: "src/test.ts" | ||
| }, | ||
| // function | ||
| { | ||
| code: ` | ||
| /** | ||
| * Other documentation | ||
| * @internal | ||
| */ | ||
| function ExampleFunction() {}`, | ||
| filename: "src/test.ts" | ||
| }, | ||
| { | ||
| code: ` | ||
| /** | ||
| * Other documentation | ||
| */ | ||
| private function ExampleFunction() {}`, | ||
| filename: "src/test.ts" | ||
| } | ||
| ], | ||
| invalid: [ | ||
| // class | ||
| { | ||
| code: ` | ||
| /** | ||
| * Other documentation | ||
| * @internal | ||
| */ | ||
| private class ExampleClass { | ||
| }`, | ||
| filename: "src/test.ts", | ||
| errors: [ | ||
| { | ||
| message: "private class members should not include an @internal tag" | ||
| } | ||
| ] | ||
| }, | ||
| // interface | ||
| { | ||
| code: ` | ||
| /** | ||
| * Other documentation | ||
| * @internal | ||
| */ | ||
| private interface ExampleInterface {}`, | ||
| filename: "src/test.ts", | ||
| errors: [ | ||
| { | ||
| message: "private class members should not include an @internal tag" | ||
| } | ||
| ] | ||
| }, | ||
| // function | ||
| { | ||
| code: ` | ||
| /** | ||
| * Other documentation | ||
| * @internal | ||
| * @param {object} len - length | ||
| */ | ||
| private function ExampleFunction(len) {}`, | ||
| filename: "src/test.ts", | ||
| errors: [ | ||
| { | ||
| message: "private class members should not include an @internal tag" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| }); | ||
deyaaeldeen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.