Add preferWhile rule
MODIFY
packages/@romejs/diagnostics/categories.ts
packages/@romejs/diagnostics/categories.ts
@@ -57,6 +57,7 @@ export type DiagnosticCategory =
| 'lint/pendingFixes'
| 'lint/preferFunctionDeclarations'
| 'lint/preferTemplate'
+ | 'lint/preferWhile'
| 'lint/restrictedGlobals'
| 'lint/singleVarDeclarator'
| 'lint/sortImportExportSpecifiers'

MODIFY
packages/@romejs/diagnostics/descriptions.ts
packages/@romejs/diagnostics/descriptions.ts
@@ -316,6 +316,10 @@ export const descriptions = createMessages({
category: 'lint/preferTemplate',
message: 'Template literals are preferred over string concatenation',
},
+ PREFER_WHILE: {
+ category: 'lint/preferWhile',
+ message: 'A while loop should be used over a for loop',
+ },
UNSAFE_NEGATION: {
category: 'lint/unsafeNegation',
message: 'Unsafe usage of negation operator in left side of binary expression',

MODIFY
packages/@romejs/js-compiler/lint/rules/index.ts
packages/@romejs/js-compiler/lint/rules/index.ts
@@ -41,6 +41,7 @@ import noUnsafeFinally from './noUnsafeFinally';
import noVar from './noVar';
import preferFunctionDeclarations from './preferFunctionDeclarations';
import preferTemplate from './preferTemplate';
+import preferWhile from './preferWhile';
import restrictedGlobals from './restrictedGlobals';
import singleVarDeclarator from './singleVarDeclarator';
import sortImportExportSpecifiers from './sortImportExportSpecifiers';
@@ -86,6 +87,7 @@ export const lintTransforms = [
noVar,
preferFunctionDeclarations,
preferTemplate,
+ preferWhile,
restrictedGlobals,
singleVarDeclarator,
sortImportExportSpecifiers,

ADD
packages/@romejs/js-compiler/lint/rules/preferWhile.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 'rome';
+import {testLintMultiple} from '../testHelpers';
+
+test(
+ 'prefer while',
+ async (t) => {
+ await testLintMultiple(
+ t,
+ [`for (; x.running;) x.step();`, `for (;;) doSomething();`],
+ {
+ category: 'lint/preferWhile',
+ },
+ );
+ },
+);

ADD
packages/@romejs/js-compiler/lint/rules/preferWhile.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 {descriptions} from '@romejs/diagnostics';
+import {Path, TransformExitResult} from '@romejs/js-compiler';
+import {booleanLiteral, whileStatement} from '@romejs/js-ast';
+
+export default {
+ name: 'preferWhile',
+ enter(path: Path): TransformExitResult {
+ const {context, node} = path;
+
+ if (
+ node.type === 'ForStatement' &&
+ node.init === undefined &&
+ node.update === undefined
+ ) {
+ return context.addFixableDiagnostic(
+ {
+ old: node,
+ fixed: whileStatement.create(
+ {
+ test: node.test !== undefined
+ ? node.test
+ : booleanLiteral.quick(true),
+ body: node.body,
+ leadingComments: node.leadingComments,
+ trailingComments: node.trailingComments,
+ },
+ node,
+ ),
+ },
+ descriptions.LINT.PREFER_WHILE,
+ );
+ }
+
+ return node;
+ },
+};

Add preferWhile rule