Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Commit d8c7a78

Browse files
make regex-literals single line by default
1 parent 52394f6 commit d8c7a78

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

js-modules/prettify.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -497,26 +497,38 @@ var prettyPrint;
497497
fallthroughStylePatterns.push(
498498
[PR_COMMENT, /^\/\*[\s\S]*?(?:\*\/|$)/, null]);
499499
}
500-
if (options['regexLiterals']) {
500+
var regexLiterals = options['regexLiterals'];
501+
if (regexLiterals) {
502+
/**
503+
* @const
504+
*/
505+
var regexExcls = regexLiterals > 1
506+
? '' // Multiline regex literals
507+
: '\n\r';
508+
/**
509+
* @const
510+
*/
511+
var regexAny = regexExcls ? '.' : '[\\S\\s]';
501512
/**
502513
* @const
503514
*/
504515
var REGEX_LITERAL = (
505516
// A regular expression literal starts with a slash that is
506517
// not followed by * or / so that it is not confused with
507518
// comments.
508-
'/(?=[^/*])'
519+
'/(?=[^/*' + regexExcls + '])'
509520
// and then contains any number of raw characters,
510-
+ '(?:[^/\\x5B\\x5C]'
521+
+ '(?:[^/\\x5B\\x5C' + regexExcls + ']'
511522
// escape sequences (\x5C),
512-
+ '|\\x5C[\\s\\S]'
523+
+ '|\\x5C' + regexAny
513524
// or non-nesting character sets (\x5B\x5D);
514-
+ '|\\x5B(?:[^\\x5C\\x5D]|\\x5C[\\s\\S])*(?:\\x5D|$))+'
525+
+ '|\\x5B(?:[^\\x5C\\x5D' + regexExcls + ']'
526+
+ '|\\x5C' + regexAny + ')*(?:\\x5D|$))+'
515527
// finally closed by a /.
516528
+ '/');
517529
fallthroughStylePatterns.push(
518530
['lang-regex',
519-
new RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')')
531+
RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')')
520532
]);
521533
}
522534

@@ -728,7 +740,7 @@ var prettyPrint;
728740
'keywords': PERL_KEYWORDS,
729741
'hashComments': true,
730742
'multiLineStrings': true,
731-
'regexLiterals': true
743+
'regexLiterals': 2 // multiline regex literals
732744
}), ['perl', 'pl', 'pm']);
733745
registerLangHandler(sourceDecorator({
734746
'keywords': RUBY_KEYWORDS,

src/prettify.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -852,26 +852,38 @@ var prettyPrint;
852852
fallthroughStylePatterns.push(
853853
[PR_COMMENT, /^\/\*[\s\S]*?(?:\*\/|$)/, null]);
854854
}
855-
if (options['regexLiterals']) {
855+
var regexLiterals = options['regexLiterals'];
856+
if (regexLiterals) {
857+
/**
858+
* @const
859+
*/
860+
var regexExcls = regexLiterals > 1
861+
? '' // Multiline regex literals
862+
: '\n\r';
863+
/**
864+
* @const
865+
*/
866+
var regexAny = regexExcls ? '.' : '[\\S\\s]';
856867
/**
857868
* @const
858869
*/
859870
var REGEX_LITERAL = (
860871
// A regular expression literal starts with a slash that is
861872
// not followed by * or / so that it is not confused with
862873
// comments.
863-
'/(?=[^/*])'
874+
'/(?=[^/*' + regexExcls + '])'
864875
// and then contains any number of raw characters,
865-
+ '(?:[^/\\x5B\\x5C]'
876+
+ '(?:[^/\\x5B\\x5C' + regexExcls + ']'
866877
// escape sequences (\x5C),
867-
+ '|\\x5C[\\s\\S]'
878+
+ '|\\x5C' + regexAny
868879
// or non-nesting character sets (\x5B\x5D);
869-
+ '|\\x5B(?:[^\\x5C\\x5D]|\\x5C[\\s\\S])*(?:\\x5D|$))+'
880+
+ '|\\x5B(?:[^\\x5C\\x5D' + regexExcls + ']'
881+
+ '|\\x5C' + regexAny + ')*(?:\\x5D|$))+'
870882
// finally closed by a /.
871883
+ '/');
872884
fallthroughStylePatterns.push(
873885
['lang-regex',
874-
new RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')')
886+
RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')')
875887
]);
876888
}
877889

@@ -1336,7 +1348,7 @@ var prettyPrint;
13361348
'keywords': PERL_KEYWORDS,
13371349
'hashComments': true,
13381350
'multiLineStrings': true,
1339-
'regexLiterals': true
1351+
'regexLiterals': 2 // multiline regex literals
13401352
}), ['perl', 'pl', 'pm']);
13411353
registerLangHandler(sourceDecorator({
13421354
'keywords': RUBY_KEYWORDS,

src/run_prettify.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,26 +1074,38 @@ var IN_GLOBAL_SCOPE = false;
10741074
fallthroughStylePatterns.push(
10751075
[PR_COMMENT, /^\/\*[\s\S]*?(?:\*\/|$)/, null]);
10761076
}
1077-
if (options['regexLiterals']) {
1077+
var regexLiterals = options['regexLiterals'];
1078+
if (regexLiterals) {
1079+
/**
1080+
* @const
1081+
*/
1082+
var regexExcls = regexLiterals > 1
1083+
? '' // Multiline regex literals
1084+
: '\n\r';
1085+
/**
1086+
* @const
1087+
*/
1088+
var regexAny = regexExcls ? '.' : '[\\S\\s]';
10781089
/**
10791090
* @const
10801091
*/
10811092
var REGEX_LITERAL = (
10821093
// A regular expression literal starts with a slash that is
10831094
// not followed by * or / so that it is not confused with
10841095
// comments.
1085-
'/(?=[^/*])'
1096+
'/(?=[^/*' + regexExcls + '])'
10861097
// and then contains any number of raw characters,
1087-
+ '(?:[^/\\x5B\\x5C]'
1098+
+ '(?:[^/\\x5B\\x5C' + regexExcls + ']'
10881099
// escape sequences (\x5C),
1089-
+ '|\\x5C[\\s\\S]'
1100+
+ '|\\x5C' + regexAny
10901101
// or non-nesting character sets (\x5B\x5D);
1091-
+ '|\\x5B(?:[^\\x5C\\x5D]|\\x5C[\\s\\S])*(?:\\x5D|$))+'
1102+
+ '|\\x5B(?:[^\\x5C\\x5D' + regexExcls + ']'
1103+
+ '|\\x5C' + regexAny + ')*(?:\\x5D|$))+'
10921104
// finally closed by a /.
10931105
+ '/');
10941106
fallthroughStylePatterns.push(
10951107
['lang-regex',
1096-
new RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')')
1108+
RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')')
10971109
]);
10981110
}
10991111

@@ -1558,7 +1570,7 @@ var IN_GLOBAL_SCOPE = false;
15581570
'keywords': PERL_KEYWORDS,
15591571
'hashComments': true,
15601572
'multiLineStrings': true,
1561-
'regexLiterals': true
1573+
'regexLiterals': 2 // multiline regex literals
15621574
}), ['perl', 'pl', 'pm']);
15631575
registerLangHandler(sourceDecorator({
15641576
'keywords': RUBY_KEYWORDS,

0 commit comments

Comments
 (0)