Last active
December 12, 2023 06:33
-
-
Save PatrickJS/5411dcbe71e35ad1628262c2a938592f to your computer and use it in GitHub Desktop.
normalize-function args from extracted code
This file contains hidden or 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
| let funcCode = '(n) => { "ArrowDown" === n.key && n.preventDefault() }'; | |
| let { needsNormalization, normalizedCode } = normalizeFunction(funcCode, 'e'); | |
| console.log(needsNormalization); | |
| console.log(normalizedCode); |
This file contains hidden or 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
| const { parse, traverse, generate } = require("@babel/core"); | |
| function normalizeFunction(funcCode) { | |
| const parsed = parse(funcCode); | |
| let needsNormalization = false; | |
| let originalArgNames = {}; | |
| traverse(parsed, { | |
| FunctionExpression(path) { | |
| const params = path.node.params; | |
| // If the function has parameters | |
| if (params.length > 0) { | |
| needsNormalization = true; | |
| originalArgNames.first = params[0].name; | |
| params[0].name = 'e'; | |
| // If the function has a second parameter | |
| if (params[1]) { | |
| originalArgNames.second = params[1].name; | |
| params[1].name = 't'; | |
| } | |
| } | |
| } | |
| }); | |
| if (needsNormalization) { | |
| // replace all 'originalArgNames.first' variable references with 'e' | |
| // and 'originalArgNames.second' variable references with 't' | |
| traverse(parsed, { | |
| Identifier(path) { | |
| if (path.node.name === originalArgNames.first) { | |
| path.node.name = 'e'; | |
| } else if (path.node.name === originalArgNames.second) { | |
| path.node.name = 't'; | |
| } | |
| } | |
| }); | |
| } | |
| return { | |
| needsNormalization, | |
| normalizedCode: needsNormalization ? generate(parsed).code : funcCode | |
| }; | |
| } |
This file contains hidden or 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
| const { parse, traverse, generate } = require("@babel/core"); | |
| const Terser = require("terser"); | |
| function normalizeFunction(funcCode) { | |
| const parsed = parse(funcCode); | |
| let needsNormalization = false; | |
| let originalArgNames = {}; | |
| traverse(parsed, { | |
| FunctionExpression(path) { | |
| const params = path.node.params; | |
| // If the function has parameters | |
| if (params.length > 0) { | |
| needsNormalization = true; | |
| originalArgNames.first = params[0].name; | |
| params[0].name = 'e'; | |
| // If function has a second parameter | |
| if (params[1]) { | |
| originalArgNames.second = params[1].name; | |
| params[1].name = 't'; | |
| } | |
| } | |
| } | |
| }); | |
| if (needsNormalization) { | |
| // replace all 'originalArgNames.first' variable references with 'e' | |
| // and 'originalArgNames.second' variable references with 't' | |
| traverse(parsed, { | |
| Identifier(path) { | |
| if (path.node.name === originalArgNames.first) { | |
| path.node.name = 'e'; | |
| } else if (path.node.name === originalArgNames.second) { | |
| path.node.name = 't'; | |
| } | |
| } | |
| }); | |
| } | |
| const normalizedCode = needsNormalization ? generate(parsed).code : funcCode; | |
| // Minify with Terser | |
| const minifiedCode = Terser.minify(normalizedCode).code; | |
| return { | |
| needsNormalization, | |
| normalizedCode: minifiedCode | |
| }; | |
| } |
This file contains hidden or 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
| function shouldNormalize(funcCode) { | |
| const funcRegex = /^\s*[(]\s*([^)\s]+)/; | |
| const match = funcCode.match(funcRegex); | |
| return match ? match[1] !== 'e' : false; | |
| } | |
| let funcCode = '(n) => { "ArrowDown" === n.key && n.preventDefault() }'; | |
| if (shouldNormalize(funcCode)) { | |
| let { needsNormalization, normalizedCode } = normalizeFunction(funcCode, 'e'); | |
| console.log(needsNormalization); | |
| console.log(normalizedCode); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
e=>console.log(e)->e=>console.log(e);(e, t)=>console.log(e)->(e,t)=>console.log(e);(_, t)=>console.log(t)->(e,t)=>console.log(t);()=>console.log('hi')->()=>console.log("hi");evt=>console.log(evt)->e=>console.log(e);