|
| 1 | +/* |
| 2 | + Copyright (C) 2011-2013 Yusuke Suzuki <utatane.tea@gmail.com> |
| 3 | + Copyright (C) 2015 Ingvar Stepanyan <me@rreverser.com> |
| 4 | + Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com> |
| 5 | + Copyright (C) 2012 Joost-Wim Boekesteijn <joost-wim@boekesteijn.nl> |
| 6 | + Copyright (C) 2012 Yusuke Suzuki <utatane.tea@gmail.com> |
| 7 | + Copyright (C) 2012 Arpad Borsos <arpad.borsos@googlemail.com> |
| 8 | + Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com> |
| 9 | + Copyright (C) 2011 Arpad Borsos <arpad.borsos@googlemail.com> |
| 10 | +
|
| 11 | + Redistribution and use in source and binary forms, with or without |
| 12 | + modification, are permitted provided that the following conditions are met: |
| 13 | +
|
| 14 | + * Redistributions of source code must retain the above copyright |
| 15 | + notice, this list of conditions and the following disclaimer. |
| 16 | + * Redistributions in binary form must reproduce the above copyright |
| 17 | + notice, this list of conditions and the following disclaimer in the |
| 18 | + documentation and/or other materials provided with the distribution. |
| 19 | +
|
| 20 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | + ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY |
| 24 | + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 25 | + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 27 | + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 29 | + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | +*/ |
| 31 | + |
| 32 | +'use strict'; |
| 33 | + |
| 34 | +var esprima = require('./3rdparty/esprima-2.5.0'), |
| 35 | + escodegen = require('./loader'), |
| 36 | + chai = require('chai'), |
| 37 | + expect = chai.expect, |
| 38 | + data; |
| 39 | + |
| 40 | +data = { |
| 41 | + 'Harmony MetaProperty': { |
| 42 | + 'class SomeClass { constructor() { if (new.target === SomeClass) { throw new Error(\'Boom\'); }}}': { |
| 43 | + type: 'Program', |
| 44 | + body: [ { |
| 45 | + type: "ClassDeclaration", |
| 46 | + id: { |
| 47 | + type: "Identifier", |
| 48 | + name: "SomeClass" |
| 49 | + }, |
| 50 | + superClass: null, |
| 51 | + body: { |
| 52 | + type: "ClassBody", |
| 53 | + body: [ |
| 54 | + { |
| 55 | + type: "MethodDefinition", |
| 56 | + key: { |
| 57 | + type: "Identifier", |
| 58 | + name: "constructor" |
| 59 | + }, |
| 60 | + computed: false, |
| 61 | + value: { |
| 62 | + type: "FunctionExpression", |
| 63 | + id: null, |
| 64 | + params: [], |
| 65 | + defaults: [], |
| 66 | + body: { |
| 67 | + type: "BlockStatement", |
| 68 | + body: [ |
| 69 | + { |
| 70 | + type: "IfStatement", |
| 71 | + test: { |
| 72 | + type: "BinaryExpression", |
| 73 | + operator: "===", |
| 74 | + left: { |
| 75 | + type: "MetaProperty", |
| 76 | + meta: "new", |
| 77 | + property: "target" |
| 78 | + }, |
| 79 | + right: { |
| 80 | + type: "Identifier", |
| 81 | + name: "SomeClass" |
| 82 | + } |
| 83 | + }, |
| 84 | + consequent: { |
| 85 | + type: "BlockStatement", |
| 86 | + body: [ |
| 87 | + { |
| 88 | + type: "ThrowStatement", |
| 89 | + argument: { |
| 90 | + type: "NewExpression", |
| 91 | + callee: { |
| 92 | + type: "Identifier", |
| 93 | + name: "Error" |
| 94 | + }, |
| 95 | + arguments: [ |
| 96 | + { |
| 97 | + type: "Literal", |
| 98 | + value: "Boom", |
| 99 | + raw: "'Boom'" |
| 100 | + } |
| 101 | + ] |
| 102 | + } |
| 103 | + } |
| 104 | + ] |
| 105 | + }, |
| 106 | + alternate: null |
| 107 | + } |
| 108 | + ] |
| 109 | + }, |
| 110 | + generator: false, |
| 111 | + expression: false |
| 112 | + }, |
| 113 | + kind: "constructor", |
| 114 | + static: false |
| 115 | + } |
| 116 | + ], |
| 117 | + } |
| 118 | + }], |
| 119 | + } |
| 120 | + }, |
| 121 | +}; |
| 122 | + |
| 123 | +function updateDeeply(target, override) { |
| 124 | + var key, val; |
| 125 | + |
| 126 | + function isHashObject(target) { |
| 127 | + return typeof target === 'object' && target instanceof Object && !(target instanceof RegExp); |
| 128 | + } |
| 129 | + |
| 130 | + for (key in override) { |
| 131 | + if (override.hasOwnProperty(key)) { |
| 132 | + val = override[key]; |
| 133 | + if (isHashObject(val)) { |
| 134 | + if (isHashObject(target[key])) { |
| 135 | + updateDeeply(target[key], val); |
| 136 | + } else { |
| 137 | + target[key] = updateDeeply({}, val); |
| 138 | + } |
| 139 | + } else { |
| 140 | + target[key] = val; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + return target; |
| 145 | +} |
| 146 | + |
| 147 | +// Special handling for regular expression literal since we need to |
| 148 | +// convert it to a string literal, otherwise it will be decoded |
| 149 | +// as object "{}" and the regular expression would be lost. |
| 150 | +function adjustRegexLiteral(key, value) { |
| 151 | + 'use strict'; |
| 152 | + if (key === 'value' && value instanceof RegExp) { |
| 153 | + value = value.toString(); |
| 154 | + } |
| 155 | + return value; |
| 156 | +} |
| 157 | + |
| 158 | +function testIdentity(code, syntax) { |
| 159 | + 'use strict'; |
| 160 | + var expected, tree, actual, actual2, options, StringObject; |
| 161 | + |
| 162 | + // alias, so that JSLint does not complain. |
| 163 | + StringObject = String; |
| 164 | + |
| 165 | + options = { |
| 166 | + comment: false, |
| 167 | + range: false, |
| 168 | + loc: false, |
| 169 | + tokens: false, |
| 170 | + raw: false |
| 171 | + }; |
| 172 | + |
| 173 | + tree = esprima.parse(code, options); |
| 174 | + expected = JSON.stringify(tree, adjustRegexLiteral, 4); |
| 175 | + tree = esprima.parse(escodegen.generate(tree), options); |
| 176 | + actual = JSON.stringify(tree, adjustRegexLiteral, 4); |
| 177 | + tree = esprima.parse(escodegen.generate(syntax), options); |
| 178 | + actual2 = JSON.stringify(tree, adjustRegexLiteral, 4); |
| 179 | + expect(actual).to.be.equal(expected); |
| 180 | + expect(actual2).to.be.equal(expected); |
| 181 | +} |
| 182 | + |
| 183 | +function testGenerate(expected, result) { |
| 184 | + 'use strict'; |
| 185 | + var actual, options; |
| 186 | + |
| 187 | + options = { |
| 188 | + indent: ' ', |
| 189 | + parse: esprima.parse |
| 190 | + }; |
| 191 | + |
| 192 | + if (result.options) { |
| 193 | + options = updateDeeply(options, result.options); |
| 194 | + } |
| 195 | + |
| 196 | + actual = escodegen.generate(result.generateFrom, options); |
| 197 | + expect(actual).to.be.equal(expected); |
| 198 | +} |
| 199 | + |
| 200 | +function isGeneratorIdentityFixture(result) { |
| 201 | + 'use strict'; |
| 202 | + return !result.hasOwnProperty('generateFrom') && |
| 203 | + !result.hasOwnProperty('result'); |
| 204 | +} |
| 205 | + |
| 206 | +function runTest(code, result) { |
| 207 | + 'use strict'; |
| 208 | + if (result.hasOwnProperty('generateFrom')) { |
| 209 | + testGenerate(code, result); |
| 210 | + } else { |
| 211 | + testIdentity(code, result); |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +describe('harmony 2.x test', function () { |
| 216 | + Object.keys(data).forEach(function (category) { |
| 217 | + Object.keys(data[category]).forEach(function (source) { |
| 218 | + it(category, function () { |
| 219 | + var expected = data[category][source]; |
| 220 | + runTest(source, expected); |
| 221 | + }); |
| 222 | + }); |
| 223 | + }); |
| 224 | +}); |
| 225 | +/* vim: set sw=4 ts=4 et tw=80 : */ |
0 commit comments