From c0517441b2348b7936b071952df70dcb5813c6a0 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 23 Jan 2020 20:15:28 +0800 Subject: [PATCH] Enable `unicorn/prefer-string-slice` (#7251) --- .eslintrc.yml | 1 + .../babel-plugins/transform-custom-require.js | 2 +- src/language-css/parser-postcss.js | 6 +++--- src/language-js/comments.js | 2 +- src/language-js/needs-parens.js | 2 +- src/language-js/printer-estree.js | 16 ++++++++++------ src/language-js/utils.js | 4 +++- src/language-markdown/embed.js | 2 +- src/main/core.js | 2 +- website/static/landing.js | 2 +- 10 files changed, 23 insertions(+), 16 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index 54a40050f..7fb4a0000 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -58,6 +58,7 @@ rules: - exceptRange: true unicorn/new-for-builtins: error unicorn/prefer-includes: error + unicorn/prefer-string-slice: error overrides: - files: - scripts/**/*.js diff --git a/scripts/build/babel-plugins/transform-custom-require.js b/scripts/build/babel-plugins/transform-custom-require.js index d1084c862..ae2273880 100644 --- a/scripts/build/babel-plugins/transform-custom-require.js +++ b/scripts/build/babel-plugins/transform-custom-require.js @@ -22,7 +22,7 @@ module.exports = function(babel) { if (isEvalRequire(node.callee) && node.arguments.length === 1) { let arg = node.arguments[0]; if (t.isLiteral(arg) && arg.value.startsWith(".")) { - const value = "." + arg.value.substring(arg.value.lastIndexOf("/")); + const value = "." + arg.value.slice(arg.value.lastIndexOf("/")); arg = t.stringLiteral(value); } path.replaceWith(t.callExpression(t.identifier("require"), [arg])); diff --git a/src/language-css/parser-postcss.js b/src/language-css/parser-postcss.js index a524aa7f9..5b39019f4 100644 --- a/src/language-css/parser-postcss.js +++ b/src/language-css/parser-postcss.js @@ -356,7 +356,7 @@ function parseNestedCSS(node, options) { const defaultSCSSDirectiveIndex = value.match(DEFAULT_SCSS_DIRECTIVE); if (defaultSCSSDirectiveIndex) { - value = value.substring(0, defaultSCSSDirectiveIndex.index); + value = value.slice(0, defaultSCSSDirectiveIndex.index); node.scssDefault = true; if (defaultSCSSDirectiveIndex[0].trim() !== "!default") { @@ -367,7 +367,7 @@ function parseNestedCSS(node, options) { const globalSCSSDirectiveIndex = value.match(GLOBAL_SCSS_DIRECTIVE); if (globalSCSSDirectiveIndex) { - value = value.substring(0, globalSCSSDirectiveIndex.index); + value = value.slice(0, globalSCSSDirectiveIndex.index); node.scssGlobal = true; if (globalSCSSDirectiveIndex[0].trim() !== "!global") { @@ -454,7 +454,7 @@ function parseNestedCSS(node, options) { const customSelector = params.match(/:--\S+?\s+/)[0].trim(); node.customSelector = customSelector; - node.selector = parseSelector(params.substring(customSelector.length)); + node.selector = parseSelector(params.slice(customSelector.length)); delete node.params; return node; diff --git a/src/language-js/comments.js b/src/language-js/comments.js index 6316fa2f6..c14c45f39 100644 --- a/src/language-js/comments.js +++ b/src/language-js/comments.js @@ -534,7 +534,7 @@ function handleCommentAfterArrowParams(text, enclosingNode, comment, options) { } const index = getNextNonSpaceNonCommentCharacterIndex(text, comment, options); - if (text.substr(index, 2) === "=>") { + if (text.slice(index, index + 2) === "=>") { addDanglingComment(enclosingNode, comment); return true; } diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index 65d00b612..8818c7483 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -525,7 +525,7 @@ function needsParens(path, options) { // See corresponding workaround in printer.js case: "Literal" ((options.parser !== "typescript" && !parent.directive) || (options.parser === "typescript" && - options.originalText.substr(options.locStart(node) - 1, 1) === "(")) + options.originalText.charAt(options.locStart(node) - 1) === "(")) ) { // To avoid becoming a directive const grandParent = path.getParentNode(1); diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index b30faa9fa..91af25154 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -835,7 +835,10 @@ function printPathNoParens(path, options, print, args) { comment, options ); - return options.originalText.substr(nextCharacter, 2) === "=>"; + return ( + options.originalText.slice(nextCharacter, nextCharacter + 2) === + "=>" + ); } ); if (dangling) { @@ -1191,7 +1194,7 @@ function printPathNoParens(path, options, print, args) { path.call(print, "callee"), optional, isIdentifierWithFlowAnnotation - ? `/*:: ${n.callee.trailingComments[0].value.substring(2).trim()} */` + ? `/*:: ${n.callee.trailingComments[0].value.slice(2).trim()} */` : "", printFunctionTypeParameters(path, options, print), printArgumentsList(path, options, print) @@ -3034,7 +3037,7 @@ function printPathNoParens(path, options, print, args) { value.typeAnnotation && value.typeAnnotation.range && options.originalText - .substring(value.typeAnnotation.range[0]) + .slice(value.typeAnnotation.range[0]) .match(/^\/\*\s*:/); return concat([ "(", @@ -3051,13 +3054,13 @@ function printPathNoParens(path, options, print, args) { case "TypeParameterInstantiation": { const value = path.getValue(); const commentStart = value.range - ? options.originalText.substring(0, value.range[0]).lastIndexOf("/*") + ? options.originalText.slice(0, value.range[0]).lastIndexOf("/*") : -1; // As noted in the TypeCastExpression comments above, we're able to use a normal whitespace regex here // because we know for sure that this is a type definition. const commentSyntax = commentStart >= 0 && - options.originalText.substring(commentStart).match(/^\/\*\s*::/); + options.originalText.slice(commentStart).match(/^\/\*\s*::/); if (commentSyntax) { return concat([ "/*:: ", @@ -6054,8 +6057,9 @@ function printComment(commentPath, options) { return printed; } + const commentEnd = options.locEnd(comment); const isInsideFlowComment = - options.originalText.substr(options.locEnd(comment) - 3, 3) === "*-/"; + options.originalText.slice(commentEnd - 3, commentEnd) === "*-/"; return "/*" + comment.value + (isInsideFlowComment ? "*-/" : "*/"); } diff --git a/src/language-js/utils.js b/src/language-js/utils.js index 48431a0d1..f589bd2d7 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -645,7 +645,9 @@ function isLastStatement(path) { function isFlowAnnotationComment(text, typeAnnotation, options) { const start = options.locStart(typeAnnotation); const end = skipWhitespace(text, options.locEnd(typeAnnotation)); - return text.substr(start, 2) === "/*" && text.substr(end, 2) === "*/"; + return ( + text.slice(start, start + 2) === "/*" && text.slice(end, end + 2) === "*/" + ); } function hasLeadingOwnLineComment(text, node, options) { diff --git a/src/language-markdown/embed.js b/src/language-markdown/embed.js index 84f21af40..f6e4fc0c1 100644 --- a/src/language-markdown/embed.js +++ b/src/language-markdown/embed.js @@ -74,7 +74,7 @@ function embed(path, print, textToDoc, options) { language.name.toLowerCase() === lang || (language.aliases && language.aliases.includes(lang)) || (language.extensions && - language.extensions.find(ext => ext.substring(1) === lang)) + language.extensions.find(ext => ext === `.${lang}`)) ); if (language) { return language.parsers[0]; diff --git a/src/main/core.js b/src/main/core.js index fa409dfab..68d5f4578 100644 --- a/src/main/core.js +++ b/src/main/core.js @@ -321,7 +321,7 @@ function format(text, opts) { const hasUnicodeBOM = text.charCodeAt(0) === UTF8BOM; if (hasUnicodeBOM) { - text = text.substring(1); + text = text.slice(1); if (hasCursor) { opts.cursorOffset++; } diff --git a/website/static/landing.js b/website/static/landing.js index a56bcc39a..5e9fc8847 100644 --- a/website/static/landing.js +++ b/website/static/landing.js @@ -2,7 +2,7 @@ /* eslint-env browser */ -if (location.hash.substring(1).startsWith(encodeURIComponent("{"))) { +if (location.hash.slice(1).startsWith(encodeURIComponent("{"))) { location.pathname = "/playground/"; }