From 4c4b8cd0a5bfc2478e52320e10a970169868d568 Mon Sep 17 00:00:00 2001 From: Surma Date: Sun, 18 Apr 2021 12:26:20 +0100 Subject: [PATCH 1/5] Unify handling of transforms --- cli/asc.js | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/cli/asc.js b/cli/asc.js index 341aac5cd5..e1e2578274 100644 --- a/cli/asc.js +++ b/cli/asc.js @@ -499,11 +499,14 @@ exports.main = function main(argv, options, callback) { // Initialize the program program = __pin(assemblyscript.newProgram(compilerOptions)); - // Set up transforms - const transforms = []; + // Collect transforms *constructors* from the `--transform` CLI flag as well + // as the `transform` option into the `transforms` array. + let transforms = []; + // `transform` option from `main()` if (Array.isArray(options.transforms)) { transforms.push(...options.transforms); } + // `--transform` CLI flag if (opts.transform) { let tsNodeRegistered = false; let transformArgs = unique(opts.transform); @@ -514,28 +517,35 @@ exports.main = function main(argv, options, callback) { tsNodeRegistered = true; } try { - const classOrModule = dynrequire(dynrequire.resolve(filename, { paths: [baseDir, process.cwd()] })); - if (typeof classOrModule === "function") { - Object.assign(classOrModule.prototype, { - program, - baseDir, - stdout, - stderr, - log: console.error, - readFile, - writeFile, - listFiles - }); - transforms.push(new classOrModule()); - } else { - transforms.push(classOrModule); // legacy module - } - } catch (e) { + transforms.push(dynrequire(dynrequire.resolve(filename, { paths: [baseDir, process.cwd()] }))); + } catch(e) { return callback(e); } } } + // Fix up the prototype of the transforms’ constructors and instantiate them. + try { + transforms = transforms.map(classOrModule => { + // Except if it’s a legacy module, just pass it through. + if (typeof classOrModule !== "function") { + return classOrModule; + } + return Object.assign(classOrModule.prototype, { + program, + baseDir, + stdout, + stderr, + log: console.error, + readFile, + writeFile, + listFiles + }); + }); + } catch (e) { + return callback(e) + } + function applyTransform(name, ...args) { for (let i = 0, k = transforms.length; i < k; ++i) { let transform = transforms[i]; From 1d8c561722a26ca1547aab6e752abb4cd98828c5 Mon Sep 17 00:00:00 2001 From: Surma Date: Sun, 18 Apr 2021 16:28:00 +0100 Subject: [PATCH 2/5] Update cli/asc.js Co-authored-by: Max Graey --- cli/asc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/asc.js b/cli/asc.js index e1e2578274..cc42f92244 100644 --- a/cli/asc.js +++ b/cli/asc.js @@ -518,7 +518,7 @@ exports.main = function main(argv, options, callback) { } try { transforms.push(dynrequire(dynrequire.resolve(filename, { paths: [baseDir, process.cwd()] }))); - } catch(e) { + } catch (e) { return callback(e); } } From 18fad9218540d4c425393bb1f34fcf121750673b Mon Sep 17 00:00:00 2001 From: Surma Date: Sun, 18 Apr 2021 16:28:07 +0100 Subject: [PATCH 3/5] Update cli/asc.js Co-authored-by: Max Graey --- cli/asc.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/cli/asc.js b/cli/asc.js index cc42f92244..fd93e393e1 100644 --- a/cli/asc.js +++ b/cli/asc.js @@ -532,15 +532,15 @@ exports.main = function main(argv, options, callback) { return classOrModule; } return Object.assign(classOrModule.prototype, { - program, - baseDir, - stdout, - stderr, - log: console.error, - readFile, - writeFile, - listFiles - }); + program, + baseDir, + stdout, + stderr, + log: console.error, + readFile, + writeFile, + listFiles + }); }); } catch (e) { return callback(e) From 73ccc06531ebc117cb45032db4bce5d02112dd94 Mon Sep 17 00:00:00 2001 From: Surma Date: Sun, 18 Apr 2021 19:06:14 +0100 Subject: [PATCH 4/5] Update cli/asc.js Co-authored-by: Max Graey --- cli/asc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/asc.js b/cli/asc.js index fd93e393e1..3882628cfe 100644 --- a/cli/asc.js +++ b/cli/asc.js @@ -543,7 +543,7 @@ exports.main = function main(argv, options, callback) { }); }); } catch (e) { - return callback(e) + return callback(e); } function applyTransform(name, ...args) { From 69100eb662fd8a76608999ca90b105af4a864bd7 Mon Sep 17 00:00:00 2001 From: Surma Date: Sun, 18 Apr 2021 22:56:14 +0100 Subject: [PATCH 5/5] Forgot to instantiate --- cli/asc.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cli/asc.js b/cli/asc.js index 3882628cfe..e33875a8dc 100644 --- a/cli/asc.js +++ b/cli/asc.js @@ -531,7 +531,7 @@ exports.main = function main(argv, options, callback) { if (typeof classOrModule !== "function") { return classOrModule; } - return Object.assign(classOrModule.prototype, { + Object.assign(classOrModule.prototype, { program, baseDir, stdout, @@ -541,6 +541,7 @@ exports.main = function main(argv, options, callback) { writeFile, listFiles }); + return new classOrModule(); }); } catch (e) { return callback(e);