Skip to content

Commit 1c2d528

Browse files
authored
Include current config and valid values in output when an issue is detected (#605)
* Include current config and valid values in output when an issue is detected * Lint formatting * Update cli test * Update cli.test.js
1 parent 4ce1bcb commit 1c2d528

21 files changed

+85
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"eslint": "eslint . --format=node_modules/eslint-formatter-pretty",
3535
"npmpackagejsonlint": "node dist/cli.js ./package.json",
3636
"lint": "npm run eslint && npm run npmpackagejsonlint",
37-
"test": "jest",
37+
"test": "npm run build && jest",
3838
"test:ci": "jest --runInBand",
3939
"tsc": "tsc --project tsconfig.json"
4040
},

src/rules/valid-values-author.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {isValidValue} from '../validators/valid-values';
88

99
const lintId = 'valid-values-author';
1010
const nodeName = 'author';
11-
const message = 'Invalid value for author';
1211

1312
export const ruleType = RuleType.Array;
1413

@@ -37,7 +36,12 @@ export const lint = <T>(
3736
}
3837

3938
if (!isValidValue<T>(packageJsonData, nodeName, value, validValues)) {
40-
return new LintIssue(lintId, severity, nodeName, message);
39+
return new LintIssue(
40+
lintId,
41+
severity,
42+
nodeName,
43+
`Invalid value for author. Current value is ${value}. Value values include: ${validValues.join(', ')}.`
44+
);
4145
}
4246

4347
return null;

src/rules/valid-values-engines.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {isValidValue} from '../validators/valid-values';
99

1010
const lintId = 'valid-values-engines';
1111
const nodeName = 'engines';
12-
const message = 'Invalid value for engines';
1312

1413
export const ruleType = RuleType.Array;
1514

