@digitak/grubber
Version:
Parse code files and patch it without having to use an AST
66 lines (65 loc) • 2.17 kB
JavaScript
export default class default_1 {
constructor() {
this.backtickScope = [];
this.rules = [
{
// single quote string
expression: /'.*?[^\\](?:\\\\)*'/,
},
{
// double quote string
expression: /".*?[^\\](?:\\\\)*"/,
},
{
// backtick string
startAt: /`|{|}/,
onStartMatch: (match) => {
if (match[0] === "{") {
if (this.backtickLevel)
this.currentScope++;
return false;
}
else if (match[0] === "}") {
if (this.backtickLevel) {
if (this.currentScope === 0) {
this.backtickScope.pop();
return true;
}
this.currentScope--;
}
return false;
}
return true;
},
stopAt: /[^\\](?:\\\\)*(\${|`)/,
onStopMatch: (match) => {
if (match[1] === "${")
this.backtickScope.push(0);
return true;
},
},
{
// single line comment
expression: /\/\/.*/,
},
{
// multiline comment
expression: /\/\*((?:.|\s)*?)\*\//,
},
{
// regular expression
expression: /[=,;?(\n]\s*\/[^/*].*?[^\\](?:\\\\)*\//,
},
];
}
get backtickLevel() {
return this.backtickScope.length;
}
get currentScope() {
return this.backtickScope[this.backtickScope.length - 1];
}
set currentScope(value) {
this.backtickScope[this.backtickScope.length - 1] = value;
}
}
default_1.importExpression = /\b(?:import|export)(?:\s+([^;()[\]=/'"`+\-:.]+?)\s+from)?\s+('|")(.+?)\2/g;