Skip to content

Commit bee3744

Browse files
committed
enh(autodetect) fewer false positives on variables
For languages with $ident and @Ident style variables this attempts to prevent positives for $ident$ and @Ident@ type expressions, which are likely something else entirely. - bash - perl - php - ruby
1 parent 670849b commit bee3744

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

src/languages/bash.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Website: https://www.gnu.org/software/bash/
66
Category: common
77
*/
88

9+
import * as regex from '../lib/regex.js';
10+
911
/** @type LanguageFn */
1012
export default function(hljs) {
1113
const VAR = {};
@@ -23,7 +25,7 @@ export default function(hljs) {
2325
Object.assign(VAR,{
2426
className: 'variable',
2527
variants: [
26-
{begin: /\$[\w\d#@][\w\d_]*/},
28+
{begin: regex.concat(/\$[\w\d#@][\w\d_]*/, `(?![\\w\\d])(?![$])`) },
2729
BRACED_VAR
2830
]
2931
});

src/languages/perl.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ Website: https://www.perl.org
55
Category: common
66
*/
77

8+
import * as regex from '../lib/regex.js';
9+
10+
/** @type LanguageFn */
811
export default function(hljs) {
912
var PERL_KEYWORDS = {
1013
$pattern: /[\w.]+/,
@@ -40,7 +43,12 @@ export default function(hljs) {
4043
var VAR = {
4144
variants: [
4245
{begin: /\$\d/},
43-
{begin: /[$%@](\^\w\b|#\w+(::\w+)*|\{\w+\}|\w+(::\w*)*)/},
46+
{begin: regex.concat(
47+
/[$%@](\^\w\b|#\w+(::\w+)*|\{\w+\}|\w+(::\w*)*)/,
48+
// negative look-ahead tries to avoid matching patterns that are not
49+
// Perl at all like $ident$, @ident@, etc.
50+
`(?![A-Za-z])(?![@$%])`
51+
)},
4452
{begin: /[$%@][^\s\w{]/, relevance: 0}
4553
]
4654
};

src/languages/php.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Category: common
1313
export default function(hljs) {
1414
const VARIABLE = {
1515
className: 'variable',
16-
begin: '\\$+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
16+
begin: '\\$+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' + `(?![A-Za-z0-9])(?![$])`
1717
};
1818
const PREPROCESSOR = {
1919
className: 'meta',

src/languages/ruby.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,10 @@ export default function(hljs) {
160160
},
161161
NUMBER,
162162
{
163+
// negative-look forward attemps to prevent false matches like:
164+
// @ident@ or $ident$ that might indicate this is not ruby at all
163165
className: "variable",
164-
begin: '(\\$\\W)|((\\$|@@?)(\\w+))' // variables
166+
begin: '(\\$\\W)|((\\$|@@?)(\\w+))(?=[^@$?])' + `(?![A-Za-z])(?![@$?'])`
165167
},
166168
{
167169
className: 'params',

0 commit comments

Comments
 (0)