From 1dd158f2a5af881c5c9d2d66a41099efb3f20e36 Mon Sep 17 00:00:00 2001 From: tclindner Date: Sat, 29 Jun 2019 16:01:15 -0500 Subject: [PATCH] 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 --- src/rules/name-format.js | 26 +++++++++++++++++++++----- src/validators/format.js | 4 ---- test/unit/rules/name-format.test.js | 2 +- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/rules/name-format.js b/src/rules/name-format.js index 5acf8cdb..9817fb8a 100755 --- a/src/rules/name-format.js +++ b/src/rules/name-format.js @@ -3,16 +3,32 @@ const LintIssue = require('./../LintIssue'); const lintId = 'name-format'; const nodeName = 'name'; -const message = 'Format should be all lowercase'; const ruleType = 'standard'; +const maxLength = 214; const lint = (packageJsonData, severity) => { - if (!isLowercase(packageJsonData, nodeName)) { - return new LintIssue(lintId, severity, nodeName, message); + if (!packageJsonData.hasOwnProperty(nodeName)) { + return true; + } + + const name = packageJsonData[nodeName]; + + if (!isLowercase(name)) { + return new LintIssue(lintId, severity, nodeName, 'Format should be all lowercase'); + } + + if (name.length > maxLength) { + return new LintIssue(lintId, severity, nodeName, `name should be less than or equal to ${maxLength} characters.`); + } + + if (name.startsWith('.') || name.startsWith('_')) { + return new LintIssue(lintId, severity, nodeName, 'name should not start with . or _'); } return true; }; -module.exports.lint = lint; -module.exports.ruleType = ruleType; +module.exports = { + lint, + ruleType +}; diff --git a/src/validators/format.js b/src/validators/format.js index 398ed8d3..00dddab7 100755 --- a/src/validators/format.js +++ b/src/validators/format.js @@ -6,10 +6,6 @@ const semver = require('semver'); * @return {boolean} True if the string is lowercase or is missing. False if it is not. */ const isLowercase = name => { - if (typeof name !== 'string') { - return true; - } - return name === name.toLowerCase(); }; diff --git a/test/unit/rules/name-format.test.js b/test/unit/rules/name-format.test.js index d3ff29d3..7019e421 100755 --- a/test/unit/rules/name-format.test.js +++ b/test/unit/rules/name-format.test.js @@ -10,7 +10,7 @@ describe('name-format Unit Tests', () => { }); describe('when package.json has node with incorrect format', () => { - test('LintIssue object should be returned', () => { + test('not lowercase - LintIssue object should be returned', () => { const packageJsonData = { name: 'ImNotLowercase' };