Skip to content

Commit

Permalink
Code tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
slevithan committed Jul 22, 2024
1 parent da122a4 commit 576358d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
8 changes: 4 additions & 4 deletions spec/flag-x-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ describe('flag x', () => {
it('should not remove (?:) when followed by a quantifier', () => {
expect('').toMatch(regex`(?:)?`);
expect('').toMatch(regex`^(?:)?$`);
expect(':').toMatch(regex`^((?:)?:)$`);
expect('=').toMatch(regex`^((?:)?=)$`);
expect('!').toMatch(regex`^((?:)?!)$`);
expect('DEFINE').toMatch(regex`^((?:)?(DEFINE))$`);
expect(':').toMatch(regex({__flagN: false})`^((?:)?:)$`);
expect('=').toMatch(regex({__flagN: false})`^((?:)?=)$`);
expect('!').toMatch(regex({__flagN: false})`^((?:)?!)$`);
expect('DEFINE').toMatch(regex({__flagN: false})`^((?:)?(DEFINE))$`);
});

it('should maintain the error status of invalid syntax', () => {
Expand Down
14 changes: 5 additions & 9 deletions src/subroutines.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {capturingDelim, countCaptures, namedCapturingDelim} from './utils.js';
@returns {string}
*/
export function subroutinesPostprocessor(expression) {
const namedGroups = getNamedCapturingGroups(expression, true);
const namedGroups = getNamedCapturingGroups(expression, {includeContents: true});
return processDefinitionGroup(
processSubroutines(expression, namedGroups),
namedGroups
Expand All @@ -28,7 +28,7 @@ ${subroutinePattern}
@typedef {
Map<string, {
isUnique: boolean;
contents?: string;
contents: string | null;
}>} NamedCapturingGroupsMap
*/

Expand Down Expand Up @@ -285,10 +285,10 @@ function spliceStr(str, pos, oldValue, newValue) {

/**
@param {string} expression
@param {boolean} [includeContents] Leave off if unneeded, for perf
@param {Object<includeContents?: boolean>} [options]
@returns {NamedCapturingGroupsMap}
*/
function getNamedCapturingGroups(expression, includeContents) {
function getNamedCapturingGroups(expression, {includeContents} = {}) {
const namedGroups = new Map();
forEachUnescaped(
expression,
Expand All @@ -301,11 +301,7 @@ function getNamedCapturingGroups(expression, includeContents) {
} else {
namedGroups.set(captureName, {
isUnique: true,
...(
includeContents ? {
contents: getGroupContents(expression, index + m.length),
} : null
),
contents: includeContents ? getGroupContents(expression, index + m.length) : null,
});
}
},
Expand Down

0 comments on commit 576358d

Please sign in to comment.