|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
-
-
- "use strict";
-
-
-
-
-
- const astUtils = require("../util/ast-utils");
-
-
-
-
-
- const QUOTE_SETTINGS = {
- double: {
- quote: "\"",
- alternateQuote: "'",
- description: "doublequote"
- },
- single: {
- quote: "'",
- alternateQuote: "\"",
- description: "singlequote"
- },
- backtick: {
- quote: "`",
- alternateQuote: "\"",
- description: "backtick"
- }
- };
-
-
- const UNESCAPED_LINEBREAK_PATTERN = new RegExp(String.raw`(^|[^\\])(\\\\)*[${Array.from(astUtils.LINEBREAKS).join("")}]`);
-
-
- QUOTE_SETTINGS.double.convert =
- QUOTE_SETTINGS.single.convert =
- QUOTE_SETTINGS.backtick.convert = function(str) {
- const newQuote = this.quote;
- const oldQuote = str[0];
-
- if (newQuote === oldQuote) {
- return str;
- }
- return newQuote + str.slice(1, -1).replace(/\\(\${|\r\n?|\n|.)|["'`]|\${|(\r\n?|\n)/g, (match, escaped, newline) => {
- if (escaped === oldQuote || oldQuote === "`" && escaped === "${") {
- return escaped;
- }
- if (match === newQuote || newQuote === "`" && match === "${") {
- return `\\${match}`;
- }
- if (newline && oldQuote === "`") {
- return "\\n";
- }
- return match;
- }) + newQuote;
- };
-
- const AVOID_ESCAPE = "avoid-escape";
-
-
-
-
-
- module.exports = {
- meta: {
- type: "layout",
-
- docs: {
- description: "enforce the consistent use of either backticks, double, or single quotes",
- category: "Stylistic Issues",
- recommended: false,
- url: "https://eslint.org/docs/rules/quotes"
- },
-
- fixable: "code",
-
- schema: [
- {
- enum: ["single", "double", "backtick"]
- },
- {
- anyOf: [
- {
- enum: ["avoid-escape"]
- },
- {
- type: "object",
- properties: {
- avoidEscape: {
- type: "boolean"
- },
- allowTemplateLiterals: {
- type: "boolean"
- }
- },
- additionalProperties: false
- }
- ]
- }
- ]
- },
-
- create(context) {
-
- const quoteOption = context.options[0],
- settings = QUOTE_SETTINGS[quoteOption || "double"],
- options = context.options[1],
- allowTemplateLiterals = options && options.allowTemplateLiterals === true,
- sourceCode = context.getSourceCode();
- let avoidEscape = options && options.avoidEscape === true;
-
-
- if (options === AVOID_ESCAPE) {
- avoidEscape = true;
- }
-
-
-
- function isJSXLiteral(node) {
- return node.parent.type === "JSXAttribute" || node.parent.type === "JSXElement" || node.parent.type === "JSXFragment";
- }
-
-
-
- function isDirective(node) {
- return (
- node.type === "ExpressionStatement" &&
- node.expression.type === "Literal" &&
- typeof node.expression.value === "string"
- );
- }
-
-
-
- function isPartOfDirectivePrologue(node) {
- const block = node.parent.parent;
-
- if (block.type !== "Program" && (block.type !== "BlockStatement" || !astUtils.isFunction(block.parent))) {
- return false;
- }
-
-
- for (let i = 0; i < block.body.length; ++i) {
- const statement = block.body[i];
-
- if (statement === node.parent) {
- return true;
- }
- if (!isDirective(statement)) {
- break;
- }
- }
-
- return false;
- }
-
-
-
- function isAllowedAsNonBacktick(node) {
- const parent = node.parent;
-
- switch (parent.type) {
-
-
- case "ExpressionStatement":
- return isPartOfDirectivePrologue(node);
-
-
- case "Property":
- case "MethodDefinition":
- return parent.key === node && !parent.computed;
-
-
- case "ImportDeclaration":
- case "ExportNamedDeclaration":
- case "ExportAllDeclaration":
- return parent.source === node;
-
-
- default:
- return false;
- }
- }
-
- return {
-
- Literal(node) {
- const val = node.value,
- rawVal = node.raw;
-
- if (settings && typeof val === "string") {
- let isValid = (quoteOption === "backtick" && isAllowedAsNonBacktick(node)) ||
- isJSXLiteral(node) ||
- astUtils.isSurroundedBy(rawVal, settings.quote);
-
- if (!isValid && avoidEscape) {
- isValid = astUtils.isSurroundedBy(rawVal, settings.alternateQuote) && rawVal.indexOf(settings.quote) >= 0;
- }
-
- if (!isValid) {
- context.report({
- node,
- message: "Strings must use {{description}}.",
- data: {
- description: settings.description
- },
- fix(fixer) {
- return fixer.replaceText(node, settings.convert(node.raw));
- }
- });
- }
- }
- },
-
- TemplateLiteral(node) {
-
-
- if (
- allowTemplateLiterals ||
- quoteOption === "backtick" ||
- node.parent.type === "TaggedTemplateExpression" && node === node.parent.quasi
- ) {
- return;
- }
-
-
- 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)) {
-
-
-
- return null;
- }
- return fixer.replaceText(node, settings.convert(sourceCode.getText(node)));
- }
- });
- }
- }
- };
-
- }
- };
|