From 978395e073dd1185e36beb47078e87db5cbda400 Mon Sep 17 00:00:00 2001 From: Nikyle Nguyen Date: Sun, 9 Apr 2023 03:39:08 -0700 Subject: [PATCH] fix: macro string escaping --- package-lock.json | 8 +++--- package.json | 4 +-- src/common.js | 7 ++++-- src/eval-blobs.js | 8 +++--- src/eval-snippet-injections.js | 46 ++++++++++++++-------------------- src/scan-blobs.js | 6 ++++- src/scan-scripts.js | 8 ++---- src/scan-snippets.js | 2 +- 8 files changed, 43 insertions(+), 46 deletions(-) diff --git a/package-lock.json b/package-lock.json index cb139df..b6b8911 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "code-formation", - "version": "0.7.0", + "version": "0.7.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -680,9 +680,9 @@ } }, "s-expression.js": { - "version": "0.6.2", - "resolved": "https://registry.npmjs.org/s-expression.js/-/s-expression.js-0.6.2.tgz", - "integrity": "sha512-rVfvgonS3qlHNeCBPtsXE8p4Ng/8QOYCCsOmRtvTo9YMlLTN7hf97zEFsHS51T2LvmBuCYmqR3WUvpMrnDWoAA==" + "version": "0.6.4", + "resolved": "https://registry.npmjs.org/s-expression.js/-/s-expression.js-0.6.4.tgz", + "integrity": "sha512-3HEtulxAMlwfNQW9LVGy57u5VrAooZXXoX2QyBD5BIragTIapDSTcTBWrzY0fzwfEiTFBivmQBaX+iOBqdFjzQ==" }, "safe-buffer": { "version": "5.2.1", diff --git a/package.json b/package.json index dfca597..5a312c4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "code-formation", - "version": "0.7.0", + "version": "0.7.1", "description": "context-free text manipulator using line-oriented DSL for easy embedding to existing source code", "main": "./src/index.js", "bin": "./src/index.js", @@ -41,7 +41,7 @@ "parse-pairs": "^1.1.0", "query-string": "^7.1.1", "requireg": "^0.2.2", - "s-expression.js": "^0.6.2", + "s-expression.js": "^0.6.4", "vm2": "^3.9.11", "wildcard-match": "^5.1.2", "winston": "^3.6.0" diff --git a/src/common.js b/src/common.js index 7802c18..6b3944f 100644 --- a/src/common.js +++ b/src/common.js @@ -50,7 +50,7 @@ invoke = async (snippet, context) => { // } const LANGUAGE = _.get(context, "LANGUAGE") const LINE_BREAK = _.get(context, "LINE_BREAK") - + // console.dir({template: snippet.template, LANGUAGE, LINE_BREAK}) const template = snippet.template.join(LINE_BREAK) let result = "" @@ -104,7 +104,10 @@ invoke = async (snippet, context) => { } function serializeMacro(macro) { const S = new SExpr() - return S.serialize(S.parse(macro), { rootBrackets: false }) + const ast = S.parse(macro, { includedRootParentheses: false }) + console.dir(ast) + process.exit() + return S.serialize(ast, { includingRootParentheses: false }) } async function parseParams(str) { const S = new SExpr() diff --git a/src/eval-blobs.js b/src/eval-blobs.js index 073ab77..30804fe 100644 --- a/src/eval-blobs.js +++ b/src/eval-blobs.js @@ -220,7 +220,9 @@ module.exports = async function (files, profile, log) { // let {key, value} = S.ExtractEntry(action) const key = Object.keys(action)[0] const value = action[key] - // console.dir({key, value}) + // console.dir({APPLY, action, key, value}) + // logger.info(colorize(params, {pretty: true})) + // process.exit() // TODO: check if key starts with @ to expand macro, no need for nested macro @@ -351,8 +353,8 @@ module.exports = async function (files, profile, log) { SECTION_SEPARATOR, }, // separator: SECTION_SEPARATOR, - }) - _.set(profile, outpath, destination) + }) + _.set(profile, outpath, destination) // console.log(destination) // _.set(profile, ['content', 'a'], output) diff --git a/src/eval-snippet-injections.js b/src/eval-snippet-injections.js index e764e0f..d9541cf 100644 --- a/src/eval-snippet-injections.js +++ b/src/eval-snippet-injections.js @@ -63,23 +63,25 @@ async function evalBlock( // console.log(rest) injection.params = await common.parseParams(rest) - // process.exit() + // TODO: should just be one "LINE_BREAK" instead of LINE_BREAK/LINE_BREAK - const LINE_BREAK = _.get(injection.params, ["LINE_BREAK"], common.profile.LINE_BREAK) + const LINE_BREAK = _.get( + injection.params, + ["LINE_BREAK"], + common.profile.LINE_BREAK + ) const INPUT = injection.blockContent.join(LINE_BREAK) - injection.args = _.merge({}, injection.params, { INPUT, _logger: logger, - _common: common + _common: common, // _ is available by the template library }) let snippet_name = command if (snippet_name.startsWith("@")) { injection.macroExpansion = true snippet_name = snippet_name.split("@")[1] - // console.dir(snippet_name) const snippet = _.get(common.profile, ["snippets", snippet_name]) if (_.isUndefined(snippet)) { @@ -92,10 +94,14 @@ async function evalBlock( let context = _.merge({}, params, snippet.params, injection.args) const macro = await common.invoke(snippet, context) - const expansion = common.serializeMacro(`(${macro})`) + const expansion = common.serializeMacro(macro) lines[line_number] = line.replace(openRegex, () => { - return macroExpansionCallback(common.profile.MARKER_PREFIX, label, expansion) + return macroExpansionCallback( + common.profile.MARKER_PREFIX, + label, + expansion + ) }) logger.info( `${chalk.cyan(`expand macro "@${snippet_name}":`)} ${chalk.gray( @@ -118,9 +124,7 @@ async function evalSnippetInjection(content, params, profile, log) { let hasSnippetInjection = false let LINE_BREAK = _.get(params, ["LINE_BREAK"]) - - - const CURRENT_DIR = _.get(params, ["CURRENT_DIR"]) + const CURRENT_DIR = _.get(params, ["CURRENT_DIR"]) // const OUT_DIR = _.get(profile, "OUT_DIR") // console.log(content) @@ -167,7 +171,6 @@ async function evalSnippetInjection(content, params, profile, log) { `${markerPrefix}$${markerLabel}[:${macroExpansion}` ) - if (injection && injection.macroExpansion) { continue } @@ -187,8 +190,6 @@ async function evalSnippetInjection(content, params, profile, log) { ) } - - if (injection && injection.macroExpansion) { continue } @@ -231,10 +232,10 @@ async function evalSnippetInjection(content, params, profile, log) { if (!_.isUndefined(FILE)) { // console.dir(params) - + // console.log(FILE) // if (common.isLocalFilePath(FILE)) { - + // FILE = path.posix.join(CURRENT_DIR, FILE) // console.log("is local file") @@ -311,11 +312,7 @@ async function evalSnippetInjection(content, params, profile, log) { let evaluatedContent let data = context.INPUT do { - logger.info( - `${chalk.cyan( - `try evaluate nested snippet injections` - )}` - ) + logger.info(`${chalk.cyan(`try evaluate nested snippet injections`)}`) // console.dir({ // context, // // "context.args.INPUT": context.args.INPUT, @@ -329,20 +326,15 @@ async function evalSnippetInjection(content, params, profile, log) { log ) // TODO: use common.profile and logger // console.dir({ evaluatedContent }) - data = evaluatedContent.data // console.log(data) // throw new Error(data) - // process.exit() // context.INPUT = evaluatedContent.data } while (evaluatedContent.hasSnippetInjection) - - - // console.log(context.INPUT) // process.exit() @@ -394,7 +386,7 @@ async function evalSnippetInjection(content, params, profile, log) { // if (content.includes("February")){ // // console.dir(injection) // throw new Error(content) - // } + // } line_number = injection.lineNumber } @@ -407,7 +399,7 @@ async function evalSnippetInjection(content, params, profile, log) { // if (output.includes("February")){ // // console.dir(injection) // throw new Error(output) - // } + // } // console.dir(output) // process.exit() diff --git a/src/scan-blobs.js b/src/scan-blobs.js index fe27cf7..ae4b155 100644 --- a/src/scan-blobs.js +++ b/src/scan-blobs.js @@ -5,6 +5,7 @@ const chalk = require("chalk") const error = require("./error.js") const common = require("./common.js") const logger = require("./logger.js") +const colorize = require("json-colorizer") const source_id = "scan-blobs" module.exports = async function (files, profile, log) { @@ -131,7 +132,7 @@ module.exports = async function (files, profile, log) { // logger.info(`expansion = ${expansion}`) lines[line_number] = line.replace( openRegex, - () => `${profile.MARKER_PREFIX}!${label}<:${expansion}` + () => `${profile.MARKER_PREFIX}!${label}<:${expansion} ` ) logger.info( `${chalk.cyan( @@ -275,5 +276,8 @@ module.exports = async function (files, profile, log) { ) } } + + // logger.info(colorize(focused_entity, {pretty: true})) + // process.exit() } } diff --git a/src/scan-scripts.js b/src/scan-scripts.js index 3a27f5a..8409c93 100644 --- a/src/scan-scripts.js +++ b/src/scan-scripts.js @@ -53,7 +53,7 @@ module.exports = async function (files, profile, log) { // `${chalk.cyan(`define variable`)} ${chalk.green( // name // )} ${chalk.cyan(`as`)} ${chalk.green(JSON.stringify(value))} ` - // ) + // ) _.set(profile, name, value) return true // not needed }, @@ -92,11 +92,6 @@ module.exports = async function (files, profile, log) { let params = await common.parseParams(rest) logger.info(`${chalk.cyan(`defined macro`)} ${chalk.green(name)}`) - // console.dir({ - // name, - // params, - // expansion, - // }) let LANGUAGE = _.get(params, "LANGUAGE", "ejs") _.set(params, "LANGUAGE", LANGUAGE) @@ -112,6 +107,7 @@ module.exports = async function (files, profile, log) { custom: {}, } + // logger.info(colorize(new_snippet, { pretty: true })) _.set(profile, ["snippets", name], new_snippet) return true // not needed diff --git a/src/scan-snippets.js b/src/scan-snippets.js index bcde8de..2460f61 100644 --- a/src/scan-snippets.js +++ b/src/scan-snippets.js @@ -54,7 +54,7 @@ module.exports = async function (files, profile, log) { ) let rest = _.get(openTag, "[3]", "").trim() - console.dir(rest) + // console.dir(rest) let params = await common.parseParams(rest) // console.dir({params}) // process.exit()