// Requirements
//------------------------------------------------------------------------------
-const lodash = require("lodash");
const astUtils = require("./utils/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
-const MESSAGE_UNNECESSARY_COMPUTED = "Unnecessarily computed property [{{property}}] found.";
-
module.exports = {
meta: {
type: "suggestion",
},
additionalProperties: false
}],
- fixable: "code"
+ fixable: "code",
+
+ messages: {
+ unnecessarilyComputedProperty: "Unnecessarily computed property [{{property}}] found."
+ }
},
create(context) {
const sourceCode = context.getSourceCode();
if (key.type === "Literal" && (nodeType === "string" || nodeType === "number") && key.value !== allowedKey) {
context.report({
node,
- message: MESSAGE_UNNECESSARY_COMPUTED,
+ messageId: "unnecessarilyComputedProperty",
data: { property: sourceCode.getText(key) },
fix(fixer) {
- const leftSquareBracket = sourceCode.getFirstToken(node, astUtils.isOpeningBracketToken);
- const rightSquareBracket = sourceCode.getFirstTokenBetween(node.key, node.value, astUtils.isClosingBracketToken);
- const tokensBetween = sourceCode.getTokensBetween(leftSquareBracket, rightSquareBracket, 1);
-
- if (tokensBetween.slice(0, -1).some((token, index) =>
- sourceCode.getText().slice(token.range[1], tokensBetween[index + 1].range[0]).trim())) {
+ const leftSquareBracket = sourceCode.getTokenBefore(key, astUtils.isOpeningBracketToken);
+ const rightSquareBracket = sourceCode.getTokenAfter(key, astUtils.isClosingBracketToken);
- // If there are comments between the brackets and the property name, don't do a fix.
+ // If there are comments between the brackets and the property name, don't do a fix.
+ if (sourceCode.commentsExistBetween(leftSquareBracket, rightSquareBracket)) {
return null;
}
}
}
+ /**
+ * A no-op function to act as placeholder for checking a node when the `enforceForClassMembers` option is `false`.
+ * @returns {void}
+ * @private
+ */
+ function noop() {}
+
return {
Property: check,
- MethodDefinition: enforceForClassMembers ? check : lodash.noop
+ MethodDefinition: enforceForClassMembers ? check : noop
};
}
};