Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple leading underscores in field names #179

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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