aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <hi@ypei.me>2022-04-29 17:05:16 +1000
committerYuchen Pei <hi@ypei.me>2022-04-29 17:05:16 +1000
commite86693c682d15a29efd936c2b185e121a0c9ffda (patch)
tree6ad9272cb9fbdf38c5600ac6177da59ef9883963
parent3dafc83753fbabc8d80540e05cf1e374ae73f4fa (diff)
Stripping comments when checking between @license and @license-end
example (say it's an external script): /* my awesome script */ /* @license magnet ... */ .... // @license-end
-rw-r--r--main_background.js7
-rw-r--r--pattern_utils.js8
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, '');
}
};