add noDuplicateGroupNamesInRegularExpressions lint rule
MODIFY
packages/@romejs/diagnostics/categories.ts
packages/@romejs/diagnostics/categories.ts
@@ -24,6 +24,7 @@ export type DiagnosticCategory =
| 'lint/pendingFixes'
| 'lint/defaultExportSameBasename'
| 'lint/noMultipleSpacesInRegularExpressionLiterals'
+ | 'lint/noDuplicateGroupNamesInRegularExpressions'
| 'lint/noVar'
| 'lint/emptyBlocks'
| 'lint/getterReturn'

MODIFY
packages/@romejs/js-compiler/transforms/lint/index.ts
packages/@romejs/js-compiler/transforms/lint/index.ts
@@ -33,6 +33,7 @@ import sparseArray from './sparseArray';
import undeclaredVariables from './undeclaredVariables';
import unsafeNegation from './unsafeNegation';
import unusedVariables from './unusedVariables';
+import noDuplicateGroupNamesInRegularExpressions from './noDuplicateGroupNamesInRegularExpressions';
// Add transforms in alphabetical order.
export const lintTransforms = [
@@ -63,4 +64,5 @@ export const lintTransforms = [
undeclaredVariables,
unsafeNegation,
unusedVariables,
+ noDuplicateGroupNamesInRegularExpressions,
];

ADD
packages/@romejs/js-compiler/transforms/lint/noDuplicateGroupNamesInRegularExpressions.test.md
+# `noDuplicateGroupNamesInRegularExpressions.test.ts`
+
+**DO NOT MODIFY**. This file has been autogenerated. Run `rome test packages/@romejs/js-compiler/transforms/lint/noDuplicateGroupNamesInRegularExpressions.test.ts --update-snapshots` to update.
+
+## `disallow duplicate group names in regular expression`
+
+```javascript
+Object {
+ src: '/(?<month>[0-9])-(?<year>[0-9])-(?<month>[0-9])-(?<year>[0-9])-(?<day>[0-9])-([0-9])/'
+ suppressions: Array []
+ diagnostics: Array [
+ Object {
+ category: 'lint/noDuplicateGroupNamesInRegularExpressions'
+ filename: 'unknown'
+ language: 'js'
+ message: 'Duplicate group name <emphasis>month</emphasis> in regular expression'
+ mtime: undefined
+ sourceType: 'module'
+ origins: Array [Object {category: 'lint'}]
+ end: Object {
+ column: 47
+ index: 47
+ line: 1
+ }
+ start: Object {
+ column: 32
+ index: 32
+ line: 1
+ }
+ }
+ Object {
+ category: 'lint/noDuplicateGroupNamesInRegularExpressions'
+ filename: 'unknown'
+ language: 'js'
+ message: 'Duplicate group name <emphasis>year</emphasis> in regular expression'
+ mtime: undefined
+ sourceType: 'module'
+ origins: Array [Object {category: 'lint'}]
+ end: Object {
+ column: 62
+ index: 62
+ line: 1
+ }
+ start: Object {
+ column: 48
+ index: 48
+ line: 1
+ }
+ }
+ ]
+}
+```

ADD
packages/@romejs/js-compiler/transforms/lint/noDuplicateGroupNamesInRegularExpressions.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('disallow duplicate group names in regular expression', async t => {
+ t.snapshot(
+ await testLint(
+ `/(?<month>[0-9])-(?<year>[0-9])-(?<month>[0-9])-(?<year>[0-9])-(?<day>[0-9])-([0-9])/`,
+ ),
+ );
+});

ADD
packages/@romejs/js-compiler/transforms/lint/noDuplicateGroupNamesInRegularExpressions.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 {AnyNode} from '@romejs/js-ast';
+import {Path} from '@romejs/js-compiler';
+
+export default {
+ name: 'noDuplicateGroupNamesInRegularExpressions',
+ enter(path: Path): AnyNode {
+ const {context, node} = path;
+
+ if (node.type === 'RegExpSubExpression') {
+ const uniqueGroupNames = new Set();
+
+ for (const regExpBodyItem of node.body) {
+ if (regExpBodyItem.type === 'RegExpGroupCapture') {
+ const group = regExpBodyItem;
+ const groupName = group.name;
+ if (groupName !== undefined) {
+ if (uniqueGroupNames.has(groupName)) {
+ context.addNodeDiagnostic(group, {
+ category: 'lint/noDuplicateGroupNamesInRegularExpressions',
+ message: `Duplicate group name <emphasis>${groupName}</emphasis> in regular expression`,
+ });
+ }
+ uniqueGroupNames.add(groupName);
+ }
+ }
+ }
+ }
+
+ return node;
+ },
+};

add noDuplicateGroupNamesInRegularExpressions lint rule