-
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.
feat(require-author): add new
require-author
rule
This change adds the first or our new `require-` rules, and creates some foundational plumbing to make it super easy to add new require rules. I've only done author in this PR. Assuming this all looks good, we can quickly knock all the rest of the require rules in one shot.
- Loading branch information
1 parent
336f1bf
commit 81aecf0
Showing
7 changed files
with
132 additions
and
1 deletion.
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
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,25 @@ | ||
# require-author | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
This rule checks for the existence of the `"author"` property in a package.json, | ||
and reports a violation if it doesn't exist. | ||
|
||
Example of **incorrect** code for this rule: | ||
|
||
```json | ||
{ | ||
"name": "thee-silver-mt-zion", | ||
"version": "13.0.0" | ||
} | ||
``` | ||
|
||
Example of **correct** code for this rule: | ||
|
||
```json | ||
{ | ||
"name": "thee-silver-mt-zion", | ||
"version": "13.0.0", | ||
"author": "Jessica Moss" | ||
} | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { PackageJsonRuleModule } from "../createRule.js"; | ||
|
||
import { createRequirePropertyRule } from "../utils/createRequirePropertyRule.js"; | ||
|
||
interface PropertyRule { | ||
isRecommended: boolean; | ||
propertyName: string; | ||
} | ||
|
||
// List of all properties we want to create require- rules for. | ||
const properties = [ | ||
{ isRecommended: false, propertyName: "author" }, | ||
] satisfies PropertyRule[]; | ||
|
||
/** All require- flavor of rules */ | ||
const rules: Record<string, PackageJsonRuleModule> = {}; | ||
|
||
// Create all require- rules | ||
for (const { isRecommended, propertyName } of properties) { | ||
rules[`require-${propertyName}`] = createRequirePropertyRule( | ||
propertyName, | ||
isRecommended, | ||
); | ||
} | ||
|
||
export { rules }; |
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,35 @@ | ||
import { rules } from "../../rules/require-properties.js"; | ||
import { ruleTester } from "./ruleTester.js"; | ||
|
||
ruleTester.run("require-author", rules["require-author"], { | ||
invalid: [ | ||
{ | ||
code: "{}", | ||
errors: [ | ||
{ | ||
data: { property: "author" }, | ||
line: 1, | ||
messageId: "missing", | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `{ | ||
"name": "foo", | ||
"version": "1.0.0" | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
data: { property: "author" }, | ||
line: 1, | ||
messageId: "missing", | ||
}, | ||
], | ||
}, | ||
], | ||
valid: [ | ||
`{ "main": "./index.js", "author": "Sophie Trudeau" }`, | ||
`{ "author": "Jessica Moss" }`, | ||
], | ||
}); |
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,42 @@ | ||
import { createRule } from "../createRule.js"; | ||
|
||
/** | ||
* Given a top-level property name, create a rule that requires that property to be present. | ||
* Optionally, include it in the recommended config. | ||
*/ | ||
export const createRequirePropertyRule = ( | ||
propertyName: string, | ||
isRecommended = false, | ||
) => { | ||
return createRule({ | ||
create(context) { | ||
let hasSeen = false; | ||
return { | ||
[`Program > JSONExpressionStatement > JSONObjectExpression > JSONProperty[key.value=${propertyName}]`]: | ||
() => { | ||
hasSeen = true; | ||
}, | ||
"Program:exit": () => { | ||
if (!hasSeen) { | ||
context.report({ | ||
data: { property: propertyName }, | ||
messageId: "missing", | ||
node: context.sourceCode.ast, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
meta: { | ||
docs: { | ||
description: `Requires the \`${propertyName}\` property to be present.`, | ||
recommended: isRecommended, | ||
}, | ||
messages: { | ||
missing: "Property '{{property}}' is required.", | ||
}, | ||
schema: [], | ||
type: "suggestion", | ||
}, | ||
}); | ||
}; |