-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
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,76 @@ | ||
# require-license | ||
|
||
💼 This rule is enabled in the ✅ `recommended` config. | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
This rule applies two validations to the `"licence"` property: | ||
|
||
- It must be a string rather than any other data type | ||
- It's value should match one of the values provided in the options | ||
|
||
Example of **incorrect** code for this rule: | ||
|
||
When the rule is configured with | ||
|
||
```ts | ||
{ | ||
"require-license": ["error", "GPL"] | ||
} | ||
``` | ||
|
||
```json | ||
{ | ||
"license": "MIT" | ||
} | ||
``` | ||
|
||
When the rule is configured with | ||
|
||
```ts | ||
{ | ||
"require-license": ["error", ["MIT", "GPL"]] | ||
} | ||
``` | ||
|
||
```json | ||
{ | ||
"license": "Apache" | ||
} | ||
``` | ||
|
||
Example of **correct** code for this rule: | ||
|
||
When the rule is configured with | ||
|
||
```ts | ||
{ | ||
"require-license": ["error", "GPL"] | ||
} | ||
``` | ||
|
||
```json | ||
{ | ||
"license": "GPL" | ||
} | ||
``` | ||
|
||
When the rule is configured with | ||
|
||
```ts | ||
{ | ||
"require-license": ["error", ["Apache", "MIT"]] | ||
} | ||
``` | ||
|
||
```json | ||
{ | ||
"license": "Apache" | ||
} | ||
``` | ||
|
||
```json | ||
{ | ||
"license": "MIT" | ||
} | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import type { AST as JsonAST } from "jsonc-eslint-parser"; | ||
|
||
import * as ESTree from "estree"; | ||
|
||
import { createRule } from "../createRule.js"; | ||
|
||
export type AllowedValues = string | string[]; | ||
export type Options = [AllowedValues]; | ||
|
||
export function generateReportData(allowed: string[]) { | ||
return { | ||
allowed: allowed.map((v) => `'${v}'`).join(","), | ||
multiple: allowed.length > 1 ? "one of" : "", | ||
}; | ||
} | ||
|
||
export const rule = createRule<Options>({ | ||
create(context) { | ||
const ruleOptions = context.options[0]; | ||
const allowedValues = Array.isArray(ruleOptions) | ||
? ruleOptions | ||
: [ruleOptions]; | ||
return { | ||
"Program > JSONExpressionStatement > JSONObjectExpression > JSONProperty[key.value=license]"( | ||
node: JsonAST.JSONProperty, | ||
) { | ||
if ( | ||
node.value.type !== "JSONLiteral" || | ||
typeof node.value.value !== "string" | ||
) { | ||
context.report({ | ||
messageId: "nonString", | ||
node: node.value as unknown as ESTree.Node, | ||
}); | ||
return; | ||
} | ||
|
||
if (!allowedValues.includes(node.value.value)) { | ||
context.report({ | ||
data: generateReportData(allowedValues), | ||
loc: node.loc, | ||
messageId: "invalidValue", | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
|
||
meta: { | ||
docs: { | ||
category: "Best Practices", | ||
description: | ||
"Enforce the 'license' field to be a specific value/values", | ||
recommended: false, | ||
}, | ||
messages: { | ||
invalidValue: '"license" must be{{multiple}} {{allowed}}"', | ||
nonString: '"license" must be a string"', | ||
}, | ||
schema: { | ||
items: [ | ||
{ | ||
oneOf: [ | ||
{ | ||
items: { | ||
type: "string", | ||
}, | ||
type: "array", | ||
}, | ||
{ | ||
type: "string", | ||
}, | ||
], | ||
}, | ||
], | ||
type: "array", | ||
}, | ||
type: "problem", | ||
}, | ||
}); |
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,62 @@ | ||
import { generateReportData, rule } from "../../rules/require-license.js"; | ||
import { ruleTester } from "./ruleTester.js"; | ||
|
||
ruleTester.run("requires-license", rule, { | ||
invalid: [ | ||
{ | ||
code: JSON.stringify({ | ||
license: "CC BY-SA", | ||
name: "some-test-package", | ||
}), | ||
errors: [ | ||
{ | ||
data: generateReportData(["GPL"]), | ||
messageId: "invalidValue", | ||
}, | ||
], | ||
options: ["GPL"], | ||
}, | ||
{ | ||
code: JSON.stringify({ | ||
license: "Apache", | ||
name: "some-test-package", | ||
}), | ||
errors: [ | ||
{ | ||
data: generateReportData(["MIT", "GPL"]), | ||
messageId: "invalidValue", | ||
}, | ||
], | ||
options: [["MIT", "GPL"]], | ||
}, | ||
{ | ||
code: JSON.stringify({ | ||
license: 1234, | ||
name: "some-test-package", | ||
}), | ||
errors: [ | ||
{ | ||
data: generateReportData(["GPL"]), | ||
messageId: "nonString", | ||
}, | ||
], | ||
options: ["GPL"], | ||
}, | ||
], | ||
valid: [ | ||
{ | ||
code: JSON.stringify({ | ||
license: "Apache", | ||
name: "some-test-package", | ||
}), | ||
options: ["Apache"], | ||
}, | ||
{ | ||
code: JSON.stringify({ | ||
license: "GPL", | ||
name: "some-test-package", | ||
}), | ||
options: [["MIT", "GPL"]], | ||
}, | ||
], | ||
}); |