-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[Linter] Update linter rule to check if sdk-type exists in package.json #18597
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e69cc64
updated ```ts-package-json-sdktype``` rule to enforce the existence of
bzhang0 7718f42
added docs
bzhang0 4f78042
update linter + some tests
bzhang0 d4c8299
final fixes
bzhang0 5901318
Merge branch 'Azure:main' into issue-13214
bzhang0 6e58932
Updated ```README.md```
bzhang0 f7557d8
Update accepted values, responses, and test filess
bzhang0 15d7fa2
Merge branch 'issue-13214' of github.com:bzhang0/azure-sdk-for-js int…
bzhang0 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
47 changes: 47 additions & 0 deletions
47
common/tools/eslint-plugin-azure-sdk/docs/rules/ts-package-json-sdktype.md
This file contains 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,47 @@ | ||
# ts-package-json-sdktype | ||
|
||
Requires the existence of the `sdk-type` field and be either 'client' or 'mgmt'. | ||
|
||
## Examples | ||
|
||
### Good | ||
|
||
```json | ||
{ | ||
"sdk-type": "client" | ||
} | ||
``` | ||
|
||
```json | ||
{ | ||
"sdk-type": "mgmt" | ||
} | ||
``` | ||
|
||
### Bad | ||
|
||
```json | ||
{ | ||
"sdk-type": "invalid" | ||
} | ||
``` | ||
|
||
```json | ||
{ | ||
"sdk-type": 1 | ||
} | ||
``` | ||
|
||
```json | ||
{ | ||
"not-sdk-type": "client" | ||
} | ||
``` | ||
|
||
```json | ||
{} | ||
``` | ||
|
||
## When to turn off | ||
|
||
Only if the rule breaks. |
This file contains 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 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -2,13 +2,13 @@ | |||||
// Licensed under the MIT license. | ||||||
|
||||||
/** | ||||||
* @file Rule to force package.json's 'sdk-type' value to be valid | ||||||
* @author Arpan Laha | ||||||
* @file Rule to force package.json's 'sdk-type' value to be valid (and exist) | ||||||
* @author Arpan Laha, Ben Zhang | ||||||
*/ | ||||||
|
||||||
import { Rule } from "eslint"; | ||||||
import { Property } from "estree"; | ||||||
import { getRuleMetaData, stripPath } from "../utils"; | ||||||
import { getRuleMetaData, getVerifiers, stripPath } from "../utils"; | ||||||
|
||||||
//------------------------------------------------------------------------------ | ||||||
// Rule Definition | ||||||
|
@@ -17,41 +17,46 @@ import { getRuleMetaData, stripPath } from "../utils"; | |||||
export = { | ||||||
meta: getRuleMetaData( | ||||||
"ts-package-json-sdktype", | ||||||
"force package.json's sdk-type value to contain be 'client' or 'mgmt'" | ||||||
"force package.json's sdk-type to exist and for its value to be 'client' or 'mgmt'", | ||||||
"code" | ||||||
), | ||||||
create: (context: Rule.RuleContext): Rule.RuleListener => | ||||||
stripPath(context.getFilename()) === "package.json" | ||||||
create: (context: Rule.RuleContext): Rule.RuleListener => { | ||||||
const verifiers = getVerifiers(context, { | ||||||
outer: "sdk-type" | ||||||
}); | ||||||
return stripPath(context.getFilename()) === "package.json" | ||||||
? ({ | ||||||
// callback functions | ||||||
|
||||||
// check the node corresponding to sdk-type to see if its value contains "client" or "mgmt" | ||||||
"ExpressionStatement > ObjectExpression > Property[key.value='sdk-type']": ( | ||||||
node: Property | ||||||
) => { | ||||||
if (!node) { | ||||||
// Track1 packages don't have this property. Stop checking | ||||||
return; | ||||||
} | ||||||
|
||||||
const { value } = node; | ||||||
if (value.type !== "Literal" || typeof value.value !== "string") { | ||||||
context.report({ | ||||||
node: node.value, | ||||||
message: "sdk-type is not set to a string" | ||||||
}); | ||||||
return; | ||||||
} | ||||||
|
||||||
const strValue = stripPath(value.value); | ||||||
|
||||||
if (!["client", "mgmt"].includes(strValue)) { | ||||||
context.report({ | ||||||
node: node.value, | ||||||
message: "sdk-type is not set to `client` or `mgmt`" | ||||||
}); | ||||||
return; | ||||||
} | ||||||
// callback functions | ||||||
|
||||||
// check to see if package.json includes 'sdk-type' | ||||||
"ExpressionStatement > ObjectExpression": verifiers.existsInFile, | ||||||
|
||||||
// check the node corresponding to sdk-type to see if its value contains "client" or "mgmt" | ||||||
"ExpressionStatement > ObjectExpression > Property[key.value='sdk-type']": ( | ||||||
node: Property | ||||||
): void => { | ||||||
const { value } = node; | ||||||
|
||||||
// check for valid type | ||||||
if (value.type !== "Literal" || typeof value.value !== "string") { | ||||||
context.report({ | ||||||
node: node.value, | ||||||
message: "sdk-type is not set to a string" | ||||||
}); | ||||||
return; | ||||||
} | ||||||
} as Rule.RuleListener) | ||||||
: {} | ||||||
|
||||||
const strValue = stripPath(value.value); | ||||||
|
||||||
if (!["client", "mgmt"].includes(strValue)) { | ||||||
context.report({ | ||||||
node: node.value, | ||||||
message: "sdk-type is not set to `client` or `mgmt`" | ||||||
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. Perhaps it could be useful to print the unrecognized value too?
Suggested change
Please note the error messages in the tests will need to be updated too. |
||||||
}); | ||||||
return; | ||||||
} | ||||||
} | ||||||
} as Rule.RuleListener) | ||||||
: {}; | ||||||
} | ||||||
}; |
This file contains 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.
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.
Please add
"utility"
into the allowed list. It was just added earlier in #18402.