@@ -27,7 +26,14 @@ export const lint = (
2726
const valueAsJsonString = JSON.stringify(packageJsonData[nodeName]);
2827

2928
if (!isValidValue<string>(packageJsonData, nodeName, valueAsJsonString, validValuesAsJsonString)) {
30-
return new LintIssue(lintId, severity, nodeName, message);
29+
return new LintIssue(
30+
lintId,
31+
severity,
32+
nodeName,
33+
`Invalid value for engines. Current value is ${valueAsJsonString}. Value values include: ${validValuesAsJsonString.join(
34+
', '
35+
)}.`
36+
);
3137
}
3238

3339
// eslint-disable-next-line no-restricted-syntax, guard-for-in

src/rules/valid-values-license.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {isValidValue} from '../validators/valid-values';
66

77
const lintId = 'valid-values-license';
88
const nodeName = 'license';
9-
const message = 'Invalid value for license';
109

1110
export const ruleType = RuleType.Array;
1211

@@ -27,7 +26,14 @@ export const lint = (
2726
validValues: string[]
2827
): LintIssue | null => {
2928
if (!isValidValue<string>(packageJsonData, nodeName, packageJsonData[nodeName], validValues)) {
30-
return new LintIssue(lintId, severity, nodeName, message);
29+
return new LintIssue(
30+
lintId,
31+
severity,
32+
nodeName,
33+
`Invalid value for license. Current value is ${packageJsonData[nodeName]}. Valid values include: ${validValues.join(
34+
', '
35+
)}.`
36+
);
3137
}
3238

3339
return null;

src/rules/valid-values-name-scope.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {matchValidValue} from '../validators/valid-values';
77

88
const lintId = 'valid-values-name-scope';
99
const nodeName = 'name';
10-
const message = 'Invalid value for name scope';
1110

1211
export const ruleType = RuleType.Array;
1312

@@ -30,7 +29,14 @@ export const lint = (
3029
const validRegexes = validValues.map((scope) => new RegExp(`^${scope}/`));
3130

3231
if (!matchValidValue(packageJsonData, nodeName, packageJsonData[nodeName], validRegexes)) {
33-
return new LintIssue(lintId, severity, nodeName, message);
32+
return new LintIssue(
33+
lintId,
34+
severity,
35+
nodeName,
36+
`Invalid value for name scope. Current value is ${
37+
packageJsonData[nodeName]
38+
}. Valid values include: ${validValues.join(', ')}.`
39+
);
3440
}
3541

3642
return null;

src/rules/valid-values-private.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {isValidValue} from '../validators/valid-values';
77

88
const lintId = 'valid-values-private';
99
const nodeName = 'private';
10-
const message = 'Invalid value for private';
1110

1211
export const ruleType = RuleType.Array;
1312

@@ -27,7 +26,14 @@ export const lint = (
2726
validValues: boolean[]
2827
): LintResult => {
2928
if (!isValidValue<boolean>(packageJsonData, nodeName, packageJsonData[nodeName], validValues)) {
30-
return new LintIssue(lintId, severity, nodeName, message);
29+
return new LintIssue(
30+
lintId,
31+
severity,
32+
nodeName,
33+
`Invalid value for private. Current value is ${packageJsonData[nodeName]}. Valid values include: ${validValues.join(
34+
', '
35+
)}.`
36+
);
3137
}
3238

3339
return null;

src/rules/valid-values-publishConfig.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {isValidValue} from '../validators/valid-values';
88

99
const lintId = 'valid-values-publishConfig';
1010
const nodeName = 'publishConfig';
11-
const message = 'Invalid value for publishConfig';
1211

1312
export const ruleType = RuleType.Array;
1413

@@ -26,7 +25,14 @@ export const lint = (
2625
const valueAsJsonString = JSON.stringify(packageJsonData[nodeName]);
2726

2827
if (!isValidValue<string>(packageJsonData, nodeName, valueAsJsonString, validValuesAsJsonString)) {
29-
return new LintIssue(lintId, severity, nodeName, message);
28+
return new LintIssue(
29+
lintId,
30+
severity,
31+
nodeName,
32+
`Invalid value for publishConfig. Current value is ${valueAsJsonString}. Value values include: ${validValuesAsJsonString.join(
33+
', '
34+
)}.`
35+
);
3036
}
3137
} else {
3238
return new LintIssue(lintId, severity, nodeName, 'publishConfig node has invalid data type');

test/integration/cli.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ ${figures.cross} prefer-property-order - node: - Your package.json properties a
373373
374374
./packages/packageTwo/package.json
375375
${figures.warning} license-type - node: license - Type should be a string
376-
${figures.cross} valid-values-author - node: author - Invalid value for author
376+
${figures.cross} valid-values-author - node: author - Invalid value for author. Current value is Thomas Lindner. Value values include: TC.
377377
1 error
378378
1 warning
379379
@@ -408,7 +408,7 @@ ${figures.cross} prefer-property-order - node: - Your package.json properties a
408408
409409
./packages/packageTwo/package.json
410410
${figures.warning} license-type - node: license - Type should be a string
411-
${figures.cross} valid-values-author - node: author - Invalid value for author
411+
${figures.cross} valid-values-author - node: author - Invalid value for author. Current value is Thomas Lindner. Value values include: TC.
412412
1 error
413413
1 warning
414414

test/unit/linter/linter.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,12 @@ describe('linter Unit Tests', () => {
297297
const rules = new Rules();
298298
rules.load();
299299

300-
const lintIssue = new LintIssue('valid-values-author', Severity.Error, 'author', 'Invalid value for author');
300+
const lintIssue = new LintIssue(
301+
'valid-values-author',
302+
Severity.Error,
303+
'author',
304+
'Invalid value for author. Current value is Spiderman. Value values include: Peter Parker.'
305+
);
301306
const expected = {
302307
errorCount: 1,
303308
ignoreCount: 0,

test/unit/rules/valid-values-author.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ describe('valid-values-author Unit Tests', () => {
2525
expect(response.lintId).toStrictEqual('valid-values-author');
2626
expect(response.severity).toStrictEqual('error');
2727
expect(response.node).toStrictEqual('author');
28-
expect(response.lintMessage).toStrictEqual('Invalid value for author');
28+
expect(response.lintMessage).toStrictEqual(
29+
'Invalid value for author. Current value is LastName, FirstName. Value values include: FirstName LastName, FirstName MiddleName LastName.'
30+
);
2931
});
3032
});
3133

@@ -55,7 +57,9 @@ describe('valid-values-author Unit Tests', () => {
5557
expect(response.lintId).toStrictEqual('valid-values-author');
5658
expect(response.severity).toStrictEqual('error');
5759
expect(response.node).toStrictEqual('author');
58-
expect(response.lintMessage).toStrictEqual('Invalid value for author');
60+
expect(response.lintMessage).toStrictEqual(
61+
'Invalid value for author. Current value is LastName, FirstName. Value values include: FirstName LastName, FirstName MiddleName LastName.'
62+
);
5963
});
6064
});
6165

0 commit comments

Comments
 (0)