Skip to content

Commit 1dd158f

Browse files
authored
Name format (#118)
* Remove validator and add ajv-errors * Add new property exist module * Add the ability to add exceptions Closes #93 * Update format.test.js * Update format.js * Update format.js * Enhance name format rule Closes #115
1 parent 973c603 commit 1dd158f

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

src/rules/name-format.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,32 @@ const LintIssue = require('./../LintIssue');
33

44
const lintId = 'name-format';
55
const nodeName = 'name';
6-
const message = 'Format should be all lowercase';
76
const ruleType = 'standard';
7+
const maxLength = 214;
88

99
const lint = (packageJsonData, severity) => {
10-
if (!isLowercase(packageJsonData, nodeName)) {
11-
return new LintIssue(lintId, severity, nodeName, message);
10+
if (!packageJsonData.hasOwnProperty(nodeName)) {
11+
return true;
12+
}
13+
14+
const name = packageJsonData[nodeName];
15+
16+
if (!isLowercase(name)) {
17+
return new LintIssue(lintId, severity, nodeName, 'Format should be all lowercase');
18+
}
19+
20+
if (name.length > maxLength) {
21+
return new LintIssue(lintId, severity, nodeName, `name should be less than or equal to ${maxLength} characters.`);
22+
}
23+
24+
if (name.startsWith('.') || name.startsWith('_')) {
25+
return new LintIssue(lintId, severity, nodeName, 'name should not start with . or _');
1226
}
1327

1428
return true;
1529
};
1630

17-
module.exports.lint = lint;
18-
module.exports.ruleType = ruleType;
31+
module.exports = {
32+
lint,
33+
ruleType
34+
};

src/validators/format.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ const semver = require('semver');
66
* @return {boolean} True if the string is lowercase or is missing. False if it is not.
77
*/
88
const isLowercase = name => {
9-
if (typeof name !== 'string') {
10-
return true;
11-
}
12-
139
return name === name.toLowerCase();
1410
};
1511

test/unit/rules/name-format.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('name-format Unit Tests', () => {
1010
});
1111

1212
describe('when package.json has node with incorrect format', () => {
13-
test('LintIssue object should be returned', () => {
13+
test('not lowercase - LintIssue object should be returned', () => {
1414
const packageJsonData = {
1515
name: 'ImNotLowercase'
1616
};

0 commit comments

Comments
 (0)