From 92cfd31c0faf333c3affb2c9a7b14c4960cf156a Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 12 Dec 2020 14:47:17 -0500 Subject: Fix global declarations check (#1101) * Fix getSubstringCount not properly checking word boundaries * Remove unused global declarations --- dev/lint/global-declarations.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'dev') 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; } -- cgit v1.2.3