Skip to content

Commit afa046f

Browse files
fix: read a named group and an i rule's case in the extension probe
A named capture group's body was skipped along with the lookarounds, and the probe compared a lower-case extension against the source of a rule that may spell it in any case, so `/[\\/]src[\\/].*\.(?<ext>css|scss)$/` and `/[\\/]src[\\/].*\.(CSS|SCSS)$/i` stayed undetected.
1 parent 7d12c50 commit afa046f

3 files changed

Lines changed: 46 additions & 21 deletions

File tree

‎lib/rules/RuleSetCompiler.js‎

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,21 @@ const splitAlternatives = (body) => {
708708
};
709709

710710
/**
711-
* Rewrites the first alternation group, word-character class or optional atom
711+
* The part of a group body the match spells: the body itself for a capturing
712+
* group, what follows the `?:` or `?<name>` prefix for the others, and nothing
713+
* for a lookaround, which says what surrounds the match rather than spelling it.
714+
* @param {string} body group body without the enclosing parentheses
715+
* @returns {string | undefined} the spelled part, or `undefined` for a lookaround
716+
*/
717+
const groupContent = (body) => {
718+
if (!body.startsWith("?")) return body;
719+
if (body.startsWith("?:")) return body.slice(2);
720+
const named = /^\?<[^=!][^>]*>/.exec(body);
721+
return named ? body.slice(named[0].length) : undefined;
722+
};
723+
724+
/**
725+
* Rewrites the first group, word-character class or optional atom
712726
* in the source into the spellings it can match, leaving the rest as written.
713727
* @param {string} source regexp source
714728
* @returns {string[] | undefined} the rewritten sources, or `undefined` when the source holds no such construct
@@ -717,7 +731,6 @@ const expandFirstConstruct = (source) => {
717731
for (let i = 0; i < source.length; i++) {
718732
const char = source[i];
719733
let end = i + 1;
720-
let resumeInside = false;
721734
/** @type {string[] | undefined} */
722735
let spellings;
723736
if (char === "\\") {
@@ -732,15 +745,8 @@ const expandFirstConstruct = (source) => {
732745
} else if (char === "(") {
733746
const close = findGroupEnd(source, i);
734747
end = close + 1;
735-
const body = source.slice(i + 1, close);
736-
// a lookaround says what must surround the match, not what it spells
737-
if (!body.startsWith("?") || body.startsWith("?:")) {
738-
const alternatives = splitAlternatives(
739-
body.startsWith("?:") ? body.slice(2) : body
740-
);
741-
if (alternatives.length > 1) spellings = alternatives;
742-
else resumeInside = true;
743-
}
748+
const content = groupContent(source.slice(i + 1, close));
749+
if (content !== undefined) spellings = splitAlternatives(content);
744750
}
745751
// `?` makes the atom optional, so its absence is a spelling as well
746752
if (source[end] === "?") {
@@ -752,7 +758,7 @@ const expandFirstConstruct = (source) => {
752758
(spelling) => source.slice(0, i) + spelling + source.slice(end)
753759
);
754760
}
755-
if (!resumeInside) i = end - 1;
761+
i = end - 1;
756762
}
757763
};
758764

@@ -855,11 +861,11 @@ const globReferencesExtension = (glob, extension) => {
855861
*/
856862
const conditionReferencesExtension = (condition, extProbe) => {
857863
if (condition instanceof RegExp) {
858-
return someSpellingContains(
859-
condition.source,
860-
extProbe,
861-
expandFirstConstruct
862-
);
864+
// an `/i` rule spells the extension in any case; the probe is lower case
865+
const source = condition.ignoreCase
866+
? condition.source.toLowerCase()
867+
: condition.source;
868+
return someSpellingContains(source, extProbe, expandFirstConstruct);
863869
}
864870
if (Array.isArray(condition)) {
865871
return condition.some((c) => conditionReferencesExtension(c, extProbe));

‎test/RuleSetCompiler.unittest.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,27 @@ describe("RuleSetCompiler.hasRuleForResource", () => {
9494
);
9595
});
9696

97+
it("reads a capturing, non-capturing or named group the same way", () => {
98+
expect(has([{ test: /[\\/]src[\\/].*\.(css)$/, use: ["x"] }])).toBe(true);
99+
expect(
100+
has([{ test: /[\\/]src[\\/].*\.(?<ext>css|scss)$/, use: ["x"] }])
101+
).toBe(true);
102+
expect(
103+
has([{ test: /[\\/]src[\\/].*\.(?<ext>scss|sass)$/, use: ["x"] }])
104+
).toBe(false);
105+
});
106+
107+
it("spells the extension in the case an `i` rule accepts", () => {
108+
expect(has([{ test: /[\\/]src[\\/].*\.(CSS|SCSS)$/i, use: ["x"] }])).toBe(
109+
true
110+
);
111+
expect(has([{ test: /[\\/]SRC[\\/].*\.Css$/i, use: ["x"] }])).toBe(true);
112+
// without `i` the rule really does not match `.css`
113+
expect(has([{ test: /[\\/]src[\\/].*\.(CSS|SCSS)$/, use: ["x"] }])).toBe(
114+
false
115+
);
116+
});
117+
97118
it("detects an extension spelled behind an optional atom", () => {
98119
expect(has([{ test: /[\\/]src[\\/].*\.s?css$/, use: ["x"] }])).toBe(true);
99120
expect(has([{ test: /[\\/]src[\\/].*\.s?ass$/, use: ["x"] }])).toBe(false);

‎test/configCases/typescript/experiments-auto-and-scoped-ts-loader/webpack.config.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
"use strict";
22

3-
// `experiments.typescript` stays at its "auto" default and the ts-loader rule is
4-
// path-scoped with its extensions in an alternation — the sample path the
5-
// detection probes with doesn't match it, so only reading the extension out of
6-
// the alternation keeps the built-in support off. Left on, it rejects `.tsx`.
3+
// The ts-loader rule is path-scoped, so the detection probe has to read `.ts`
4+
// out of its alternation — left on, the built-in support rejects `.tsx`.
75

86
/** @type {import("../../../../").Configuration} */
97
module.exports = {

0 commit comments

Comments
 (0)