diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-03-12 19:40:49 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-13 03:40:49 +0800 |
commit | 3b5d5014e09f0158a46e1729f36b5cdf7e7fd0ee (patch) | |
tree | c2d5fd5fee3d39190f0cd31793473e1605a9d15e /lib/output.js | |
parent | c36c3cb47053fc83b984d3a37eb3036d6df26cbe (diff) | |
download | tracifyjs-3b5d5014e09f0158a46e1729f36b5cdf7e7fd0ee.tar.gz tracifyjs-3b5d5014e09f0158a46e1729f36b5cdf7e7fd0ee.zip |
implement `annotations` (#4763)
Diffstat (limited to 'lib/output.js')
-rw-r--r-- | lib/output.js | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/lib/output.js b/lib/output.js index ff05356e..fc71a62b 100644 --- a/lib/output.js +++ b/lib/output.js @@ -52,6 +52,7 @@ function OutputStream(options) { var readonly = !options; options = defaults(options, { + annotations : false, ascii_only : false, beautify : false, braces : false, @@ -500,12 +501,14 @@ function OutputStream(options) { space(); } } + var value = c.value.replace(/[@#]__PURE__/g, " "); + if (/^\s*$/.test(value)) return; if (/comment[134]/.test(c.type)) { - print("//" + c.value.replace(/[@#]__PURE__/g, " ") + "\n"); + print("//" + value + "\n"); indent(); last_nlb = true; } else if (c.type == "comment2") { - print("/*" + c.value.replace(/[@#]__PURE__/g, " ") + "*/"); + print("/*" + value + "*/"); last_nlb = false; } }); @@ -558,11 +561,13 @@ function OutputStream(options) { } else if (i > 0 || !tail) { space(); } + var value = c.value.replace(/[@#]__PURE__/g, " "); + if (/^\s*$/.test(value)) return; if (/comment[134]/.test(c.type)) { - print("//" + c.value.replace(/[@#]__PURE__/g, " ")); + print("//" + value); need_newline_indented = true; } else if (c.type == "comment2") { - print("/*" + c.value.replace(/[@#]__PURE__/g, " ") + "*/"); + print("/*" + value + "*/"); need_space = true; } }); @@ -1436,6 +1441,17 @@ function OutputStream(options) { }); /* -----[ other expressions ]----- */ + function print_annotation(self, output) { + if (!output.option("annotations")) return; + if (!self.pure) return; + var level = 0, parent = self, node; + do { + node = parent; + parent = output.parent(level++); + if (parent instanceof AST_Call && parent.expression === node) return; + } while (parent instanceof AST_PropAccess && parent.expression === node); + output.print("/*" + self.pure + "*/"); + } function print_call_args(self, output) { if (self.expression instanceof AST_Call || self.expression instanceof AST_Lambda) { output.add_mapping(self.start); @@ -1448,11 +1464,14 @@ function OutputStream(options) { }); } DEFPRINT(AST_Call, function(output) { - this.expression.print(output); - print_call_args(this, output); + var self = this; + print_annotation(self, output); + self.expression.print(output); + print_call_args(self, output); }); DEFPRINT(AST_New, function(output) { var self = this; + print_annotation(self, output); output.print("new"); output.space(); self.expression.print(output); |