From e86693c682d15a29efd936c2b185e121a0c9ffda Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Fri, 29 Apr 2022 17:05:16 +1000 Subject: Stripping comments when checking between @license and @license-end example (say it's an external script): /* my awesome script */ /* @license magnet ... */ .... // @license-end --- main_background.js | 7 ++++--- pattern_utils.js | 8 ++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/main_background.js b/main_background.js index df5be8e..9f8b0d6 100644 --- a/main_background.js +++ b/main_background.js @@ -27,6 +27,7 @@ const { Storage, ListStore, hash } = require('./common/Storage'); const { ListManager } = require('./bg/ListManager'); const { ExternalLicenses } = require('./bg/ExternalLicenses'); const { licenses } = require('./license_definitions'); +const { patternUtils } = require('./pattern_utils'); console.log('main_background.js'); /** @@ -540,8 +541,8 @@ function evaluate(script, name) { return new RegExp('(?:[^\\w\\d]|^|(?:' + arith_operators + '))' + object + '(?:\\s*?(?:[\\;\\,\\.\\(\\[])\\s*?)', 'g'); } reserved_object_regex('window'); - var ml_comment = /\/\*([\s\S]+?)\*\//g; - var il_comment = /\/\/.+/gm; + const ml_comment = /\/\*([\s\S]+?)\*\//g; + const il_comment = /\/\/.+/gm; var temp = script.replace(/'.+?'+/gm, '\'string\''); temp = temp.replace(/".+?"+/gm, '"string"'); temp = temp.replace(ml_comment, ''); @@ -616,7 +617,7 @@ function license_read(scriptSrc, name, external = false) { let partsAccepted = false; function checkTriviality(s) { - if (!s.trim()) { + if (!patternUtils.removeJsComments(s).trim()) { return true; // empty, ignore it } const [trivial, message] = external ? diff --git a/pattern_utils.js b/pattern_utils.js index ef1e304..7ab143b 100644 --- a/pattern_utils.js +++ b/pattern_utils.js @@ -31,10 +31,18 @@ exports.patternUtils = { removeNonalpha: function(str) { return str.replace(/[^a-z0-9<>@]+/gi, ''); }, + removeWhitespace: function(str) { return str.replace(/\/\//gmi, '').replace(/\*/gmi, '').replace(/\s+/gmi, ''); }, + replaceTokens: function(str) { return str.replace(/<.*?>/gi, '.*?'); + }, + + removeJsComments: function(str) { + const ml_comments = /\/\*.*?(\*\/)/g; + const il_comments = /\/\/.*/gm; + return str.replace(ml_comments, '').replace(il_comments, ''); } }; -- cgit v1.2.3