Add caseSingleStatement lint rule
MODIFY
packages/@romejs/diagnostics/categories.ts
packages/@romejs/diagnostics/categories.ts
@@ -20,6 +20,7 @@ export type DiagnosticCategory =
| 'format/disabled'
| 'internalError/httpServer'
| 'internalError/request'
+ | 'lint/caseSingleStatement'
| 'lint/defaultExportSameBasename'
| 'lint/disabled'
| 'lint/duplicateImportSource'

MODIFY
packages/@romejs/diagnostics/descriptions.ts
packages/@romejs/diagnostics/descriptions.ts
@@ -258,6 +258,12 @@ export const descriptions = createMessages(
message: markup`Undeclared variable <emphasis>${name}</emphasis>`,
}),
+ CASE_SINGLE_STATEMENT: {
+ category: 'lint/caseSingleStatement',
+ message: 'A switch case should only have a single statement. If you want more then wrap it in a block.',
+ fixable: true,
+ },
+
NO_CATCH_ASSIGN: {
category: 'lint/noCatchAssign',
message: "Don't reassign catch parameters",

ADD
packages/@romejs/js-compiler/transforms/lint/caseSingleStatement.test.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 test from '@romejs/test';
+import {testLintMultiple} from '../../api/lint.test';
+
+test('case single statement', async (t) => {
+ await testLintMultiple(
+ t,
+ [
+ // VALID
+ "switch (foo) {case true: case false: return 'yes';}", // Single statement
+ 'switch (foo) {case true: {}}', // Single block
+ 'switch (foo) {case true:}', // Nothing
+
+ // INVALID
+ "switch (foo) {case true: case false: let foo = ''; foo;}", // Multiple statements
+ ],
+ {category: 'lint/caseSingleStatement'},
+ );
+});

ADD
packages/@romejs/js-compiler/transforms/lint/caseSingleStatement.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 {Path} from '@romejs/js-compiler';
+import {AnyNode, blockStatement} from '@romejs/js-ast';
+import {descriptions} from '@romejs/diagnostics';
+
+export default {
+ name: 'caseSingleStatement',
+ enter(path: Path): AnyNode {
+ const {node, context} = path;
+
+ if (node.type === 'SwitchCase') {
+ if (node.consequent.length > 1) {
+ const {suppressed} = context.addNodeDiagnostic(
+ node,
+ descriptions.LINT.CASE_SINGLE_STATEMENT,
+ );
+
+ if (!suppressed) {
+ return {
+ ...node,
+ consequent: [blockStatement.quick(node.consequent)],
+ };
+ }
+ }
+ }
+
+ return node;
+ },
+};

MODIFY
packages/@romejs/js-compiler/transforms/lint/index.ts
packages/@romejs/js-compiler/transforms/lint/index.ts
*/
// Add imports in alphabetical order.
+import caseSingleStatement from './caseSingleStatement';
import defaultExportSameBasename from './defaultExportSameBasename';
import duplicateImportSource from './duplicateImportSource';
import emptyBlocks from './emptyBlocks';
@@ -47,6 +48,7 @@ import unusedVariables from './unusedVariables';
export const lintTransforms = [
defaultExportSameBasename,
duplicateImportSource,
+ caseSingleStatement,
emptyBlocks,
getterReturn,
noArguments,

Add caseSingleStatement lint rule