-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from wpdas/feature/core-improvement-beta-24
Feature: Code Improvement for version Beta 24
- Loading branch information
Showing
9 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const regexObjects = require("../parsers/regex-parser/regexObjects"); | ||
|
||
/** | ||
* Troca as referencias de expressoes regulares com elas sendo devidamente escapadas. Isso é necessário | ||
* pois todas as expressoes encontradas sao extraídas para depois serem realocadas de forma formatada/escapada | ||
* corretamente no código final. | ||
* | ||
* @param {*} bundleContent | ||
* @returns | ||
*/ | ||
const injectFoundRegExps = (bundleContent) => { | ||
const expressions = regexObjects.getExpressions(); | ||
const exps = Object.keys(expressions); | ||
exps.forEach((expressionKey) => { | ||
bundleContent = bundleContent.replaceAll( | ||
`"${expressionKey}"`, | ||
expressions[expressionKey], | ||
); | ||
}); | ||
|
||
return bundleContent; | ||
}; | ||
|
||
module.exports = injectFoundRegExps; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const { parse } = require("@babel/parser"); | ||
const generate = require("@babel/generator").default; | ||
const { default: traverse } = require("@babel/traverse"); | ||
const { create_new_name } = require("../../utils"); | ||
|
||
function escapeRegexContent(regex) { | ||
// Escapa todos os backslashes primeiro para evitar duplicação | ||
let escapedRegex = regex.replace(/\\/g, "\\\\"); | ||
|
||
// Agora escapa todos os backticks | ||
escapedRegex = escapedRegex.replace(/`/g, "\\`"); | ||
|
||
return escapedRegex; | ||
} | ||
|
||
function replaceRegexWithReferences(code) { | ||
const regexExpressions = {}; | ||
let ast = parse(code, { | ||
sourceType: "module", | ||
plugins: ["jsx", "typescript"], | ||
}); | ||
|
||
traverse(ast, { | ||
RegExpLiteral(path) { | ||
const { pattern, flags } = path.node; | ||
const regex = new RegExp(pattern, flags); | ||
const referenceId = `:::${create_new_name()}:::`; | ||
regexExpressions[referenceId] = escapeRegexContent(regex.toString()); | ||
|
||
// Replace the RegExpLiteral with the reference ID | ||
path.replaceWith({ | ||
type: "StringLiteral", | ||
value: referenceId, | ||
}); | ||
}, | ||
}); | ||
|
||
const output = generate(ast, { | ||
retainLines: true, | ||
concise: false, | ||
comments: true, | ||
}); | ||
|
||
return { | ||
code: output.code, | ||
expressions: regexExpressions, | ||
hasExpressions: Object.keys(regexExpressions).length > 0, | ||
}; | ||
} | ||
|
||
module.exports = replaceRegexWithReferences; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
let _regexObjects = {}; | ||
|
||
/** | ||
* INFO: Houve a tentativa de guardar as referencias das expressoes no estado global, porém a VM não suporta | ||
* passar expressoes regulares como parametros para os Widgets abaixo | ||
*/ | ||
// function convertRegexStringsToLiterals(obj) { | ||
// const entries = Object.keys(obj); | ||
// let finalObjects = ""; | ||
|
||
// entries.forEach((key) => { | ||
// const currentEntrieValue = obj[key]; | ||
// let currentRegexString = `${key}: ${currentEntrieValue}`; | ||
// finalObjects += `${currentRegexString},\n`; | ||
// }); | ||
|
||
// return finalObjects; | ||
// } | ||
|
||
const addExpressions = (expressions) => | ||
(_regexObjects = { ..._regexObjects, ...expressions }); | ||
const getExpressions = () => _regexObjects; | ||
// const getStringExpressions = () => convertRegexStringsToLiterals(_regexObjects); | ||
|
||
module.exports = { | ||
addExpressions, | ||
getExpressions, | ||
// getStringExpressions, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters