Skip to content

Commit c89a149

Browse files
committed
Fix crash occurring due to unconverted 'x' flag
The PCRE `(?x)` inline flag did not have a conversion process, causing a crash. Fixes #16.
1 parent 4c0c1ba commit c89a149

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## Next
4+
- Fixed a crash occurring when parsing heuristics for `.txt` files ([#16](https://github.com/Nixinova/LinguistJS/issues/16)).
5+
36
## 2.5.2
47
*2022-06-28*
58
- Fixed file extensions with multiple delimiters not being prioritised over basic extensions.

src/helpers/convert-pcre.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,25 @@ export default function pcre(regex: string): RegExp {
1010
replace(match, '');
1111
[...flags].forEach(flag => finalFlags.add(flag));
1212
}
13-
// Remove invalid syntax
13+
// Remove PCRE-only syntax
1414
replace(/([*+]){2}/g, '$1');
1515
replace(/\(\?>/g, '(?:');
1616
// Remove start/end-of-file markers
1717
if (/\\[AZ]/.test(finalRegex)) {
18-
replace(/\\A/g, '^').replace(/\\Z/g, '$');
18+
replace(/\\A/g, '^');
19+
replace(/\\Z/g, '$');
1920
finalFlags.delete('m');
2021
}
21-
else finalFlags.add('m');
22+
else {
23+
finalFlags.add('m');
24+
}
25+
// Reformat free-spacing mode
26+
if (finalFlags.has('x')) {
27+
finalFlags.delete('x');
28+
replace(/#.+/g, '');
29+
replace(/^\s+|\s+$|\n/gm, '');
30+
replace(/\s+/g, ' ');
31+
}
2232
// Return final regex
2333
return RegExp(finalRegex, [...finalFlags].join(''));
2434
}

0 commit comments

Comments
 (0)