improved strategy
MODIFY
packages/@romejs/js-compiler/transforms/lint/noDuplicateGroupNamesInRegularExpressions.test.md
packages/@romejs/js-compiler/transforms/lint/noDuplicateGroupNamesInRegularExpressions.test.md
@@ -17,23 +17,6 @@ Object {
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
- }
- advice: Array [
- log {
- category: 'info'
- message: 'Originally defined here'
- }
- frame {
- filename: 'unknown'
end: Object {
column: 16
index: 16
@@ -44,42 +27,34 @@ Object {
index: 1
line: 1
}
+ advice: Array [
+ log {
+ category: 'info'
+ message: 'Defined again here'
}
- ]
- }
- Object {
- category: 'lint/noDuplicateGroupNamesInRegularExpressions'
+ frame {
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
+ column: 47
+ index: 47
line: 1
}
start: Object {
- column: 48
- index: 48
+ column: 32
+ index: 32
line: 1
}
- advice: Array [
- log {
- category: 'info'
- message: 'Originally defined here'
}
frame {
filename: 'unknown'
end: Object {
- column: 31
- index: 31
+ column: 99
+ index: 100
line: 1
}
start: Object {
- column: 17
- index: 17
+ column: 85
+ index: 85
line: 1
}
}
@@ -89,35 +64,35 @@ Object {
category: 'lint/noDuplicateGroupNamesInRegularExpressions'
filename: 'unknown'
language: 'js'
- message: 'Duplicate group name <emphasis>month</emphasis> in regular expression'
+ message: 'Duplicate group name <emphasis>year</emphasis> in regular expression'
mtime: undefined
sourceType: 'module'
origins: Array [Object {category: 'lint'}]
end: Object {
- column: 99
- index: 100
+ column: 31
+ index: 31
line: 1
}
start: Object {
- column: 85
- index: 85
+ column: 17
+ index: 17
line: 1
}
advice: Array [
log {
category: 'info'
- message: 'Originally defined here'
+ message: 'Defined again here'
}
frame {
filename: 'unknown'
end: Object {
- column: 47
- index: 47
+ column: 62
+ index: 62
line: 1
}
start: Object {
- column: 32
- index: 32
+ column: 48
+ index: 48
line: 1
}
}

MODIFY
packages/@romejs/js-compiler/transforms/lint/noDuplicateGroupNamesInRegularExpressions.ts
packages/@romejs/js-compiler/transforms/lint/noDuplicateGroupNamesInRegularExpressions.ts
* LICENSE file in the root directory of this source tree.
*/
-import {AnyNode} from '@romejs/js-ast';
+import {AnyNode, RegExpGroupCapture} from '@romejs/js-ast';
import {Path} from '@romejs/js-compiler';
-import {SourceLocation} from '@romejs/parser-core';
import {PartialDiagnosticAdvice} from '@romejs/diagnostics';
export default {
@@ -16,39 +15,61 @@ export default {
const {context, node} = path;
if (node.type === 'RegExpSubExpression') {
- const uniqueGroupNames = new Map<string, undefined | SourceLocation>();
+ const groupUsage = new Map<string, Array<RegExpGroupCapture>>();
- for (const regExpBodyItem of node.body) {
- if (regExpBodyItem.type === 'RegExpGroupCapture') {
- const advice: PartialDiagnosticAdvice = [];
- const group = regExpBodyItem;
- const groupName = group.name;
+ for (const bodyItem of node.body) {
+ if (bodyItem.type === 'RegExpGroupCapture') {
+ const groupName = bodyItem.name;
if (groupName !== undefined) {
- const originalLoc = uniqueGroupNames.get(groupName);
+ let usages = groupUsage.get(groupName);
- if (originalLoc !== undefined) {
- advice.push({
- type: 'log',
- category: 'info',
- message: 'Originally defined here',
- });
+ if (usages === undefined) {
+ usages = [];
+ groupUsage.set(groupName, usages);
+ }
+ usages.push(bodyItem);
+ }
+ }
+ }
+
+ for (const [name, usages] of groupUsage) {
+ if (usages.length === 1) {
+ continue;
+ }
+
+ const firstUsage = usages[0];
- advice.push({
+ const duplicateAdvice: PartialDiagnosticAdvice = usages
+ .slice(1)
+ .map(node => {
+ if (node.loc === undefined) {
+ return {
+ type: 'log',
+ category: 'warn',
+ message: 'Unable to find location',
+ };
+ } else {
+ return {
type: 'frame',
- ...originalLoc,
+ ...node.loc,
+ };
+ }
});
- context.addNodeDiagnostic(group, {
+ context.addNodeDiagnostic(firstUsage, {
category: 'lint/noDuplicateGroupNamesInRegularExpressions',
- message: `Duplicate group name <emphasis>${groupName}</emphasis> in regular expression`,
- advice,
+ message: `Duplicate group name <emphasis>${name}</emphasis> in regular expression`,
+ advice: [
+ {
+ type: 'log',
+ category: 'info',
+ message: 'Defined again here',
+ },
+ ...duplicateAdvice,
+ ],
});
}
- uniqueGroupNames.set(groupName, group.loc);
- }
- }
- }
}
return node;
},

improved strategy