|
1 | 1 | 'use strict';
|
2 | 2 | const {isOpeningBraceToken} = require('@eslint-community/eslint-utils');
|
3 |
| -const {matches} = require('./selectors/index.js'); |
4 | 3 |
|
5 | 4 | const MESSAGE_ID = 'empty-brace-spaces';
|
6 | 5 | const messages = {
|
7 | 6 | [MESSAGE_ID]: 'Do not add spaces between braces.',
|
8 | 7 | };
|
9 | 8 |
|
10 |
| -const selector = matches([ |
11 |
| - 'BlockStatement[body.length=0]', |
12 |
| - 'ClassBody[body.length=0]', |
13 |
| - 'ObjectExpression[properties.length=0]', |
14 |
| - 'StaticBlock[body.length=0]', |
15 |
| - // Experimental https://github.com/tc39/proposal-record-tuple |
16 |
| - 'RecordExpression[properties.length=0]', |
17 |
| -]); |
| 9 | +const getProblem = (node, context) => { |
| 10 | + const {sourceCode} = context; |
| 11 | + const filter = node.type === 'RecordExpression' |
| 12 | + ? token => token.type === 'Punctuator' && (token.value === '#{' || token.value === '{|') |
| 13 | + : isOpeningBraceToken; |
| 14 | + const openingBrace = sourceCode.getFirstToken(node, {filter}); |
| 15 | + const closingBrace = sourceCode.getLastToken(node); |
| 16 | + const [, start] = openingBrace.range; |
| 17 | + const [end] = closingBrace.range; |
| 18 | + const textBetween = sourceCode.text.slice(start, end); |
| 19 | + |
| 20 | + if (!/^\s+$/.test(textBetween)) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + return { |
| 25 | + loc: { |
| 26 | + start: openingBrace.loc.end, |
| 27 | + end: closingBrace.loc.start, |
| 28 | + }, |
| 29 | + messageId: MESSAGE_ID, |
| 30 | + fix: fixer => fixer.removeRange([start, end]), |
| 31 | + }; |
| 32 | +}; |
18 | 33 |
|
19 | 34 | /** @param {import('eslint').Rule.RuleContext} context */
|
20 |
| -const create = context => ({ |
21 |
| - [selector](node) { |
22 |
| - const {sourceCode} = context; |
23 |
| - const filter = node.type === 'RecordExpression' |
24 |
| - ? token => token.type === 'Punctuator' && (token.value === '#{' || token.value === '{|') |
25 |
| - : isOpeningBraceToken; |
26 |
| - const openingBrace = sourceCode.getFirstToken(node, {filter}); |
27 |
| - const closingBrace = sourceCode.getLastToken(node); |
28 |
| - const [, start] = openingBrace.range; |
29 |
| - const [end] = closingBrace.range; |
30 |
| - const textBetween = sourceCode.text.slice(start, end); |
31 |
| - |
32 |
| - if (!/^\s+$/.test(textBetween)) { |
| 35 | +const create = context => { |
| 36 | + context.on([ |
| 37 | + 'BlockStatement', |
| 38 | + 'ClassBody', |
| 39 | + 'StaticBlock', |
| 40 | + ], node => { |
| 41 | + if (node.body.length > 0) { |
33 | 42 | return;
|
34 | 43 | }
|
35 | 44 |
|
36 |
| - return { |
37 |
| - loc: { |
38 |
| - start: openingBrace.loc.end, |
39 |
| - end: closingBrace.loc.start, |
40 |
| - }, |
41 |
| - messageId: MESSAGE_ID, |
42 |
| - fix: fixer => fixer.removeRange([start, end]), |
43 |
| - }; |
44 |
| - }, |
45 |
| -}); |
| 45 | + return getProblem(node, context); |
| 46 | + }); |
| 47 | + |
| 48 | + context.on([ |
| 49 | + 'ObjectExpression', |
| 50 | + // Experimental https://github.com/tc39/proposal-record-tuple |
| 51 | + 'RecordExpression', |
| 52 | + ], node => { |
| 53 | + if (node.properties.length > 0) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + return getProblem(node, context); |
| 58 | + }); |
| 59 | +}; |
46 | 60 |
|
47 | 61 | /** @type {import('eslint').Rule.RuleModule} */
|
48 | 62 | module.exports = {
|
|
0 commit comments