adds noLabelVar to linter
MODIFY
packages/@romejs/js-compiler/__rtests__/lint.ts
packages/@romejs/js-compiler/__rtests__/lint.ts
@@ -93,3 +93,19 @@ test('format enabled in project config should result in regenerated file', async
);
t.is(res.src, "foobar('yes');\n");
});
+
+test('no label var', async t => {
+ const badLabel = await testLint( `
+ const x = "test";
+ x: const y = "test";
+ `, LINT_ENABLED_FORMAT_DISABLED_CONFIG);
+
+ t.truthy(badLabel.diagnostics.find(d => d.category === 'lint/noLabelVar'));
+
+ const okLabel = await testLint( `
+ const x = "test";
+ z: const y = "test";
+ `, LINT_ENABLED_FORMAT_DISABLED_CONFIG);
+
+ t.falsy(okLabel.diagnostics.find(d => d.category === 'lint/noLabelVar'));
+});

MODIFY
packages/@romejs/js-compiler/transforms/lint/index.ts
packages/@romejs/js-compiler/transforms/lint/index.ts
@@ -9,10 +9,12 @@ import defaultExportSameBasename from './defaultExportSameBasename';
import undeclaredVariables from './undeclaredVariables';
import unusedVariables from './unusedVariables';
import emptyBlocks from './emptyBlocks';
+import noLabelVar from './noLabelVar';
export const lintTransforms = [
undeclaredVariables,
defaultExportSameBasename,
unusedVariables,
emptyBlocks,
+ noLabelVar,
];

ADD
packages/@romejs/js-compiler/transforms/lint/noLabelVar.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} from '@romejs/js-ast';
+
+export default {
+ name: 'noLabelVar',
+ enter(path: Path): AnyNode {
+ const {node, scope} = path;
+
+ if (
+ node.type === 'LabeledStatement'
+ ) {
+ const name = node.label.name;
+ const binding = scope.getBinding(name);
+ const isDefined =
+ binding !== undefined ||
+ scope.getRootScope().isGlobal(name);
+
+ if (isDefined) {
+ path.context.addNodeDiagnostic(node, {
+ category: 'lint/noLabelVar',
+ message: 'Labels should not be variable names',
+ });
+ }
+ }
+
+ return node;
+ },
+};

adds noLabelVar to linter