Skip to content

Commit

Permalink
Chore: disallow .substr and .substring in favor of .slice (#9010)
Browse files Browse the repository at this point in the history
This updates eslint-config-eslint to disallow String#substr and String#substring in favor of String#slice.
  • Loading branch information
not-an-aardvark committed Jul 26, 2017
1 parent d0536d6 commit acbe86a
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function validateJsonFile(filePath) {
*/
function fileType(extension) {
return function(filename) {
return filename.substring(filename.lastIndexOf(".") + 1) === extension;
return filename.slice(filename.lastIndexOf(".") + 1) === extension;
};
}

Expand Down
4 changes: 2 additions & 2 deletions lib/config/config-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,8 @@ function resolve(filePath, relativeTo) {

if (filePath.startsWith("plugin:")) {
const configFullName = filePath;
const pluginName = filePath.substr(7, filePath.lastIndexOf("/") - 7);
const configName = filePath.substr(filePath.lastIndexOf("/") + 1, filePath.length - filePath.lastIndexOf("/") - 1);
const pluginName = filePath.slice(7, filePath.lastIndexOf("/"));
const configName = filePath.slice(filePath.lastIndexOf("/") + 1);

normalizedPackageName = normalizePackageName(pluginName, "eslint-plugin");
debug(`Attempting to resolve ${normalizedPackageName}`);
Expand Down
2 changes: 1 addition & 1 deletion lib/config/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Plugins {
* @returns {string} The name of the plugin without prefix.
*/
static removePrefix(pluginName) {
return pluginName.startsWith(PLUGIN_NAME_PREFIX) ? pluginName.substring(PLUGIN_NAME_PREFIX.length) : pluginName;
return pluginName.startsWith(PLUGIN_NAME_PREFIX) ? pluginName.slice(PLUGIN_NAME_PREFIX.length) : pluginName;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ function parseBooleanConfig(string, comment) {
let value;

if (pos !== -1) {
value = name.substring(pos + 1, name.length);
name = name.substring(0, pos);
value = name.slice(pos + 1);
name = name.slice(0, pos);
}

items[name] = {
Expand Down Expand Up @@ -338,7 +338,7 @@ function modifyConfigsFromComments(filename, ast, config, linterContext) {
const match = /^(eslint(-\w+){0,3}|exported|globals?)(\s|$)/.exec(value);

if (match) {
value = value.substring(match.index + match[1].length);
value = value.slice(match.index + match[1].length);

if (comment.type === "Block") {
switch (match[1]) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-multi-spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ module.exports = {
function formatReportedCommentValue(token) {
const valueLines = token.value.split("\n");
const value = valueLines[0];
const formattedValue = `${value.substring(0, 12)}...`;
const formattedValue = `${value.slice(0, 12)}...`;

return valueLines.length === 1 && value.length <= 12 ? value : formattedValue;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/space-infix-ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ module.exports = {
const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode);

if (nonSpacedNode) {
if (!(int32Hint && sourceCode.getText(node).substr(-2) === "|0")) {
if (!(int32Hint && sourceCode.getText(node).endsWith("|0"))) {
report(node, nonSpacedNode);
}
}
Expand Down
5 changes: 5 additions & 0 deletions packages/eslint-config-eslint/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ rules:
no-process-exit: "error"
no-proto: "error"
no-redeclare: "error"
no-restricted-properties: [
"error",
{ property: "substring", message: "Use .slice instead of .substring." },
{ property: "substr", message: "Use .slice instead of .substr." }
]
no-return-assign: "error"
no-script-url: "error"
no-self-assign: "error"
Expand Down

0 comments on commit acbe86a

Please sign in to comment.