github-pages-deploy-action/node_modules/eslint/lib/rules/no-useless-computed-key.js

104 lines
3.8 KiB
JavaScript
Raw Permalink Normal View History

2020-03-07 11:45:40 +08:00
/**
* @fileoverview Rule to disallow unnecessary computed property keys in object literals
* @author Burak Yigit Kaya
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
2020-03-31 20:40:00 +08:00
const lodash = require("lodash");
const astUtils = require("./utils/ast-utils");
2020-03-07 11:45:40 +08:00
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: "suggestion",
docs: {
2020-03-31 20:40:00 +08:00
description: "disallow unnecessary computed property keys in objects and classes",
2020-03-07 11:45:40 +08:00
category: "ECMAScript 6",
recommended: false,
url: "https://eslint.org/docs/rules/no-useless-computed-key"
},
2020-03-31 20:40:00 +08:00
schema: [{
type: "object",
properties: {
enforceForClassMembers: {
type: "boolean",
default: false
}
},
additionalProperties: false
}],
2020-05-15 05:33:08 +08:00
fixable: "code",
messages: {
unnecessarilyComputedProperty: "Unnecessarily computed property [{{property}}] found."
}
2020-03-07 11:45:40 +08:00
},
create(context) {
const sourceCode = context.getSourceCode();
2020-03-31 20:40:00 +08:00
const enforceForClassMembers = context.options[0] && context.options[0].enforceForClassMembers;
/**
* Reports a given node if it violated this rule.
* @param {ASTNode} node The node to check.
* @returns {void}
*/
function check(node) {
if (!node.computed) {
return;
}
2020-03-07 11:45:40 +08:00
2020-03-31 20:40:00 +08:00
const key = node.key,
nodeType = typeof key.value;
2020-03-07 11:45:40 +08:00
2020-03-31 20:40:00 +08:00
let allowedKey;
2020-03-07 11:45:40 +08:00
2020-03-31 20:40:00 +08:00
if (node.type === "MethodDefinition") {
allowedKey = node.static ? "prototype" : "constructor";
} else {
allowedKey = "__proto__";
}
2020-03-07 11:45:40 +08:00
2020-03-31 20:40:00 +08:00
if (key.type === "Literal" && (nodeType === "string" || nodeType === "number") && key.value !== allowedKey) {
context.report({
node,
2020-05-15 05:33:08 +08:00
messageId: "unnecessarilyComputedProperty",
2020-03-31 20:40:00 +08:00
data: { property: sourceCode.getText(key) },
fix(fixer) {
2020-05-15 05:33:08 +08:00
const leftSquareBracket = sourceCode.getTokenBefore(key, astUtils.isOpeningBracketToken);
const rightSquareBracket = sourceCode.getTokenAfter(key, astUtils.isClosingBracketToken);
2020-03-31 20:40:00 +08:00
2020-05-15 05:33:08 +08:00
// If there are comments between the brackets and the property name, don't do a fix.
if (sourceCode.commentsExistBetween(leftSquareBracket, rightSquareBracket)) {
2020-03-31 20:40:00 +08:00
return null;
}
2020-03-07 11:45:40 +08:00
2020-03-31 20:40:00 +08:00
const tokenBeforeLeftBracket = sourceCode.getTokenBefore(leftSquareBracket);
2020-03-07 11:45:40 +08:00
2020-03-31 20:40:00 +08:00
// Insert a space before the key to avoid changing identifiers, e.g. ({ get[2]() {} }) to ({ get2() {} })
const needsSpaceBeforeKey = tokenBeforeLeftBracket.range[1] === leftSquareBracket.range[0] &&
!astUtils.canTokensBeAdjacent(tokenBeforeLeftBracket, sourceCode.getFirstToken(key));
2020-03-07 11:45:40 +08:00
2020-03-31 20:40:00 +08:00
const replacementKey = (needsSpaceBeforeKey ? " " : "") + key.raw;
2020-03-07 11:45:40 +08:00
2020-03-31 20:40:00 +08:00
return fixer.replaceTextRange([leftSquareBracket.range[0], rightSquareBracket.range[1]], replacementKey);
}
});
2020-03-07 11:45:40 +08:00
}
2020-03-31 20:40:00 +08:00
}
return {
Property: check,
MethodDefinition: enforceForClassMembers ? check : lodash.noop
2020-03-07 11:45:40 +08:00
};
}
};