Add preferBlockStatements rule
MODIFY
packages/@romejs/diagnostics/categories.ts
packages/@romejs/diagnostics/categories.ts
@@ -55,6 +55,7 @@ export type DiagnosticCategory =
| 'lint/noUnsafeFinally'
| 'lint/noVar'
| 'lint/pendingFixes'
+ | 'lint/preferBlockStatements'
| 'lint/preferFunctionDeclarations'
| 'lint/preferTemplate'
| 'lint/preferWhile'

MODIFY
packages/@romejs/diagnostics/descriptions.ts
packages/@romejs/diagnostics/descriptions.ts
@@ -312,6 +312,10 @@ export const descriptions = createMessages({
},
],
}),
+ PREFER_BLOCK_STATEMENT: {
+ category: 'lint/preferBlockStatements',
+ message: 'Block statements are preferred in this position',
+ },
PREFER_TEMPLATE: {
category: 'lint/preferTemplate',
message: 'Template literals are preferred over string concatenation',

MODIFY
packages/@romejs/js-compiler/lint/rules/index.ts
packages/@romejs/js-compiler/lint/rules/index.ts
@@ -39,6 +39,7 @@ import noShorthandArrayType from './noShorthandArrayType';
import noTemplateCurlyInString from './noTemplateCurlyInString';
import noUnsafeFinally from './noUnsafeFinally';
import noVar from './noVar';
+import preferBlockStatements from './preferBlockStatements';
import preferFunctionDeclarations from './preferFunctionDeclarations';
import preferTemplate from './preferTemplate';
import preferWhile from './preferWhile';
@@ -85,6 +86,7 @@ export const lintTransforms = [
noTemplateCurlyInString,
noUnsafeFinally,
noVar,
+ preferBlockStatements,
preferFunctionDeclarations,
preferTemplate,
preferWhile,

ADD
packages/@romejs/js-compiler/lint/rules/preferBlockStatements.ts
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+import {CompilerContext, Path} from '@romejs/js-compiler';
+import {AnyNode, AnyStatement, blockStatement} from '@romejs/js-ast';
+import {descriptions} from '@romejs/diagnostics';
+
+function addDiagnostic(context: CompilerContext, statement: AnyStatement): void {
+ context.addFixableDiagnostic(
+ {
+ old: statement,
+ fixed: blockStatement.quick([statement]),
+ },
+ descriptions.LINT.PREFER_BLOCK_STATEMENT,
+ );
+}
+
+export default {
+ name: 'preferBlockStatements',
+ enter(path: Path): AnyNode {
+ const {context, node} = path;
+
+ if (node.type === 'IfStatement') {
+ if (node.consequent.type !== 'BlockStatement') {
+ addDiagnostic(context, node.consequent);
+ }
+
+ if (
+ node.alternate !== undefined &&
+ node.alternate.type !== 'BlockStatement' &&
+ node.alternate.type !== 'IfStatement'
+ ) {
+ addDiagnostic(context, node.alternate);
+ }
+ } else if (
+ node.type === 'ForStatement' ||
+ node.type === 'ForInStatement' ||
+ node.type === 'ForOfStatement' ||
+ node.type === 'DoWhileStatement' ||
+ node.type === 'WhileStatement' ||
+ node.type === 'WithStatement'
+ ) {
+ if (
+ node.body.type !== 'BlockStatement' &&
+ node.body.type !== 'EmptyStatement'
+ ) {
+ addDiagnostic(context, node.body);
+ }
+ }
+
+ return node;
+ },
+};

Add preferBlockStatements rule