fix: modify/remove some inefficient regexes (#1916)

* fix: modify/remove some inefficient regexes
This commit is contained in:
Michael Rawlings 2023-03-17 13:34:06 -04:00 committed by GitHub
parent 43b5da5a7d
commit ac1d5062a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 33 additions and 106 deletions

View File

@ -0,0 +1,7 @@
---
"@marko/compiler": patch
"marko": patch
"@marko/translator-default": patch
---
fix: modify/remove some inefficient regexes

View File

@ -3,10 +3,7 @@
const nodePath = require("path");
const taglibConfig = require("../config");
const jsonFileReader = require("./json-file-reader");
const tagDefFromCode = require("./tag-def-from-code");
const loaders = require("./loaders");
const fsReadOptions = { encoding: "utf8" };
const extend = require("raptor-util/extend");
const types = require("./types");
const tagFileTypes = [
@ -188,19 +185,6 @@ module.exports = function scanTagsDir(
}
}
}
if (!hasTagJson && (tagDef.renderer || tagDef.template)) {
let templateCode = String(
taglibConfig.fs.readFileSync(
tagDef.renderer || tagDef.template,
fsReadOptions
)
);
let extractedTagDef = tagDefFromCode.extractTagDef(templateCode);
if (extractedTagDef) {
extend(tagDef, extractedTagDef);
}
}
}
let tagDependencyChain;

View File

@ -1,58 +0,0 @@
// Rather than using a full-blown JavaScript parser, we are going to use a few regular expressions
// to tokenize the code and find what we are interested in
var tagStartRegExp = /(^\s*(?:(?:exports.(?:tag|TAG))|(?:TAG))\s*=\s*)\{/m;
// Tokens: "<string>", '<string>', /*<some comment*/, //<single line comment>, {, }, ;
var tokensRegExp =
/"(?:[^"]|\\")*"|'(?:[^'])|(\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/)|(\/\/.*)|[{};]/g;
function extractTagDef(code) {
var startMatches = tagStartRegExp.exec(code);
var tagDefStart;
var tagDefEnd;
if (startMatches) {
tagDefStart = startMatches.index + startMatches[1].length;
var nextTokenMatches;
tokensRegExp.lastIndex = tagDefStart;
var depth = 0;
while ((nextTokenMatches = tokensRegExp.exec(code))) {
if (nextTokenMatches[0] === "{") {
depth++;
continue;
} else if (nextTokenMatches[0] === "}") {
if (--depth === 0) {
tagDefEnd = tokensRegExp.lastIndex;
break;
}
} else if (nextTokenMatches[0] === ";") {
tagDefEnd = nextTokenMatches.index;
break;
}
}
if (tagDefStart != null && tagDefEnd != null) {
var jsTagDef = code.substring(tagDefStart, tagDefEnd);
var tagDefObject;
try {
// Try parsing it as JSON
tagDefObject = JSON.parse(jsTagDef);
} catch (e) {
// Try parsing it as JavaScript
try {
tagDefObject = eval("(" + jsTagDef + ")");
} catch (e) {
tagDefObject = {};
}
}
return tagDefObject;
}
} else {
return null;
}
}
exports.extractTagDef = extractTagDef;

View File

@ -36,7 +36,7 @@ function State(root, stream, writer, events) {
}
function escapeEndingComment(text) {
return text.replace(/-->/g, "--&gt;");
return text.replace(/(--!?)>/g, "$1&gt;");
}
function AsyncStream(global, writer, parentOut) {

View File

@ -0,0 +1,8 @@
{
"attributes": {
"name": {
"type": "string",
"target-property": "NAME"
}
}
}

View File

@ -1,13 +1,3 @@
<!--
TAG = {
"attributes": {
"name": {
"type": "string",
"target-property": "NAME"
}
}
}
-->
---
scanned-d: Hello ${input.NAME}
---

View File

@ -0,0 +1,8 @@
{
"attributes": {
"name": {
"type": "string",
"target-property": "NAME"
}
}
}

View File

@ -1,12 +1,3 @@
exports.TAG = {
attributes: {
name: {
type: "string",
"target-property": "NAME"
}
}
};
module.exports = function render(input, out) {
out.write("scanned-e: Hello " + input.NAME);
};

View File

@ -0,0 +1,8 @@
{
"attributes": {
"name": {
"type": "string",
"target-property": "NAME"
}
}
}

View File

@ -1,14 +1,3 @@
/*
TAG = {
"attributes": {
"name": {
"type": "string",
"target-property": "NAME"
}
}
}
*/
module.exports = function render(input, out) {
out.write("scanned-f: Hello " + input.NAME);
};

View File

@ -1,7 +1,7 @@
import path from "path";
import getComponentFiles from "../../util/get-component-files";
const STYLE_REG = /^style((?:\.[^\s\\/:*?"<>|({]+)+)?\s*\{/;
const STYLE_REG = /^style((?:\.[^.\s\\/:*?"<>|({]+)+)?\s*\{/;
export default function (tag) {
const { hub, node } = tag;