eslint-plugin-grules
Advanced tools
Comparing version 0.0.10 to 0.0.11
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"type": "commonjs", | ||
@@ -8,0 +8,0 @@ "main": "./src/index.js", |
module.exports = { | ||
meta: { | ||
fixable: "code", | ||
fixable: "code", // Or "whitespace" if it's just about spaces, tabs, etc. | ||
}, | ||
create: (context) => { | ||
create: function (context) { | ||
return { | ||
CallExpression: (node) => { | ||
CallExpression(node) { | ||
if ( | ||
node.callee.type === "MemberExpression" && | ||
node.callee.property.name === "charAt" && | ||
node.callee.object.type === "Identifier" && | ||
node.arguments.length === 1 | ||
node.callee.property.name === "charAt" | ||
) { | ||
const arg = node.arguments[0]; | ||
const objectText = context | ||
.getSourceCode() | ||
.getText(node.callee.object); | ||
const argument = node.arguments[0]; | ||
let replacement; | ||
if (arg.type === "Literal" && typeof arg.value === "number") { | ||
// Simple number index | ||
replacement = `[${arg.value}]`; | ||
if (argument && argument.type === "Literal") { | ||
replacement = `${objectText}[${argument.raw}]`; | ||
} else { | ||
// Complex expression or non-numeric literal | ||
replacement = `[${context.getSourceCode().getText(arg)}]`; | ||
// For non-literal arguments, use the expression as is | ||
replacement = `${objectText}[${context | ||
.getSourceCode() | ||
.getText(argument)}]`; | ||
} | ||
@@ -27,8 +29,5 @@ | ||
node, | ||
message: `Use index-based access ${replacement} instead of '.charAt()'`, | ||
fix: (fixer) => { | ||
return fixer.replaceText( | ||
node, | ||
`${node.callee.object.name}${replacement}` | ||
); | ||
message: "Use bracket notation instead of .charAt()", | ||
fix(fixer) { | ||
return fixer.replaceText(node, replacement); | ||
}, | ||
@@ -35,0 +34,0 @@ }); |
@@ -5,47 +5,54 @@ module.exports = { | ||
}, | ||
create: (context) => { | ||
create: function (context) { | ||
return { | ||
CallExpression: (node) => { | ||
CallExpression(node) { | ||
if ( | ||
node.callee.type === "MemberExpression" && | ||
node.callee.property.name === "at" && | ||
node.callee.object.type === "Identifier" && | ||
node.arguments.length === 1 | ||
(node.callee.object.type === "Identifier" || | ||
node.callee.object.type === "MemberExpression") | ||
) { | ||
const arg = node.arguments[0]; | ||
const isUnaryNumber = | ||
arg.type === "UnaryExpression" && | ||
(arg.operator === "+" || arg.operator === "-") && | ||
arg.argument.type === "Literal" && | ||
typeof arg.argument.value === "number"; | ||
const objectText = context | ||
.getSourceCode() | ||
.getText(node.callee.object), | ||
argument = node.arguments[0]; | ||
let value; | ||
let message; | ||
let fix = null; | ||
if ( | ||
(arg.type === "Literal" && typeof arg.value === "number") || | ||
isUnaryNumber | ||
argument.type === "UnaryExpression" && | ||
typeof argument.argument.value === "number" | ||
) { | ||
const index = isUnaryNumber | ||
? arg.operator === "+" | ||
? +arg.argument.value | ||
: -arg.argument.value | ||
: arg.value; | ||
const replacement = | ||
index >= 0 | ||
? `[${index}]` | ||
: `[${node.callee.object.name}.length - ${Math.abs(index)}]`; | ||
value = | ||
argument.operator === "-" | ||
? -argument.argument.value | ||
: +argument.argument.value; | ||
} else if ( | ||
argument.type === "Literal" && | ||
typeof argument.value === "number" | ||
) { | ||
value = argument.value; | ||
} | ||
message = `Use index-based access ${replacement} instead of '.at(${index})'`; | ||
fix = (fixer) => { | ||
return fixer.replaceText( | ||
node, | ||
`${node.callee.object.name}${replacement}` | ||
); | ||
}; | ||
let replacement; | ||
if (value === undefined) { | ||
replacement = `${objectText}[${context | ||
.getSourceCode() | ||
.getText(argument)}]`; | ||
} else { | ||
message = "Use index-based access instead of '.at()'"; | ||
if (value >= 0) { | ||
replacement = `${objectText}[${value}]`; | ||
} else { | ||
replacement = `${objectText}[${objectText}.length - ${Math.abs( | ||
value | ||
)}]`; | ||
} | ||
} | ||
context.report({ node, message, fix }); | ||
context.report({ | ||
node, | ||
message: "Use array indexing instead of .at()", | ||
fix(fixer) { | ||
return fixer.replaceText(node, replacement); | ||
}, | ||
}); | ||
} | ||
@@ -52,0 +59,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31645
829