Add noShorthandArrayType lint rule
MODIFY
packages/@romejs/diagnostics/categories.ts
packages/@romejs/diagnostics/categories.ts
@@ -45,6 +45,7 @@ export type DiagnosticCategory =
| 'lint/noLabelVar'
| 'lint/noReferenceToNonExistingGroup'
| 'lint/noShadowRestrictedNames'
+ | 'lint/noShorthandArrayType'
| 'lint/noTemplateCurlyInString'
| 'lint/noUnsafeFinally'
| 'lint/preferFunctionDeclarations'

MODIFY
packages/@romejs/diagnostics/descriptions.ts
packages/@romejs/diagnostics/descriptions.ts
@@ -281,6 +281,12 @@ export const descriptions = createMessages({
message: 'Variable declarations using `var` are disallowed, use `let` or `const` instead.',
},
+ NO_SHORTHAND_ARRAY_TYPE: {
+ fixable: true,
+ category: 'lint/noShorthandArrayType',
+ message: 'Use Array<T> instead of shorthand T[]',
+ },
+
NO_UNSAFE_FINALLY: (type: string) =>
({
category: 'lint/noUnsafeFinally',

MODIFY
packages/@romejs/js-compiler/transforms/lint/index.ts
packages/@romejs/js-compiler/transforms/lint/index.ts
@@ -38,6 +38,7 @@ import unsafeNegation from './unsafeNegation';
import unusedVariables from './unusedVariables';
import singleVarDeclarator from './singleVarDeclarator';
import noReferenceToNonExistingGroup from './noReferenceToNonExistingGroup';
+import noShorthandArrayType from './noShorthandArrayType';
// Add transforms in alphabetical order.
export const lintTransforms = [
@@ -63,6 +64,7 @@ export const lintTransforms = [
noReferenceToNonExistingGroup,
noMultipleSpacesInRegularExpressionLiterals,
noShadowRestrictedNames,
+ noShorthandArrayType,
noTemplateCurlyInString,
noUnsafeFinally,
noVar,

ADD
packages/@romejs/js-compiler/transforms/lint/noShorthandArrayType.test.md
+# `noShorthandArrayType.test.ts`
+
+**DO NOT MODIFY**. This file has been autogenerated. Run `rome test packages/@romejs/js-compiler/transforms/lint/noShorthandArrayType.test.ts --update-snapshots` to update.
+
+## `no shorthand array type`
+
+### `0`
+
+```javascript
+Object {
+ src: '\n let valid: Array<foo>;\n let invalid = bar[];\n '
+ suppressions: Array []
+ diagnostics: Array [
+ Object {
+ origins: Array [Object {category: 'js-parser'}]
+ description: Object {
+ category: 'parse/js'
+ message: PARTIAL_BLESSED_DIAGNOSTIC_MESSAGE {value: 'Unknown start to an member expression computed property'}
+ }
+ location: Object {
+ filename: 'unknown'
+ mtime: undefined
+ sourceType: 'module'
+ end: Object {
+ column: 22
+ index: 50
+ line: 3
+ }
+ start: Object {
+ column: 22
+ index: 50
+ line: 3
+ }
+ }
+ }
+ ]
+}
+```
+
+### `1`
+
+```javascript
+Object {
+ src: '\n let valid: Array<foo>;\n let invalid = bar[];\n '
+ suppressions: Array []
+ diagnostics: Array [
+ Object {
+ origins: Array [Object {category: 'js-parser'}]
+ description: Object {
+ category: 'parse/js'
+ message: PARTIAL_BLESSED_DIAGNOSTIC_MESSAGE {value: 'Unknown start to an member expression computed property'}
+ }
+ location: Object {
+ filename: 'unknown'
+ mtime: undefined
+ sourceType: 'module'
+ end: Object {
+ column: 22
+ index: 50
+ line: 3
+ }
+ start: Object {
+ column: 22
+ index: 50
+ line: 3
+ }
+ }
+ }
+ ]
+}
+```

ADD
packages/@romejs/js-compiler/transforms/lint/noShorthandArrayType.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 {testLint} from '../../api/lint.test';
+
+test('no shorthand array type', async (t) => {
+ // TypeScript
+ const ts = await testLint(`
+ let valid: Array<foo>;
+ let invalid = bar[];
+ `, true, 'module', ['ts']);
+ t.snapshot(ts);
+
+ // Flow
+ const flow = await testLint(`
+ let valid: Array<foo>;
+ let invalid = bar[];
+ `, true, 'module', ['flow']);
+ t.snapshot(flow);
+});

ADD
packages/@romejs/js-compiler/transforms/lint/noShorthandArrayType.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 {TransformExitResult} from '@romejs/js-compiler/types';
+import {
+ tsTypeReference,
+ referenceIdentifier,
+ tsTypeParameterInstantiation,
+ flowGenericTypeAnnotation,
+ flowTypeParameterInstantiation,
+} from '@romejs/js-ast';
+import {descriptions} from '@romejs/diagnostics';
+
+export default {
+ name: 'noShorthandArrayType',
+ enter(path: Path): TransformExitResult {
+ const {node, context} = path;
+
+ if (node.type === 'TSArrayType') {
+ context.addNodeDiagnostic(node, descriptions.LINT.NO_SHORTHAND_ARRAY_TYPE);
+
+ return tsTypeReference.create({
+ typeName: referenceIdentifier.quick('Array'),
+ typeParameters: tsTypeParameterInstantiation.create({
+ params: [node.elementType],
+ }),
+ });
+ }
+
+ if (node.type === 'FlowArrayTypeAnnotation') {
+ context.addNodeDiagnostic(node, descriptions.LINT.NO_SHORTHAND_ARRAY_TYPE);
+
+ return flowGenericTypeAnnotation.create({
+ id: referenceIdentifier.quick('Array'),
+ typeParameters: flowTypeParameterInstantiation.create({
+ params: [node.elementType],
+ }),
+ });
+ }
+
+ return node;
+ },
+};

Add noShorthandArrayType lint rule