|
| 1 | +'use strict'; |
| 2 | +const stripIndent = require('strip-indent'); |
| 3 | +const indentString = require('indent-string'); |
| 4 | +const esquery = require('esquery'); |
| 5 | +const {replaceTemplateElement} = require('./fix/index.js'); |
| 6 | + |
| 7 | +const MESSAGE_ID_IMPROPERLY_INDENTED_TEMPLATE = 'template-indent'; |
| 8 | +const messages = { |
| 9 | + [MESSAGE_ID_IMPROPERLY_INDENTED_TEMPLATE]: 'Templates should be properly indented.', |
| 10 | +}; |
| 11 | + |
| 12 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 13 | +const create = context => { |
| 14 | + const sourceCode = context.getSourceCode(); |
| 15 | + const options = { |
| 16 | + tags: ['outdent', 'dedent', 'gql', 'sql', 'html', 'styled'], |
| 17 | + functions: ['dedent', 'stripIndent'], |
| 18 | + selectors: [], |
| 19 | + comments: ['HTML', 'indent'], |
| 20 | + ...context.options[0], |
| 21 | + }; |
| 22 | + |
| 23 | + options.comments = options.comments.map(comment => comment.toLowerCase()); |
| 24 | + |
| 25 | + const selectors = [ |
| 26 | + ...options.tags.map(tag => `TaggedTemplateExpression[tag.name="${tag}"] > .quasi`), |
| 27 | + ...options.functions.map(fn => `CallExpression[callee.name="${fn}"] > .arguments`), |
| 28 | + ...options.selectors, |
| 29 | + ]; |
| 30 | + |
| 31 | + /** @param {import('@babel/core').types.TemplateLiteral} node */ |
| 32 | + const indentTemplateLiteralNode = node => { |
| 33 | + const delimiter = '__PLACEHOLDER__' + Math.random(); |
| 34 | + const joined = node.quasis |
| 35 | + .map(quasi => { |
| 36 | + const untrimmedText = sourceCode.getText(quasi); |
| 37 | + return untrimmedText.slice(1, quasi.tail ? -1 : -2); |
| 38 | + }) |
| 39 | + .join(delimiter); |
| 40 | + |
| 41 | + const eolMatch = joined.match(/\r?\n/); |
| 42 | + if (!eolMatch) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + const eol = eolMatch[0]; |
| 47 | + |
| 48 | + const startLine = sourceCode.lines[node.loc.start.line - 1]; |
| 49 | + const marginMatch = startLine.match(/^(\s*)\S/); |
| 50 | + const parentMargin = marginMatch ? marginMatch[1] : ''; |
| 51 | + |
| 52 | + let indent; |
| 53 | + if (typeof options.indent === 'string') { |
| 54 | + indent = options.indent; |
| 55 | + } else if (typeof options.indent === 'number') { |
| 56 | + indent = ' '.repeat(options.indent); |
| 57 | + } else { |
| 58 | + const tabs = parentMargin.startsWith('\t'); |
| 59 | + indent = tabs ? '\t' : ' '; |
| 60 | + } |
| 61 | + |
| 62 | + const dedented = stripIndent(joined); |
| 63 | + const fixed |
| 64 | + = eol |
| 65 | + + indentString(dedented.trim(), 1, {indent: parentMargin + indent}) |
| 66 | + + eol |
| 67 | + + parentMargin; |
| 68 | + |
| 69 | + if (fixed === joined) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + context.report({ |
| 74 | + node, |
| 75 | + messageId: MESSAGE_ID_IMPROPERLY_INDENTED_TEMPLATE, |
| 76 | + fix: fixer => fixed |
| 77 | + .split(delimiter) |
| 78 | + .map((replacement, index) => replaceTemplateElement(fixer, node.quasis[index], replacement)), |
| 79 | + }); |
| 80 | + }; |
| 81 | + |
| 82 | + return { |
| 83 | + /** @param {import('@babel/core').types.TemplateLiteral} node */ |
| 84 | + TemplateLiteral: node => { |
| 85 | + if (options.comments.length > 0) { |
| 86 | + const previousToken = sourceCode.getTokenBefore(node, {includeComments: true}); |
| 87 | + if (previousToken && previousToken.type === 'Block' && options.comments.includes(previousToken.value.trim().toLowerCase())) { |
| 88 | + indentTemplateLiteralNode(node); |
| 89 | + return; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + const ancestry = context.getAncestors().reverse(); |
| 94 | + const shouldIndent = selectors.some(selector => esquery.matches(node, esquery.parse(selector), ancestry)); |
| 95 | + |
| 96 | + if (shouldIndent) { |
| 97 | + indentTemplateLiteralNode(node); |
| 98 | + } |
| 99 | + }, |
| 100 | + }; |
| 101 | +}; |
| 102 | + |
| 103 | +/** @type {import('json-schema').JSONSchema7[]} */ |
| 104 | +const schema = [ |
| 105 | + { |
| 106 | + type: 'object', |
| 107 | + properties: { |
| 108 | + indent: { |
| 109 | + oneOf: [ |
| 110 | + { |
| 111 | + type: 'string', |
| 112 | + pattern: /^\s+$/.source, |
| 113 | + }, |
| 114 | + { |
| 115 | + type: 'integer', |
| 116 | + minimum: 1, |
| 117 | + }, |
| 118 | + ], |
| 119 | + }, |
| 120 | + tags: { |
| 121 | + type: 'array', |
| 122 | + uniqueItems: true, |
| 123 | + items: { |
| 124 | + type: 'string', |
| 125 | + }, |
| 126 | + }, |
| 127 | + functions: { |
| 128 | + type: 'array', |
| 129 | + uniqueItems: true, |
| 130 | + items: { |
| 131 | + type: 'string', |
| 132 | + }, |
| 133 | + }, |
| 134 | + selectors: { |
| 135 | + type: 'array', |
| 136 | + uniqueItems: true, |
| 137 | + items: { |
| 138 | + type: 'string', |
| 139 | + }, |
| 140 | + }, |
| 141 | + comments: { |
| 142 | + type: 'array', |
| 143 | + uniqueItems: true, |
| 144 | + items: { |
| 145 | + type: 'string', |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + additionalProperties: false, |
| 150 | + }, |
| 151 | +]; |
| 152 | + |
| 153 | +module.exports = { |
| 154 | + create, |
| 155 | + meta: { |
| 156 | + type: 'suggestion', |
| 157 | + docs: { |
| 158 | + description: 'Fix whitespace-insensitive template indentation.', |
| 159 | + }, |
| 160 | + fixable: 'code', |
| 161 | + schema, |
| 162 | + messages, |
| 163 | + }, |
| 164 | +}; |
0 commit comments