Skip to content

Commit

Permalink
Allow multiple leading underscores in field names (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
salomoneb authored Jan 5, 2021
1 parent af35f58 commit 563a53e
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/cmd/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,19 @@ function noDescription(obj) {
}

/**
* check camelCase, but more flexible than lodash's algorithm
* @param {string} str
* @return {Boolean}
* Check if a string contains non-letter, non-number, or non-underscore chars
*
* @param {string} str
* @return {boolean}
*/
function isCamelCased(str) {
const camel = _.camelCase(str);
function isValidKilnDotNotation(str) {
// https://regex101.com/r/wapXTM/2 for an example of this regex in action

// This function is used after "toDotNotation()" in "assertCamelCasedProps()" below
// "toDotNotation" actually allows numbers, which are not valid property accessors in dot notation, but which is why we permit numbers here

// note: we're lowercasing everything because we don't care about 100% perfect camelCase (e.g. imageURL is valid),
// we just want to make sure that people aren't using weird characters that cannot be used as properties in dot notation
return str.toLowerCase() === camel.toLowerCase() || str.toLowerCase() === `_${camel}`.toLowerCase(); // lodash's camelCase() will remove leading underscores, which we allow in things like _version, _description, and _devDescription
// If the regex returns false, it means that the string is valid
return !(/[^\w\$_]/g.test(str));
}

/**
Expand All @@ -279,7 +282,7 @@ function isCamelCased(str) {
*/
function nonCamelCasedProps(obj) {
return _.reduce(obj, (errors, value, key) => {
return !isCamelCased(key) ? errors.concat(key) : errors;
return !isValidKilnDotNotation(key) ? errors.concat(key) : errors;
}, []);
}

Expand Down

0 comments on commit 563a53e

Please sign in to comment.