Skip to content

Commit

Permalink
- Renamed rule to valid-license,
Browse files Browse the repository at this point in the history
- Added extra valid test case for missing property
- Fix documentation
- Fix linting errors
  • Loading branch information
xenobytezero committed Feb 5, 2025
1 parent 47ccb5a commit 84289f4
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 108 deletions.
20 changes: 10 additions & 10 deletions docs/rules/require-license.md → docs/rules/valid-license.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# require-license
# valid-license

💼 This rule is enabled in the ✅ `recommended` config.

<!-- end auto-generated rule header -->

This rule applies two validations to the `"licence"` property:
This rule applies two validations to the `"license"` 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
Expand All @@ -13,9 +13,9 @@ Example of **incorrect** code for this rule:

When the rule is configured with

```ts
```json
{
"require-license": ["error", "GPL"]
"valid-license": ["error", "GPL"]
}
```

Expand All @@ -27,9 +27,9 @@ When the rule is configured with

When the rule is configured with

```ts
```json
{
"require-license": ["error", ["MIT", "GPL"]]
"valid-license": ["error", ["MIT", "GPL"]]
}
```

Expand All @@ -43,9 +43,9 @@ Example of **correct** code for this rule:

When the rule is configured with

```ts
```json
{
"require-license": ["error", "GPL"]
"valid-license": ["error", "GPL"]
}
```

Expand All @@ -57,9 +57,9 @@ When the rule is configured with

When the rule is configured with

```ts
```json
{
"require-license": ["error", ["Apache", "MIT"]]
"valid-license": ["error", ["Apache", "MIT"]]
}
```

Expand Down
72 changes: 36 additions & 36 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { rule as noEmptyFields } from "./rules/no-empty-fields.js";
import { rule as noRedundantFiles } from "./rules/no-redundant-files.js";
import { rule as orderProperties } from "./rules/order-properties.js";
import { rule as preferRepositoryShorthand } from "./rules/repository-shorthand.js";
import { rule as requireLicense } from "./rules/require-license.js";
import { rule as sortCollections } from "./rules/sort-collections.js";
import { rule as uniqueDependencies } from "./rules/unique-dependencies.js";
import { rule as validLicense } from "./rules/valid-license.js";
import { rule as validLocalDependency } from "./rules/valid-local-dependency.js";
import { rule as validName } from "./rules/valid-name.js";
import { rule as validPackageDefinition } from "./rules/valid-package-definition.js";
Expand All @@ -18,49 +18,49 @@ import { rule as validVersion } from "./rules/valid-version.js";
const require = createRequire(import.meta.url || __filename);

const { name, version } = require("../package.json") as {
name: string;
version: string;
name: string;
version: string;
};

const rules: Record<string, PackageJsonRuleModule> = {
"no-empty-fields": noEmptyFields,
"no-redundant-files": noRedundantFiles,
"order-properties": orderProperties,
"repository-shorthand": preferRepositoryShorthand,
"require-license": requireLicense,
"sort-collections": sortCollections,
"unique-dependencies": uniqueDependencies,
"valid-local-dependency": validLocalDependency,
"valid-name": validName,
"valid-package-definition": validPackageDefinition,
"valid-repository-directory": validRepositoryDirectory,
"valid-version": validVersion,
"no-empty-fields": noEmptyFields,
"no-redundant-files": noRedundantFiles,
"order-properties": orderProperties,
"repository-shorthand": preferRepositoryShorthand,
"sort-collections": sortCollections,
"unique-dependencies": uniqueDependencies,
"valid-license": validLicense,
"valid-local-dependency": validLocalDependency,
"valid-name": validName,
"valid-package-definition": validPackageDefinition,
"valid-repository-directory": validRepositoryDirectory,
"valid-version": validVersion,

/** @deprecated use 'valid-package-definition' instead */
"valid-package-def": {
...validPackageDefinition,
meta: {
...validPackageDefinition.meta,
deprecated: true,
docs: {
...validPackageDefinition.meta.docs,
recommended: false,
},
replacedBy: ["valid-package-definition"],
},
},
/** @deprecated use 'valid-package-definition' instead */
"valid-package-def": {
...validPackageDefinition,
meta: {
...validPackageDefinition.meta,
deprecated: true,
docs: {
...validPackageDefinition.meta.docs,
recommended: false,
},
replacedBy: ["valid-package-definition"],
},
},
};

export const plugin = {
meta: {
name,
version,
},
rules,
meta: {
name,
version,
},
rules,
};

export const recommendedRuleSettings = Object.fromEntries(
Object.entries(rules)
.filter(([, rule]) => rule.meta.docs?.recommended)
.map(([name]) => ["package-json/" + name, "error" as const]),
Object.entries(rules)
.filter(([, rule]) => rule.meta.docs?.recommended)
.map(([name]) => ["package-json/" + name, "error" as const]),
);
File renamed without changes.
62 changes: 0 additions & 62 deletions src/tests/rules/require-license.test.ts

This file was deleted.

74 changes: 74 additions & 0 deletions src/tests/rules/valid-license.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { generateReportData, rule } from "../../rules/valid-license.js";
import { ruleTester } from "./ruleTester.js";

ruleTester.run("valid-license", rule, {
invalid: [
// Invalid with single valid value
{
code: JSON.stringify({
license: "CC BY-SA",
name: "some-test-package",
}),
errors: [
{
data: generateReportData(["GPL"]),
messageId: "invalidValue",
},
],
options: ["GPL"],
},
// Invalid with multiple valid values
{
code: JSON.stringify({
license: "Apache",
name: "some-test-package",
}),
errors: [
{
data: generateReportData(["MIT", "GPL"]),
messageId: "invalidValue",
},
],
options: [["MIT", "GPL"]],
},
// Invalid property type
{
code: JSON.stringify({
license: 1234,
name: "some-test-package",
}),
errors: [
{
data: generateReportData(["GPL"]),
messageId: "nonString",
},
],
options: ["GPL"],
},
],
valid: [
// Valid value from single valid value
{
code: JSON.stringify({
license: "Apache",
name: "some-test-package",
}),
options: ["Apache"],
},
// Valid value from multiple valid values
{
code: JSON.stringify({
license: "GPL",
name: "some-test-package",
}),
options: [["MIT", "GPL"]],
},
// Missing property
{
code: JSON.stringify({
name: "some-test-package",
}),
options: [["MIT", "GPL"]],
},
],
});

0 comments on commit 84289f4

Please sign in to comment.