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

Variable Declarations & Callee Behavior #264

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
48 changes: 30 additions & 18 deletions lib/rules/classnames-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const docsUrl = require('../util/docsUrl');
const customConfig = require('../util/customConfig');
const astUtil = require('../util/ast');
const removeDuplicatesFromClassnamesAndWhitespaces = require('../util/removeDuplicatesFromClassnamesAndWhitespaces');
const getOption = require('../util/settings');
const { getOption, normalizeCallees } = require('../util/settings');
const parserUtil = require('../util/parser');
const order = require('../util/prettier/order');
const createContextFallback = require('tailwindcss/lib/lib/setupContextUtils').createContext;
Expand Down Expand Up @@ -68,12 +68,14 @@ module.exports = {
},

create: function (context) {
const callees = getOption(context, 'callees');
const callees = normalizeCallees(getOption(context, 'callees'));
const skipClassAttribute = getOption(context, 'skipClassAttribute');
const tags = getOption(context, 'tags');
const twConfig = getOption(context, 'config');
const classRegex = getOption(context, 'classRegex');
const removeDuplicates = getOption(context, 'removeDuplicates');
const classDeclarationRegex = getOption(context, 'declarationRegex');
const skipClassDeclaration = getOption(context, 'skipClassDeclaration');

const mergedConfig = customConfig.resolve(twConfig);
const contextFallback = // Set the created contextFallback in the cache if it does not exist yet.
Expand All @@ -92,7 +94,7 @@ module.exports = {
* @param {ASTNode} arg The child node of node
* @returns {void}
*/
const sortNodeArgumentValue = (node, arg = null) => {
const sortNodeArgumentValue = (node, arg = null, calleeKey = false) => {
let originalClassNamesValue = null;
let start = null;
let end = null;
Expand All @@ -114,34 +116,33 @@ module.exports = {
return;
case 'TemplateLiteral':
arg.expressions.forEach((exp) => {
sortNodeArgumentValue(node, exp);
sortNodeArgumentValue(node, exp, calleeKey);
});
arg.quasis.forEach((quasis) => {
sortNodeArgumentValue(node, quasis);
sortNodeArgumentValue(node, quasis, calleeKey);
});
return;
case 'ConditionalExpression':
sortNodeArgumentValue(node, arg.consequent);
sortNodeArgumentValue(node, arg.alternate);
sortNodeArgumentValue(node, arg.consequent, calleeKey);
sortNodeArgumentValue(node, arg.alternate, calleeKey);
return;
case 'LogicalExpression':
sortNodeArgumentValue(node, arg.right);
sortNodeArgumentValue(node, arg.right, calleeKey);
return;
case 'ArrayExpression':
arg.elements.forEach((el) => {
sortNodeArgumentValue(node, el);
sortNodeArgumentValue(node, el, calleeKey);
});
return;
case 'ObjectExpression':
const isUsedByClassNamesPlugin = node.callee && node.callee.name === 'classnames';
const isVue = node.key && node.key.type === 'VDirectiveKey';
arg.properties.forEach((prop) => {
const propVal = isUsedByClassNamesPlugin || isVue ? prop.key : prop.value;
sortNodeArgumentValue(node, propVal);
const propVal = calleeKey || isVue ? prop.key : prop.value;
sortNodeArgumentValue(node, propVal, calleeKey);
});
return;
case 'Property':
sortNodeArgumentValue(node, arg.key);
sortNodeArgumentValue(node, arg.key, calleeKey);
break;
case 'Literal':
originalClassNamesValue = arg.value;
Expand Down Expand Up @@ -220,20 +221,31 @@ module.exports = {
};

const callExpressionVisitor = function (node) {
const calleeStr = astUtil.calleeToString(node.callee);
if (callees.findIndex((name) => calleeStr === name) === -1) {
const calleeType = callees[astUtil.calleeToString(node.callee)];
if (!calleeType) {
return;
}

node.arguments.forEach((arg) => {
sortNodeArgumentValue(node, arg);
sortNodeArgumentValue(node, arg, calleeType === 'key');
});
};

const variableDeclaratorVistor = function (node) {
if (!astUtil.isVariableDeclaration(node, classDeclarationRegex) || skipClassDeclaration) {
return;
}
if (!astUtil.isValidDeclaratorValue(node)) {
return;
}
sortNodeArgumentValue(node, node.init);
};

const scriptVisitor = {
JSXAttribute: attributeVisitor,
TextAttribute: attributeVisitor,
CallExpression: callExpressionVisitor,
VariableDeclarator: variableDeclaratorVistor,
TaggedTemplateExpression: function (node) {
if (!tags.includes(node.tag.name)) {
return;
Expand All @@ -255,12 +267,12 @@ module.exports = {
case astUtil.isVLiteralValue(node):
sortNodeArgumentValue(node, null);
break;
case astUtil.isArrayExpression(node):
case astUtil.isVArrayExpression(node):
node.value.expression.elements.forEach((arg) => {
sortNodeArgumentValue(node, arg);
});
break;
case astUtil.isObjectExpression(node):
case astUtil.isVObjectExpression(node):
node.value.expression.properties.forEach((prop) => {
sortNodeArgumentValue(node, prop);
});
Expand Down
48 changes: 30 additions & 18 deletions lib/rules/enforces-negative-arbitrary-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const docsUrl = require('../util/docsUrl');
const customConfig = require('../util/customConfig');
const astUtil = require('../util/ast');
const groupUtil = require('../util/groupMethods');
const getOption = require('../util/settings');
const { getOption, normalizeCallees } = require('../util/settings');
const parserUtil = require('../util/parser');

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -60,11 +60,13 @@ module.exports = {
},

create: function (context) {
const callees = getOption(context, 'callees');
const callees = normalizeCallees(getOption(context, 'callees'));
const skipClassAttribute = getOption(context, 'skipClassAttribute');
const tags = getOption(context, 'tags');
const twConfig = getOption(context, 'config');
const classRegex = getOption(context, 'classRegex');
const classDeclarationRegex = getOption(context, 'declarationRegex');
const skipClassDeclaration = getOption(context, 'skipClassDeclaration');

const mergedConfig = customConfig.resolve(twConfig);

Expand All @@ -78,7 +80,7 @@ module.exports = {
* @param {ASTNode} arg The child node of node
* @returns {void}
*/
const parseForNegativeArbitraryClassNames = (node, arg = null) => {
const parseForNegativeArbitraryClassNames = (node, arg = null, calleeKey = false) => {
let originalClassNamesValue = null;
if (arg === null) {
originalClassNamesValue = astUtil.extractValueFromNode(node);
Expand All @@ -88,34 +90,33 @@ module.exports = {
return;
case 'TemplateLiteral':
arg.expressions.forEach((exp) => {
parseForNegativeArbitraryClassNames(node, exp);
parseForNegativeArbitraryClassNames(node, exp, calleeKey);
});
arg.quasis.forEach((quasis) => {
parseForNegativeArbitraryClassNames(node, quasis);
parseForNegativeArbitraryClassNames(node, quasis, calleeKey);
});
return;
case 'ConditionalExpression':
parseForNegativeArbitraryClassNames(node, arg.consequent);
parseForNegativeArbitraryClassNames(node, arg.alternate);
parseForNegativeArbitraryClassNames(node, arg.consequent, calleeKey);
parseForNegativeArbitraryClassNames(node, arg.alternate, calleeKey);
return;
case 'LogicalExpression':
parseForNegativeArbitraryClassNames(node, arg.right);
parseForNegativeArbitraryClassNames(node, arg.right, calleeKey);
return;
case 'ArrayExpression':
arg.elements.forEach((el) => {
parseForNegativeArbitraryClassNames(node, el);
parseForNegativeArbitraryClassNames(node, el, calleeKey);
});
return;
case 'ObjectExpression':
const isUsedByClassNamesPlugin = node.callee && node.callee.name === 'classnames';
const isVue = node.key && node.key.type === 'VDirectiveKey';
arg.properties.forEach((prop) => {
const propVal = isUsedByClassNamesPlugin || isVue ? prop.key : prop.value;
parseForNegativeArbitraryClassNames(node, propVal);
const propVal = calleeKey || isVue ? prop.key : prop.value;
parseForNegativeArbitraryClassNames(node, propVal, calleeKey);
});
return;
case 'Property':
parseForNegativeArbitraryClassNames(node, arg.key);
parseForNegativeArbitraryClassNames(node, arg.key, calleeKey);
return;
case 'Literal':
originalClassNamesValue = arg.value;
Expand Down Expand Up @@ -165,19 +166,30 @@ module.exports = {
};

const callExpressionVisitor = function (node) {
const calleeStr = astUtil.calleeToString(node.callee);
if (callees.findIndex((name) => calleeStr === name) === -1) {
const calleeType = callees[astUtil.calleeToString(node.callee)];
if (!calleeType) {
return;
}
node.arguments.forEach((arg) => {
parseForNegativeArbitraryClassNames(node, arg);
parseForNegativeArbitraryClassNames(node, arg, calleeType === 'key');
});
};

const variableDeclaratorVistor = function (node) {
if (!astUtil.isVariableDeclaration(node, classDeclarationRegex) || skipClassDeclaration) {
return;
}
if (!astUtil.isValidDeclaratorValue(node)) {
return;
}
parseForNegativeArbitraryClassNames(node, node.init);
};

const scriptVisitor = {
JSXAttribute: attributeVisitor,
TextAttribute: attributeVisitor,
CallExpression: callExpressionVisitor,
VariableDeclarator: variableDeclaratorVistor,
TaggedTemplateExpression: function (node) {
if (!tags.includes(node.tag.name)) {
return;
Expand All @@ -199,12 +211,12 @@ module.exports = {
case astUtil.isVLiteralValue(node):
parseForNegativeArbitraryClassNames(node);
break;
case astUtil.isArrayExpression(node):
case astUtil.isVArrayExpression(node):
node.value.expression.elements.forEach((arg) => {
parseForNegativeArbitraryClassNames(node, arg);
});
break;
case astUtil.isObjectExpression(node):
case astUtil.isVObjectExpression(node):
node.value.expression.properties.forEach((prop) => {
parseForNegativeArbitraryClassNames(node, prop);
});
Expand Down
48 changes: 30 additions & 18 deletions lib/rules/enforces-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const defaultGroups = require('../config/groups').groups;
const customConfig = require('../util/customConfig');
const astUtil = require('../util/ast');
const groupUtil = require('../util/groupMethods');
const getOption = require('../util/settings');
const { getOption, normalizeCallees } = require('../util/settings');
const parserUtil = require('../util/parser');

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -61,11 +61,13 @@ module.exports = {
},

create: function (context) {
const callees = getOption(context, 'callees');
const callees = normalizeCallees(getOption(context, 'callees'));
const skipClassAttribute = getOption(context, 'skipClassAttribute');
const tags = getOption(context, 'tags');
const twConfig = getOption(context, 'config');
const classRegex = getOption(context, 'classRegex');
const classDeclarationRegex = getOption(context, 'declarationRegex');
const skipClassDeclaration = getOption(context, 'skipClassDeclaration');

const mergedConfig = customConfig.resolve(twConfig);

Expand Down Expand Up @@ -125,7 +127,7 @@ module.exports = {
* @param {ASTNode} arg The child node of node
* @returns {void}
*/
const parseForShorthandCandidates = (node, arg = null) => {
const parseForShorthandCandidates = (node, arg = null, calleeKey = false) => {
let originalClassNamesValue = null;
let start = null;
let end = null;
Expand All @@ -148,34 +150,33 @@ module.exports = {
return;
case 'TemplateLiteral':
arg.expressions.forEach((exp) => {
parseForShorthandCandidates(node, exp);
parseForShorthandCandidates(node, exp, calleeKey);
});
arg.quasis.forEach((quasis) => {
parseForShorthandCandidates(node, quasis);
parseForShorthandCandidates(node, quasis, calleeKey);
});
return;
case 'ConditionalExpression':
parseForShorthandCandidates(node, arg.consequent);
parseForShorthandCandidates(node, arg.alternate);
parseForShorthandCandidates(node, arg.consequent, calleeKey);
parseForShorthandCandidates(node, arg.alternate, calleeKey);
return;
case 'LogicalExpression':
parseForShorthandCandidates(node, arg.right);
parseForShorthandCandidates(node, arg.right, calleeKey);
return;
case 'ArrayExpression':
arg.elements.forEach((el) => {
parseForShorthandCandidates(node, el);
parseForShorthandCandidates(node, el, calleeKey);
});
return;
case 'ObjectExpression':
const isUsedByClassNamesPlugin = node.callee && node.callee.name === 'classnames';
const isVue = node.key && node.key.type === 'VDirectiveKey';
arg.properties.forEach((prop) => {
const propVal = isUsedByClassNamesPlugin || isVue ? prop.key : prop.value;
parseForShorthandCandidates(node, propVal);
const propVal = calleeKey || isVue ? prop.key : prop.value;
parseForShorthandCandidates(node, propVal, calleeKey);
});
return;
case 'Property':
parseForShorthandCandidates(node, arg.key);
parseForShorthandCandidates(node, arg.key, calleeKey);
return;

case 'Literal':
Expand Down Expand Up @@ -443,20 +444,31 @@ module.exports = {
};

const callExpressionVisitor = function (node) {
const calleeStr = astUtil.calleeToString(node.callee);
if (callees.findIndex((name) => calleeStr === name) === -1) {
const calleeType = callees[astUtil.calleeToString(node.callee)];
if (!calleeType) {
return;
}

node.arguments.forEach((arg) => {
parseForShorthandCandidates(node, arg);
parseForShorthandCandidates(node, arg, calleeType === 'key');
});
};

const variableDeclaratorVistor = function (node) {
if (!astUtil.isVariableDeclaration(node, classDeclarationRegex) || skipClassDeclaration) {
return;
}
if (!astUtil.isValidDeclaratorValue(node)) {
return;
}
parseForShorthandCandidates(node, node.init);
};

const scriptVisitor = {
JSXAttribute: attributeVisitor,
TextAttribute: attributeVisitor,
CallExpression: callExpressionVisitor,
VariableDeclarator: variableDeclaratorVistor,
TaggedTemplateExpression: function (node) {
if (!tags.includes(node.tag.name)) {
return;
Expand All @@ -479,12 +491,12 @@ module.exports = {
case astUtil.isVLiteralValue(node):
parseForShorthandCandidates(node);
break;
case astUtil.isArrayExpression(node):
case astUtil.isVArrayExpression(node):
node.value.expression.elements.forEach((arg) => {
parseForShorthandCandidates(node, arg);
});
break;
case astUtil.isObjectExpression(node):
case astUtil.isVObjectExpression(node):
node.value.expression.properties.forEach((prop) => {
parseForShorthandCandidates(node, prop);
});
Expand Down
Loading