From 1cbb105a6a696ff7255a57a82639a0cb799f913a Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Sun, 25 Nov 2018 17:04:21 -0600 Subject: [PATCH] Chore: refactor template literal feature detection in 'quotes' rule. --- lib/rules/quotes.js | 70 +++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 25 deletions(-) diff --git a/lib/rules/quotes.js b/lib/rules/quotes.js index e0db17fcb7c4..dbb02b0f39f6 100644 --- a/lib/rules/quotes.js +++ b/lib/rules/quotes.js @@ -228,6 +228,31 @@ module.exports = { } } + /** + * Checks whether or not a given TemplateLiteral node is using a template literal feature. + * @param {ASTNode} node - A TemplateLiteral node to check. + * @returns {boolean} Whether or not the TemplateLiteral node is using a template literal feature. + * @private + */ + function usesTemplateLiteralFeature(node) { + const hasTag = node.parent.type === "TaggedTemplateExpression" && node === node.parent.quasi; + if (hasTag) { + return true; + } + + const hasExpression = node.expressions.length > 0; + if (hasExpression) { + return true; + } + + const hasLinebreak = node.quasis.length >= 1 && UNESCAPED_LINEBREAK_PATTERN.test(node.quasis[0].value.raw); + if (hasLinebreak) { + return true; + } + + return false; + } + return { Literal(node) { @@ -260,39 +285,34 @@ module.exports = { TemplateLiteral(node) { - // If backticks are expected or it's a tagged template, then this shouldn't throw an errors + // Don't throw an error if backticks are expected or a template literal feature is in use. if ( allowTemplateLiterals || quoteOption === "backtick" || - node.parent.type === "TaggedTemplateExpression" && node === node.parent.quasi + usesTemplateLiteralFeature(node) ) { return; } - // A warning should be produced if the template literal only has one TemplateElement, and has no unescaped newlines. - const shouldWarn = node.quasis.length === 1 && !UNESCAPED_LINEBREAK_PATTERN.test(node.quasis[0].value.raw); - - if (shouldWarn) { - context.report({ - node, - message: "Strings must use {{description}}.", - data: { - description: settings.description - }, - fix(fixer) { - if (isPartOfDirectivePrologue(node)) { - - /* - * TemplateLiterals in a directive prologue aren't actually directives, but if they're - * in the directive prologue, then fixing them might turn them into directives and change - * the behavior of the code. - */ - return null; - } - return fixer.replaceText(node, settings.convert(sourceCode.getText(node))); + context.report({ + node, + message: "Strings must use {{description}}.", + data: { + description: settings.description + }, + fix(fixer) { + if (isPartOfDirectivePrologue(node)) { + + /* + * TemplateLiterals in a directive prologue aren't actually directives, but if they're + * in the directive prologue, then fixing them might turn them into directives and change + * the behavior of the code. + */ + return null; } - }); - } + return fixer.replaceText(node, settings.convert(sourceCode.getText(node))); + } + }); } };