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

New: add option for camelcase (fixes #12527) #12528

Merged
merged 4 commits into from Nov 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 19 additions & 6 deletions lib/rules/camelcase.js
Expand Up @@ -28,6 +28,10 @@ module.exports = {
type: "boolean",
default: false
},
ignoreImports: {
type: "boolean",
default: false
},
properties: {
enum: ["always", "never"]
},
Expand Down Expand Up @@ -56,6 +60,7 @@ module.exports = {
const options = context.options[0] || {};
let properties = options.properties || "";
const ignoreDestructuring = options.ignoreDestructuring;
const ignoreImports = options.ignoreImports;
const allow = options.allow || [];

if (properties !== "always" && properties !== "never") {
Expand All @@ -79,7 +84,7 @@ module.exports = {
function isUnderscored(name) {

// if there's an underscore, it might be A_CONSTANT, which is okay
return name.indexOf("_") > -1 && name !== name.toUpperCase();
return name.includes("_") && name !== name.toUpperCase();
}

/**
Expand All @@ -89,9 +94,9 @@ module.exports = {
* @private
*/
function isAllowed(name) {
return allow.findIndex(
return allow.some(
entry => name === entry || name.match(new RegExp(entry, "u"))
) !== -1;
);
}

/**
Expand Down Expand Up @@ -127,7 +132,7 @@ module.exports = {
* @private
*/
function report(node) {
if (reported.indexOf(node) < 0) {
if (!reported.includes(node)) {
reported.push(node);
context.report({ node, messageId: "notCamelCase", data: { name: node.name } });
}
Expand Down Expand Up @@ -209,10 +214,18 @@ module.exports = {
}

// Check if it's an import specifier
} else if (["ImportSpecifier", "ImportNamespaceSpecifier", "ImportDefaultSpecifier"].indexOf(node.parent.type) >= 0) {
} else if (["ImportSpecifier", "ImportNamespaceSpecifier", "ImportDefaultSpecifier"].includes(node.parent.type)) {

if (node.parent.type === "ImportSpecifier" && ignoreImports) {
return;
}

// Report only if the local imported identifier is underscored
if (node.parent.local && node.parent.local.name === node.name && nameIsUnderscored) {
if (
node.parent.local &&
node.parent.local.name === node.name &&
nameIsUnderscored
) {
report(node);
}

Expand Down
29 changes: 29 additions & 0 deletions tests/lib/rules/camelcase.js
Expand Up @@ -151,6 +151,11 @@ ruleTester.run("camelcase", rule, {
code: "import { no_camelcased as camelCased, anoterCamelCased } from \"external-module\";",
parserOptions: { ecmaVersion: 6, sourceType: "module" }
},
{
kaicataldo marked this conversation as resolved.
Show resolved Hide resolved
code: "import { snake_cased } from 'mod'",
options: [{ ignoreImports: true }],
parserOptions: { ecmaVersion: 6, sourceType: "module" }
},
{
code: "function foo({ no_camelcased: camelCased }) {};",
parserOptions: { ecmaVersion: 6 }
Expand Down Expand Up @@ -532,6 +537,30 @@ ruleTester.run("camelcase", rule, {
}
]
},
{
code: "import snake_cased from 'mod'",
kaicataldo marked this conversation as resolved.
Show resolved Hide resolved
options: [{ ignoreImports: true }],
parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [
{
messageId: "notCamelCase",
data: { name: "snake_cased" },
type: "Identifier"
}
]
},
{
code: "import * as snake_cased from 'mod'",
options: [{ ignoreImports: true }],
parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [
{
messageId: "notCamelCase",
data: { name: "snake_cased" },
type: "Identifier"
}
]
},
{
code: "function foo({ no_camelcased }) {};",
parserOptions: { ecmaVersion: 6 },
Expand Down