diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-03-19 02:17:15 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-19 02:17:15 +0800 |
commit | 274331d0ea05197ea7cb531ccd1d78e0c7b8662c (patch) | |
tree | c4b77238219b3e1d547ac297901395ff8c618612 /lib/compress.js | |
parent | 0489d6de6499154c758a6464387546ec5a060f67 (diff) | |
download | tracifyjs-274331d0ea05197ea7cb531ccd1d78e0c7b8662c.tar.gz tracifyjs-274331d0ea05197ea7cb531ccd1d78e0c7b8662c.zip |
transform String.charAt() to index access (#1620)
Guarded by `unsafe` as `charAt()` can be overridden.
Diffstat (limited to 'lib/compress.js')
-rw-r--r-- | lib/compress.js | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/lib/compress.js b/lib/compress.js index c3f12549..66a4120e 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2656,7 +2656,7 @@ merge(Compressor.prototype, { if (self.args.length != 1) { return make_node(AST_Array, self, { elements: self.args - }).transform(compressor); + }).optimize(compressor); } break; case "Object": @@ -2674,7 +2674,7 @@ merge(Compressor.prototype, { left: self.args[0], operator: "+", right: make_node(AST_String, self, { value: "" }) - }).transform(compressor); + }).optimize(compressor); break; case "Number": if (self.args.length == 0) return make_node(AST_Number, self, { @@ -2683,7 +2683,7 @@ merge(Compressor.prototype, { if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, { expression: self.args[0], operator: "+" - }).transform(compressor); + }).optimize(compressor); case "Boolean": if (self.args.length == 0) return make_node(AST_False, self); if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, { @@ -2692,7 +2692,7 @@ merge(Compressor.prototype, { operator: "!" }), operator: "!" - }).transform(compressor); + }).optimize(compressor); break; case "Function": // new Function() => function(){} @@ -2757,7 +2757,7 @@ merge(Compressor.prototype, { left: make_node(AST_String, self, { value: "" }), operator: "+", right: exp.expression - }).transform(compressor); + }).optimize(compressor); } else if (exp instanceof AST_Dot && exp.expression instanceof AST_Array && exp.property == "join") EXIT: { var separator; @@ -2811,7 +2811,7 @@ merge(Compressor.prototype, { left : prev, right : el }); - }, first).transform(compressor); + }, first).optimize(compressor); } // need this awkward cloning to not affect original element // best_of will decide which one to get through. @@ -2821,6 +2821,16 @@ merge(Compressor.prototype, { node.expression.expression.elements = elements; return best_of(compressor, self, node); } + else if (exp instanceof AST_Dot && exp.expression.is_string(compressor) && exp.property == "charAt") { + var arg = self.args[0]; + var index = arg ? arg.evaluate(compressor) : 0; + if (index !== arg) { + return make_node(AST_Sub, exp, { + expression: exp.expression, + property: make_node_from_constant(index | 0, arg || exp) + }).optimize(compressor); + } + } } if (exp instanceof AST_Function) { if (exp.body[0] instanceof AST_Return) { |