Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

empty-brace-spaces: Support experimental syntax #1276

Merged
merged 2 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
]
});