|
1 | 1 | import {replaceStringRaw} from './fix/index.js'; |
2 | | -import {isMethodCall} from './ast/index.js'; |
| 2 | +import {isMethodCall, isNewExpression} from './ast/index.js'; |
3 | 3 |
|
4 | 4 | const MESSAGE_ID_ERROR = 'text-encoding-identifier/error'; |
5 | 5 | const MESSAGE_ID_SUGGESTION = 'text-encoding-identifier/suggestion'; |
@@ -34,31 +34,31 @@ const isFsReadFileEncoding = node => |
34 | 34 | && node.parent.arguments[1] === node |
35 | 35 | && node.parent.arguments[0].type !== 'SpreadElement'; |
36 | 36 |
|
| 37 | +const isJsxElementAttributes = (node, {element, attributes}) => |
| 38 | + node.parent.type === 'JSXAttribute' |
| 39 | + && node.parent.value === node |
| 40 | + && node.parent.name.type === 'JSXIdentifier' |
| 41 | + && attributes.includes(node.parent.name.name.toLowerCase()) |
| 42 | + && node.parent.parent.type === 'JSXOpeningElement' |
| 43 | + && node.parent.parent.attributes.includes(node.parent) |
| 44 | + && node.parent.parent.name.type === 'JSXIdentifier' |
| 45 | + && node.parent.parent.name.name.toLowerCase() === element; |
| 46 | + |
| 47 | +const shouldEnforceDash = node => |
| 48 | + isJsxElementAttributes(node, {element: 'meta', attributes: ['charset']}) |
| 49 | + || isJsxElementAttributes(node, {element: 'form', attributes: ['acceptCharset', 'accept-charset'].map(attribute => attribute.toLowerCase())}) |
| 50 | + || (isNewExpression(node.parent, {name: 'TextDecoder'}) && node.parent.arguments[0] === node); |
| 51 | + |
37 | 52 | /** @param {import('eslint').Rule.RuleContext} context */ |
38 | 53 | const create = context => { |
39 | | - const { |
40 | | - withDash, |
41 | | - } = context.options[0]; |
| 54 | + const options = context.options[0]; |
42 | 55 |
|
43 | 56 | context.on('Literal', node => { |
44 | 57 | if (typeof node.value !== 'string') { |
45 | 58 | return; |
46 | 59 | } |
47 | 60 |
|
48 | | - if ( |
49 | | - // eslint-disable-next-line unicorn/text-encoding-identifier-case |
50 | | - node.value === 'utf-8' |
51 | | - && node.parent.type === 'JSXAttribute' |
52 | | - && node.parent.value === node |
53 | | - && node.parent.name.type === 'JSXIdentifier' |
54 | | - && node.parent.name.name.toLowerCase() === 'charset' |
55 | | - && node.parent.parent.type === 'JSXOpeningElement' |
56 | | - && node.parent.parent.attributes.includes(node.parent) |
57 | | - && node.parent.parent.name.type === 'JSXIdentifier' |
58 | | - && node.parent.parent.name.name.toLowerCase() === 'meta' |
59 | | - ) { |
60 | | - return; |
61 | | - } |
| 61 | + const withDash = options.withDash || shouldEnforceDash(node); |
62 | 62 |
|
63 | 63 | const {raw} = node; |
64 | 64 | const value = raw.slice(1, -1); |
|
0 commit comments