diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-12-12 14:47:17 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-12 14:47:17 -0500 |
commit | 92cfd31c0faf333c3affb2c9a7b14c4960cf156a (patch) | |
tree | a8280f30a5f695d29fbc53c91ef9e40a1961a4f3 /dev/lint/global-declarations.js | |
parent | fe16793779cc6ee2ecb1785e58aae10569692bc2 (diff) |
Fix global declarations check (#1101)
* Fix getSubstringCount not properly checking word boundaries
* Remove unused global declarations
Diffstat (limited to 'dev/lint/global-declarations.js')
-rw-r--r-- | dev/lint/global-declarations.js | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/dev/lint/global-declarations.js b/dev/lint/global-declarations.js index 5448df85..57097c03 100644 --- a/dev/lint/global-declarations.js +++ b/dev/lint/global-declarations.js @@ -22,6 +22,10 @@ const assert = require('assert'); const {getAllFiles} = require('../util'); +function escapeRegExp(string) { + return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); +} + function countOccurences(string, pattern) { return (string.match(pattern) || []).length; } @@ -38,13 +42,12 @@ function getNewline(string) { } function getSubstringCount(string, substring) { - let start = 0; let count = 0; + const pattern = new RegExp(`\\b${escapeRegExp(substring)}\\b`, 'g'); while (true) { - const pos = string.indexOf(substring, start); - if (pos < 0) { break; } + const match = pattern.exec(string); + if (match === null) { break; } ++count; - start = pos + substring.length; } return count; } |