mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
fix: modify/remove some inefficient regexes (#1916)
* fix: modify/remove some inefficient regexes
This commit is contained in:
parent
43b5da5a7d
commit
ac1d5062a7
7
.changeset/wet-beans-hunt.md
Normal file
7
.changeset/wet-beans-hunt.md
Normal file
@ -0,0 +1,7 @@
|
||||
---
|
||||
"@marko/compiler": patch
|
||||
"marko": patch
|
||||
"@marko/translator-default": patch
|
||||
---
|
||||
|
||||
fix: modify/remove some inefficient regexes
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
@ -36,7 +36,7 @@ function State(root, stream, writer, events) {
|
||||
}
|
||||
|
||||
function escapeEndingComment(text) {
|
||||
return text.replace(/-->/g, "-->");
|
||||
return text.replace(/(--!?)>/g, "$1>");
|
||||
}
|
||||
|
||||
function AsyncStream(global, writer, parentOut) {
|
||||
|
||||
@ -0,0 +1,8 @@
|
||||
{
|
||||
"attributes": {
|
||||
"name": {
|
||||
"type": "string",
|
||||
"target-property": "NAME"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,13 +1,3 @@
|
||||
<!--
|
||||
TAG = {
|
||||
"attributes": {
|
||||
"name": {
|
||||
"type": "string",
|
||||
"target-property": "NAME"
|
||||
}
|
||||
}
|
||||
}
|
||||
-->
|
||||
---
|
||||
scanned-d: Hello ${input.NAME}
|
||||
---
|
||||
@ -0,0 +1,8 @@
|
||||
{
|
||||
"attributes": {
|
||||
"name": {
|
||||
"type": "string",
|
||||
"target-property": "NAME"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
};
|
||||
|
||||
@ -0,0 +1,8 @@
|
||||
{
|
||||
"attributes": {
|
||||
"name": {
|
||||
"type": "string",
|
||||
"target-property": "NAME"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
};
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user