From f0207e68505e29419adb5d4a52279bb95cf98835 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 10 Apr 2020 21:20:40 +0800 Subject: [PATCH] Enable `unicorn/better-regex` (#7991) * Enable `unicorn/better-regex` * Spellcheck ignore regex --- .editorconfig | 1 + .eslintrc.yml | 1 + cspell.json | 3 ++- scripts/draft-blog-post.js | 4 ++-- scripts/lint-changelog.js | 4 ++-- scripts/release/steps/update-version.js | 2 +- src/common/util.js | 8 ++++---- src/document/doc-printer.js | 4 ++-- src/language-css/clean.js | 6 +++--- src/language-css/parser-postcss.js | 4 ++-- src/language-css/printer-postcss.js | 8 ++++---- src/language-css/utils.js | 4 ++-- src/language-graphql/pragma.js | 2 +- src/language-handlebars/printer-glimmer.js | 4 ++-- src/language-html/conditional-comment.js | 6 +++--- src/language-html/constants.evaluate.js | 2 +- src/language-html/parser-html.js | 4 ++-- src/language-html/preprocess.js | 4 ++-- src/language-html/printer-html.js | 4 ++-- src/language-html/syntax-vue.js | 6 +++--- src/language-js/comments.js | 2 +- src/language-js/embed.js | 8 ++++---- src/language-js/printer-estree.js | 4 ++-- src/language-js/utils.js | 2 +- src/language-markdown/embed.js | 2 +- src/language-markdown/parser-markdown.js | 2 +- src/language-markdown/printer-markdown.js | 8 ++++---- src/language-markdown/utils.js | 2 +- src/language-yaml/pragma.js | 2 +- src/language-yaml/utils.js | 2 +- tests_integration/__tests__/patterns-dirs.js | 2 +- 31 files changed, 60 insertions(+), 57 deletions(-) diff --git a/.editorconfig b/.editorconfig index 49a6d74cdd74..1cbc8fe300ad 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,6 +10,7 @@ insert_final_newline = true [cspell.json] indent_size = 4 +insert_final_newline = false [website/blog/*.md] trim_trailing_whitespace = false diff --git a/.eslintrc.yml b/.eslintrc.yml index 28d4f88f0a9a..8b89ff848440 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -56,6 +56,7 @@ rules: - error - never - exceptRange: true + unicorn/better-regex: error unicorn/new-for-builtins: error unicorn/prefer-includes: error unicorn/prefer-set-has: error diff --git a/cspell.json b/cspell.json index b223deba330a..399366bf9800 100644 --- a/cspell.json +++ b/cspell.json @@ -431,7 +431,8 @@ "\\[@\\w+?\\]", "\\[`\\w+`\\]", "ve{2,}r{2,}y", - "ve+r+y+long\\w*" + "ve+r+y+long\\w*", + "\\/.*?\\/" ], "ignorePaths": [ "**/node_modules/**", diff --git a/scripts/draft-blog-post.js b/scripts/draft-blog-post.js index bce3c86d9d01..28adf7e20e7d 100644 --- a/scripts/draft-blog-post.js +++ b/scripts/draft-blog-post.js @@ -67,9 +67,9 @@ for (const dir of dirs) { highlight: title.includes("[HIGHLIGHT]"), content: [ title - .replace(/\[(BREAKING|HIGHLIGHT)\]/g, "") + .replace(/\[(BREAKING|HIGHLIGHT)]/g, "") .replace(/\s+/g, " ") - .replace(/^#### [a-z]/, (s) => s.toUpperCase()), + .replace(/^#{4} [a-z]/, (s) => s.toUpperCase()), ...rest, ].join("\n"), }; diff --git a/scripts/lint-changelog.js b/scripts/lint-changelog.js index 26ff1c20a0b8..c69f92e2fada 100644 --- a/scripts/lint-changelog.js +++ b/scripts/lint-changelog.js @@ -52,14 +52,14 @@ for (const file of [ } } -const authorRegex = /by \[@(.*?)\]\(https:\/\/github\.com\/\1\)/; +const authorRegex = /by \[@(.*?)]\(https:\/\/github\.com\/\1\)/; const titleRegex = /^#{4} (.*?)\(\[#\d{4,}]/; const template = fs.readFileSync( path.join(CHANGELOG_ROOT, TEMPLATE_FILE), "utf8" ); -const [templateComment] = template.match(//); +const [templateComment] = template.match(//); const [templateAuthorLink] = template.match(authorRegex); for (const category of CHANGELOG_CATEGORIES) { diff --git a/scripts/release/steps/update-version.js b/scripts/release/steps/update-version.js index 6f01681685de..08182076560c 100644 --- a/scripts/release/steps/update-version.js +++ b/scripts/release/steps/update-version.js @@ -21,7 +21,7 @@ async function bump({ version }) { // Update unpkg link in docs processFile("docs/browser.md", (content) => - content.replace(/(\/\/unpkg\.com\/prettier@)(?:.*?)\//g, `$1${version}/`) + content.replace(/(\/\/unpkg\.com\/prettier@).*?\//g, `$1${version}/`) ); await execa("yarn", ["update-stable-docs"], { diff --git a/src/common/util.js b/src/common/util.js index 51ab3a1dce40..0de284663e21 100644 --- a/src/common/util.js +++ b/src/common/util.js @@ -73,7 +73,7 @@ const skipToLineEnd = skip(",; \t"); /** * @type {(text: string, index: number | false, opts?: SkipOptions) => number | false} */ -const skipEverythingButNewLine = skip(/[^\r\n]/); +const skipEverythingButNewLine = skip(/[^\n\r]/); /** * @param {string} text @@ -520,7 +520,7 @@ function getIndentSize(value, tabWidth) { return getAlignmentSize( // All the leading whitespaces - value.slice(lastNewlineIndex + 1).match(/^[ \t]*/)[0], + value.slice(lastNewlineIndex + 1).match(/^[\t ]*/)[0], tabWidth ); } @@ -624,7 +624,7 @@ function makeString(rawContent, enclosingQuote, unescapeUnnecessaryEscapes) { const otherQuote = enclosingQuote === '"' ? "'" : '"'; // Matches _any_ escape and unescaped quotes (both single and double). - const regex = /\\([\s\S])|(['"])/g; + const regex = /\\([\S\s])|(["'])/g; // Escape and unescape single and double quotes as needed to be able to // enclose `rawContent` with `enclosingQuote`. @@ -650,7 +650,7 @@ function makeString(rawContent, enclosingQuote, unescapeUnnecessaryEscapes) { // Unescape any unnecessarily escaped character. // Adapted from https://github.com/eslint/eslint/blob/de0b4ad7bd820ade41b1f606008bea68683dc11a/lib/rules/no-useless-escape.js#L27 return unescapeUnnecessaryEscapes && - /^[^\\nrvtbfux\r\n\u2028\u2029"'0-7]$/.test(escaped) + /^[^\n\r"'0-7\\bfnrt-vx\u2028\u2029]$/.test(escaped) ? escaped : "\\" + escaped; }); diff --git a/src/document/doc-printer.js b/src/document/doc-printer.js index 737532fb3af5..4f27b04443a5 100644 --- a/src/document/doc-printer.js +++ b/src/document/doc-printer.js @@ -121,13 +121,13 @@ function trim(out) { while ( out.length > 0 && typeof out[out.length - 1] === "string" && - out[out.length - 1].match(/^[ \t]*$/) + out[out.length - 1].match(/^[\t ]*$/) ) { trimCount += out.pop().length; } if (out.length && typeof out[out.length - 1] === "string") { - const trimmed = out[out.length - 1].replace(/[ \t]*$/, ""); + const trimmed = out[out.length - 1].replace(/[\t ]*$/, ""); trimCount += out[out.length - 1].length - trimmed.length; out[out.length - 1] = trimmed; } diff --git a/src/language-css/clean.js b/src/language-css/clean.js index 0bc1016c4116..452834d48c2d 100644 --- a/src/language-css/clean.js +++ b/src/language-css/clean.js @@ -113,7 +113,7 @@ function clean(ast, newObj, parent) { } if (newObj.value) { - newObj.value = newObj.value.trim().replace(/^['"]|['"]$/g, ""); + newObj.value = newObj.value.trim().replace(/^["']|["']$/g, ""); delete newObj.quoted; } } @@ -129,7 +129,7 @@ function clean(ast, newObj, parent) { newObj.value ) { newObj.value = newObj.value.replace( - /([\d.eE+-]+)([a-zA-Z]*)/g, + /([\d+.Ee-]+)([A-Za-z]*)/g, (match, numStr, unit) => { const num = Number(numStr); return isNaN(num) ? match : num + unit.toLowerCase(); @@ -157,7 +157,7 @@ function clean(ast, newObj, parent) { } function cleanCSSStrings(value) { - return value.replace(/'/g, '"').replace(/\\([^a-fA-F\d])/g, "$1"); + return value.replace(/'/g, '"').replace(/\\([^\dA-Fa-f])/g, "$1"); } module.exports = clean; diff --git a/src/language-css/parser-postcss.js b/src/language-css/parser-postcss.js index cd3fb3c65bf4..e3603a60d925 100644 --- a/src/language-css/parser-postcss.js +++ b/src/language-css/parser-postcss.js @@ -464,7 +464,7 @@ function parseNestedCSS(node, options) { } if (name === "at-root") { - if (/^\(\s*(without|with)\s*:[\s\S]+\)$/.test(params)) { + if (/^\(\s*(without|with)\s*:[\S\s]+\)$/.test(params)) { node.params = parseValue(params); } else { node.selector = parseSelector(params); @@ -500,7 +500,7 @@ function parseNestedCSS(node, options) { ].includes(name) ) { // Remove unnecessary spaces in SCSS variable arguments - params = params.replace(/(\$\S+?)\s+?\.\.\./, "$1..."); + params = params.replace(/(\$\S+?)\s+?\.{3}/, "$1..."); // Remove unnecessary spaces before SCSS control, mixin and function directives params = params.replace(/^(?!if)(\S+)\s+\(/, "$1("); diff --git a/src/language-css/printer-postcss.js b/src/language-css/printer-postcss.js index ed7cc9627f02..1fe079f77e36 100644 --- a/src/language-css/printer-postcss.js +++ b/src/language-css/printer-postcss.js @@ -974,10 +974,10 @@ function printNodeSequence(path, options, print) { return concat(parts); } -const STRING_REGEX = /(['"])(?:(?!\1)[^\\]|\\[\s\S])*\1/g; -const NUMBER_REGEX = /(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g; -const STANDARD_UNIT_REGEX = /[a-zA-Z]+/g; -const WORD_PART_REGEX = /[$@]?[a-zA-Z_\u0080-\uFFFF][\w\-\u0080-\uFFFF]*/g; +const STRING_REGEX = /(["'])(?:(?!\1)[^\\]|\\[\S\s])*\1/g; +const NUMBER_REGEX = /(?:\d*\.\d+|\d+\.?)(?:[Ee][+-]?\d+)?/g; +const STANDARD_UNIT_REGEX = /[A-Za-z]+/g; +const WORD_PART_REGEX = /[$@]?[A-Z_a-z\u0080-\uFFFF][\w\u0080-\uFFFF-]*/g; const ADJUST_NUMBERS_REGEX = new RegExp( STRING_REGEX.source + "|" + diff --git a/src/language-css/utils.js b/src/language-css/utils.js index 1328dd256356..f0bd1cc389ec 100644 --- a/src/language-css/utils.js +++ b/src/language-css/utils.js @@ -60,7 +60,7 @@ function getPropOfDeclNode(path) { function isSCSS(parser, text) { const hasExplicitParserChoice = parser === "less" || parser === "scss"; - const IS_POSSIBLY_SCSS = /(\w\s*:\s*[^}:]+|#){|@import[^\n]+(?:url|,)/; + const IS_POSSIBLY_SCSS = /(\w\s*:\s*[^:}]+|#){|@import[^\n]+(?:url|,)/; return hasExplicitParserChoice ? parser === "scss" : IS_POSSIBLY_SCSS.test(text); @@ -385,7 +385,7 @@ function isLessParser(options) { } function lastLineHasInlineComment(text) { - return /\/\//.test(text.split(/[\r\n]/).pop()); + return /\/\//.test(text.split(/[\n\r]/).pop()); } module.exports = { diff --git a/src/language-graphql/pragma.js b/src/language-graphql/pragma.js index d41219dc7a0a..af174a325f9e 100644 --- a/src/language-graphql/pragma.js +++ b/src/language-graphql/pragma.js @@ -1,7 +1,7 @@ "use strict"; function hasPragma(text) { - return /^\s*#[^\n\S]*@(format|prettier)\s*(\n|$)/.test(text); + return /^\s*#[^\S\n]*@(format|prettier)\s*(\n|$)/.test(text); } function insertPragma(text) { diff --git a/src/language-handlebars/printer-glimmer.js b/src/language-handlebars/printer-glimmer.js index 6327e43d7007..eca75b26fd53 100644 --- a/src/language-handlebars/printer-glimmer.js +++ b/src/language-handlebars/printer-glimmer.js @@ -493,14 +493,14 @@ function countNewLines(string) { function countLeadingNewLines(string) { /* istanbul ignore next */ string = typeof string === "string" ? string : ""; - const newLines = (string.match(/^([^\S\r\n]*[\r\n])+/g) || [])[0] || ""; + const newLines = (string.match(/^([^\S\n\r]*[\n\r])+/g) || [])[0] || ""; return countNewLines(newLines); } function countTrailingNewLines(string) { /* istanbul ignore next */ string = typeof string === "string" ? string : ""; - const newLines = (string.match(/([\r\n][^\S\r\n]*)+$/g) || [])[0] || ""; + const newLines = (string.match(/([\n\r][^\S\n\r]*)+$/g) || [])[0] || ""; return countNewLines(newLines); } diff --git a/src/language-html/conditional-comment.js b/src/language-html/conditional-comment.js index 55199c87b977..267b86eac306 100644 --- a/src/language-html/conditional-comment.js +++ b/src/language-html/conditional-comment.js @@ -3,11 +3,11 @@ // https://css-tricks.com/how-to-create-an-ie-only-stylesheet // -const IE_CONDITIONAL_START_END_COMMENT_REGEX = /^(\[if([^\]]*?)\]>)([\s\S]*?))([\S\s]*?) -const IE_CONDITIONAL_START_COMMENT_REGEX = /^\[if([^\]]*?)\]> -const IE_CONDITIONAL_END_COMMENT_REGEX = /^ htmlStyle.selectorText .split(",") .map((selector) => selector.trim()) - .filter((selector) => /^[a-zA-Z0-9]+$/.test(selector)) + .filter((selector) => /^[\dA-Za-z]+$/.test(selector)) .map((tagName) => [tagName, htmlStyle.style[property]]) ) ) diff --git a/src/language-html/parser-html.js b/src/language-html/parser-html.js index ad9207cf385d..ce88a5010118 100644 --- a/src/language-html/parser-html.js +++ b/src/language-html/parser-html.js @@ -93,7 +93,7 @@ function ngHtmlParser( attr.value = null; } else { attr.value = attr.valueSpan.toString(); - if (/['"]/.test(attr.value[0])) { + if (/["']/.test(attr.value[0])) { attr.value = attr.value.slice(1, -1); } } @@ -204,7 +204,7 @@ function _parse(text, options, parserOptions, shouldParseFrontMatter = true) { const parseSubHtml = (subContent, startSpan) => { const { offset } = startSpan; - const fakeContent = text.slice(0, offset).replace(/[^\r\n]/g, " "); + const fakeContent = text.slice(0, offset).replace(/[^\n\r]/g, " "); const realContent = subContent; const subAst = _parse( fakeContent + realContent, diff --git a/src/language-html/preprocess.js b/src/language-html/preprocess.js index 537ebd751765..956b82d43f9e 100644 --- a/src/language-html/preprocess.js +++ b/src/language-html/preprocess.js @@ -232,7 +232,7 @@ function extractInterpolation(ast, options) { return ast; } - const interpolationRegex = /\{\{([\s\S]+?)\}\}/g; + const interpolationRegex = /{{([\S\s]+?)}}/g; return ast.map((node) => { if (!canHaveInterpolation(node)) { return node; @@ -337,7 +337,7 @@ function extractWhitespaces(ast /*, options*/) { const localChildren = []; const [, leadingSpaces, text, trailingSpaces] = child.value.match( - /^(\s*)([\s\S]*?)(\s*)$/ + /^(\s*)([\S\s]*?)(\s*)$/ ); if (leadingSpaces) { diff --git a/src/language-html/printer-html.js b/src/language-html/printer-html.js index 721e07e7a0a8..8baaaff21d40 100644 --- a/src/language-html/printer-html.js +++ b/src/language-html/printer-html.js @@ -120,7 +120,7 @@ function embed(path, print, textToDoc, options) { // lwc: html`` if (options.parser === "lwc") { - const interpolationRegex = /^\{[\s\S]*\}$/; + const interpolationRegex = /^{[\S\s]*}$/; if ( interpolationRegex.test( options.originalText.slice( @@ -1100,7 +1100,7 @@ function printEmbeddedAttributeValue(node, originalTextToDoc, options) { ); } - const interpolationRegex = /\{\{([\s\S]+?)\}\}/g; + const interpolationRegex = /{{([\S\s]+?)}}/g; const value = getValue(); if (interpolationRegex.test(value)) { const parts = []; diff --git a/src/language-html/syntax-vue.js b/src/language-html/syntax-vue.js index 4d5e44e2eaf0..f028eb7e7d71 100644 --- a/src/language-html/syntax-vue.js +++ b/src/language-html/syntax-vue.js @@ -29,7 +29,7 @@ function printVueFor(value, textToDoc) { // modified from https://github.com/vuejs/vue/blob/v2.5.17/src/compiler/parser/index.js#L370-L387 function parseVueFor(value) { const forAliasRE = /([^]*?)\s+(in|of)\s+([^]*)/; - const forIteratorRE = /,([^,}\]]*)(?:,([^,}\]]*))?$/; + const forIteratorRE = /,([^,\]}]*)(?:,([^,\]}]*))?$/; const stripParensRE = /^\(|\)$/g; const inMatch = value.match(forAliasRE); @@ -69,9 +69,9 @@ function printVueSlotScope(value, textToDoc) { function isVueEventBindingExpression(eventBindingValue) { // https://github.com/vuejs/vue/blob/v2.5.17/src/compiler/codegen/events.js#L3-L4 // arrow function or anonymous function - const fnExpRE = /^([\w$_]+|\([^)]*?\))\s*=>|^function\s*\(/; + const fnExpRE = /^([\w$]+|\([^)]*?\))\s*=>|^function\s*\(/; // simple member expression chain (a, a.b, a['b'], a["b"], a[0], a[b]) - const simplePathRE = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['[^']*?']|\["[^"]*?"]|\[\d+]|\[[A-Za-z_$][\w$]*])*$/; + const simplePathRE = /^[$A-Z_a-z][\w$]*(?:\.[$A-Z_a-z][\w$]*|\['[^']*?']|\["[^"]*?"]|\[\d+]|\[[$A-Z_a-z][\w$]*])*$/; // https://github.com/vuejs/vue/blob/v2.5.17/src/compiler/helpers.js#L104 const value = eventBindingValue.trim(); diff --git a/src/language-js/comments.js b/src/language-js/comments.js index 50aa34987355..0044d54b89bf 100644 --- a/src/language-js/comments.js +++ b/src/language-js/comments.js @@ -951,7 +951,7 @@ function getGapRegex(enclosingNode) { ) { // Support degenerate single-element unions and intersections. // E.g.: `type A = /* 1 */ & B` - return /^[\s(&|]*$/; + return /^[\s&(|]*$/; } } diff --git a/src/language-js/embed.js b/src/language-js/embed.js index 679c9d6b37f5..55fde9021264 100644 --- a/src/language-js/embed.js +++ b/src/language-js/embed.js @@ -94,11 +94,11 @@ function embed(path, print, textToDoc, options) { lines[numLines - 2].trim() === ""; const commentsAndWhitespaceOnly = lines.every((line) => - /^\s*(?:#[^\r\n]*)?$/.test(line) + /^\s*(?:#[^\n\r]*)?$/.test(line) ); // Bail out if an interpolation occurs within a comment. - if (!isLast && /#[^\r\n]*$/.test(lines[numLines - 1])) { + if (!isLast && /#[^\n\r]*$/.test(lines[numLines - 1])) { return null; } @@ -204,7 +204,7 @@ function getIndentation(str) { } function uncook(cookedValue) { - return cookedValue.replace(/([\\`]|\$\{)/g, "\\$1"); + return cookedValue.replace(/([\\`]|\${)/g, "\\$1"); } function escapeTemplateCharacters(doc, raw) { @@ -292,7 +292,7 @@ function replacePlaceholders(quasisDoc, expressionDocs) { const placeholder = parts[atPlaceholderIndex]; const rest = parts.slice(atPlaceholderIndex + 1); const placeholderMatch = placeholder.match( - /@prettier-placeholder-(.+)-id([\s\S]*)/ + /@prettier-placeholder-(.+)-id([\S\s]*)/ ); const placeholderID = placeholderMatch[1]; // When the expression has a suffix appended, like: diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 750a0692444d..5d4298d585bd 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -3641,7 +3641,7 @@ function printPathNoParens(path, options, print, args) { ) ); case "NGMicrosyntaxKey": - return /^[a-z_$][a-z0-9_$]*(-[a-z_$][a-z0-9_$])*$/i.test(n.name) + return /^[$_a-z][\w$]*(-[$_a-z][\w$])*$/i.test(n.name) ? n.name : JSON.stringify(n.name); case "NGMicrosyntaxExpression": @@ -5114,7 +5114,7 @@ function printMemberChain(path, options, print) { // letter or just a sequence of _$. The rationale is that they are // likely to be factories. function isFactory(name) { - return /^[A-Z]|^[_$]+$/.test(name); + return /^[A-Z]|^[$_]+$/.test(name); } // In case the Identifier is shorter than tab width, we can keep the diff --git a/src/language-js/utils.js b/src/language-js/utils.js index 78cd6277df35..9aec30eeec6d 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -731,7 +731,7 @@ function isJestEachTemplateLiteral(node, parentNode) { * * Ref: https://github.com/facebook/jest/pull/6102 */ - const jestEachTriggerRegex = /^[xf]?(describe|it|test)$/; + const jestEachTriggerRegex = /^[fx]?(describe|it|test)$/; return ( parentNode.type === "TaggedTemplateExpression" && parentNode.quasi === node && diff --git a/src/language-markdown/embed.js b/src/language-markdown/embed.js index 2919ffce4329..fe5e2bcf2bef 100644 --- a/src/language-markdown/embed.js +++ b/src/language-markdown/embed.js @@ -13,7 +13,7 @@ function embed(path, print, textToDoc, options) { if (node.type === "code" && node.lang !== null) { // only look for the first string so as to support [markdown-preview-enhanced](https://shd101wyy.github.io/markdown-preview-enhanced/#/code-chunk) - const langMatch = node.lang.match(/^[A-Za-z0-9_-]+/); + const langMatch = node.lang.match(/^[\w-]+/); const lang = langMatch ? langMatch[0] : ""; const parser = getParserName(lang); if (parser) { diff --git a/src/language-markdown/parser-markdown.js b/src/language-markdown/parser-markdown.js index a9e4f61836ca..67a352a3628a 100644 --- a/src/language-markdown/parser-markdown.js +++ b/src/language-markdown/parser-markdown.js @@ -80,7 +80,7 @@ function liquid() { proto.inlineTokenizers.liquid = tokenizer; function tokenizer(eat, value) { - const match = value.match(/^({%[\s\S]*?%}|{{[\s\S]*?}})/); + const match = value.match(/^({%[\S\s]*?%}|{{[\S\s]*?}})/); if (match) { return eat(match[0])({ diff --git a/src/language-markdown/printer-markdown.js b/src/language-markdown/printer-markdown.js index c2cd2f40c93a..fa4c566b52fd 100644 --- a/src/language-markdown/printer-markdown.js +++ b/src/language-markdown/printer-markdown.js @@ -81,7 +81,7 @@ function genericPrint(path, options, print) { return printChildren(path, options, print); case "word": return node.value - .replace(/[*$]/g, "\\$&") // escape all `*` and `$` (math) + .replace(/[$*]/g, "\\$&") // escape all `*` and `$` (math) .replace( new RegExp( [ @@ -103,7 +103,7 @@ function genericPrint(path, options, print) { const proseWrap = // leading char that may cause different syntax - nextNode && /^>|^([-+*]|#{1,6}|[0-9]+[.)])$/.test(nextNode.value) + nextNode && /^>|^([*+-]|#{1,6}|\d+[).])$/.test(nextNode.value) ? "never" : options.proseWrap; @@ -236,7 +236,7 @@ function genericPrint(path, options, print) { privateUtil.getLast(parentNode.children) === node ? node.value.trimEnd() : node.value; - const isHtmlComment = /^$/.test(value); + const isHtmlComment = /^$/.test(value); return concat( replaceEndOfLineWith( value, @@ -951,7 +951,7 @@ function clean(ast, newObj, parent) { } if (ast.type === "inlineCode") { - newObj.value = ast.value.replace(/[ \t\n]+/g, " "); + newObj.value = ast.value.replace(/[\t\n ]+/g, " "); } // for insert pragma diff --git a/src/language-markdown/utils.js b/src/language-markdown/utils.js index 05abf00fc1aa..a7a6dde8dd50 100644 --- a/src/language-markdown/utils.js +++ b/src/language-markdown/utils.js @@ -52,7 +52,7 @@ function splitText(text, options) { ? text : text.replace(new RegExp(`(${cjkPattern})\n(${cjkPattern})`, "g"), "$1$2") ) - .split(/([ \t\n]+)/) + .split(/([\t\n ]+)/) .forEach((token, index, tokens) => { // whitespace if (index % 2 === 1) { diff --git a/src/language-yaml/pragma.js b/src/language-yaml/pragma.js index 56225a0d33e4..f65ca0dd2117 100644 --- a/src/language-yaml/pragma.js +++ b/src/language-yaml/pragma.js @@ -5,7 +5,7 @@ function isPragma(text) { } function hasPragma(text) { - return /^\s*#[^\n\S]*@(prettier|format)\s*?(\n|$)/.test(text); + return /^\s*#[^\S\n]*@(prettier|format)\s*?(\n|$)/.test(text); } function insertPragma(text) { diff --git a/src/language-yaml/utils.js b/src/language-yaml/utils.js index 0ff8f6ed79fb..c4bb86426fbc 100644 --- a/src/language-yaml/utils.js +++ b/src/language-yaml/utils.js @@ -246,7 +246,7 @@ function getBlockValueLineContents( : options.originalText .slice(node.position.start.offset, node.position.end.offset) // exclude open line `>` or `|` - .match(/^[^\n]*?\n([\s\S]*)$/)[1]; + .match(/^[^\n]*?\n([\S\s]*)$/)[1]; const leadingSpaceCount = node.indent === null diff --git a/tests_integration/__tests__/patterns-dirs.js b/tests_integration/__tests__/patterns-dirs.js index 0f7b25b23e5b..0818ab93bf66 100644 --- a/tests_integration/__tests__/patterns-dirs.js +++ b/tests_integration/__tests__/patterns-dirs.js @@ -95,7 +95,7 @@ function testPatterns(namePrefix, cliArgs, expected = {}) { (namePrefix ? namePrefix + ": " : "") + "prettier " + cliArgs - .map((arg) => (/^[\w.=/-]+$/.test(arg) ? arg : `'${arg}'`)) + .map((arg) => (/^[\w./=-]+$/.test(arg) ? arg : `'${arg}'`)) .join(" "); describe(testName, () => {