Skip to content

Commit

Permalink
empty-brace-spaces: Support experimental syntax (#1276)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 17, 2021
1 parent eb2a642 commit fa68b6f
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 4 deletions.
36 changes: 32 additions & 4 deletions rules/empty-brace-spaces.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const {isOpeningBraceToken} = require('eslint-utils');
const getDocumentationUrl = require('./utils/get-documentation-url');

const MESSAGE_ID = 'empty-brace-spaces';
Expand All @@ -10,17 +11,44 @@ const selector = `:matches(${
[
'BlockStatement[body.length=0]',
'ClassBody[body.length=0]',
'ObjectExpression[properties.length=0]'
'ObjectExpression[properties.length=0]',
// Experimental https://github.com/tc39/proposal-record-tuple
'RecordExpression[properties.length=0]',
// Experimental https://github.com/tc39/proposal-class-static-block
'StaticBlock[body.length=0]'
].join(', ')
})`;

/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const sourceCode = context.getSourceCode();
return {
[selector](node) {
let [start, end] = node.range;
start += 1;
end -= 1;
let startOffset = 1;
let endOffset = -1;

switch (node.type) {
case 'RecordExpression': {
startOffset = 2;
const [start] = node.range;
const firstTwoCharacters = sourceCode.text.slice(start, start + 2);
if (firstTwoCharacters === '{|') {
endOffset = -2;
}

break;
}

case 'StaticBlock': {
const openingBraceToken = sourceCode.getFirstToken(node, isOpeningBraceToken);
startOffset = openingBraceToken.range[1] - node.range[0];
break;
}
// No default
}

const start = node.range[0] + startOffset;
const end = node.range[1] + endOffset;

if (!/^\s+$/.test(sourceCode.text.slice(start, end))) {
return;
Expand Down
66 changes: 66 additions & 0 deletions test/empty-brace-spaces.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,69 @@ test.snapshot({
`
]
});

const enableBabelPlugins = plugins => ({
babelOptions: {
parserOpts: {
plugins
}
}
});
const enableBabelPlugin = plugin => enableBabelPlugins([plugin]);
test.babel({
valid: [],
invalid: [
{
code: outdent`
const foo = do {
};
`,
output: 'const foo = do {};',
parserOptions: enableBabelPlugin('doExpressions'),
errors: 1
},
{
code: 'const record = #{ };',
output: 'const record = #{};',
parserOptions: enableBabelPlugin(['recordAndTuple', {syntaxType: 'hash'}]),
errors: 1
},
{
code: 'const record = {| |};',
output: 'const record = {||};',
parserOptions: enableBabelPlugin(['recordAndTuple', {syntaxType: 'bar'}]),
errors: 1
},
{
code: outdent`
class Foo {
static {
}
}
`,
output: outdent`
class Foo {
static {}
}
`,
parserOptions: enableBabelPlugin('classStaticBlock'),
errors: 1
},
// ESLint can't parse this now
// {
// code: 'const foo = module { };',
// output: 'const foo = module {};',
// parserOptions: enableBabelPlugin('moduleBlocks'),
// errors: 1
// },
{
code: outdent`
const foo = async do {
};
`,
output: 'const foo = async do {};',
parserOptions: enableBabelPlugins(['doExpressions', 'asyncDoExpressions']),
errors: 1
}
]
});

0 comments on commit fa68b6f

Please sign in to comment.