browser/devtools/debugger/test/browser_dbg_parser-09.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/debugger/test/browser_dbg_parser-09.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,290 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +/**
     1.8 + * Test that inferring anonymous function information is done correctly
     1.9 + * from arrow expressions.
    1.10 + */
    1.11 +
    1.12 +function test() {
    1.13 +  let { Parser, ParserHelpers, SyntaxTreeVisitor } =
    1.14 +    Cu.import("resource:///modules/devtools/Parser.jsm", {});
    1.15 +
    1.16 +  function verify(source, predicate, details) {
    1.17 +    let { name, chain } = details;
    1.18 +    let [[sline, scol], [eline, ecol]] = details.loc;
    1.19 +    let ast = Parser.reflectionAPI.parse(source);
    1.20 +    let node = SyntaxTreeVisitor.filter(ast, predicate).pop();
    1.21 +    let info = ParserHelpers.inferFunctionExpressionInfo(node);
    1.22 +
    1.23 +    is(info.name, name,
    1.24 +      "The function expression assignment property name is correct.");
    1.25 +    is(chain ? info.chain.toSource() : info.chain, chain ? chain.toSource() : chain,
    1.26 +      "The function expression assignment property chain is correct.");
    1.27 +    is(info.loc.start.toSource(), { line: sline, column: scol }.toSource(),
    1.28 +      "The start location was correct for the identifier in: '" + source + "'.");
    1.29 +    is(info.loc.end.toSource(), { line: eline, column: ecol }.toSource(),
    1.30 +      "The end location was correct for the identifier in: '" + source + "'.");
    1.31 +  }
    1.32 +
    1.33 +  // VariableDeclarator
    1.34 +
    1.35 +  verify("var foo=()=>{}", e => e.type == "ArrowExpression", {
    1.36 +    name: "foo",
    1.37 +    chain: null,
    1.38 +    loc: [[1, 4], [1, 7]]
    1.39 +  });
    1.40 +  verify("\nvar\nfoo\n=\n(\n)\n=>\n{\n}\n", e => e.type == "ArrowExpression", {
    1.41 +    name: "foo",
    1.42 +    chain: null,
    1.43 +    loc: [[3, 0], [3, 3]]
    1.44 +  });
    1.45 +
    1.46 +  // AssignmentExpression
    1.47 +
    1.48 +  verify("foo=()=>{}", e => e.type == "ArrowExpression",
    1.49 +    { name: "foo", chain: [], loc: [[1, 0], [1, 3]] });
    1.50 +
    1.51 +  verify("\nfoo\n=\n(\n)\n=>\n{\n}\n", e => e.type == "ArrowExpression",
    1.52 +    { name: "foo", chain: [], loc: [[2, 0], [2, 3]] });
    1.53 +
    1.54 +  verify("foo.bar=()=>{}", e => e.type == "ArrowExpression",
    1.55 +    { name: "bar", chain: ["foo"], loc: [[1, 0], [1, 7]] });
    1.56 +
    1.57 +  verify("\nfoo.bar\n=\n(\n)\n=>\n{\n}\n", e => e.type == "ArrowExpression",
    1.58 +    { name: "bar", chain: ["foo"], loc: [[2, 0], [2, 7]] });
    1.59 +
    1.60 +  verify("this.foo=()=>{}", e => e.type == "ArrowExpression",
    1.61 +    { name: "foo", chain: ["this"], loc: [[1, 0], [1, 8]] });
    1.62 +
    1.63 +  verify("\nthis.foo\n=\n(\n)\n=>\n{\n}\n", e => e.type == "ArrowExpression",
    1.64 +    { name: "foo", chain: ["this"], loc: [[2, 0], [2, 8]] });
    1.65 +
    1.66 +  verify("this.foo.bar=()=>{}", e => e.type == "ArrowExpression",
    1.67 +    { name: "bar", chain: ["this", "foo"], loc: [[1, 0], [1, 12]] });
    1.68 +
    1.69 +  verify("\nthis.foo.bar\n=\n(\n)\n=>\n{\n}\n", e => e.type == "ArrowExpression",
    1.70 +    { name: "bar", chain: ["this", "foo"], loc: [[2, 0], [2, 12]] });
    1.71 +
    1.72 +  verify("foo.this.bar=()=>{}", e => e.type == "ArrowExpression",
    1.73 +    { name: "bar", chain: ["foo", "this"], loc: [[1, 0], [1, 12]] });
    1.74 +
    1.75 +  verify("\nfoo.this.bar\n=\n(\n)\n=>\n{\n}\n", e => e.type == "ArrowExpression",
    1.76 +    { name: "bar", chain: ["foo", "this"], loc: [[2, 0], [2, 12]] });
    1.77 +
    1.78 +  // ObjectExpression
    1.79 +
    1.80 +  verify("({foo:()=>{}})", e => e.type == "ArrowExpression",
    1.81 +    { name: "foo", chain: [], loc: [[1, 2], [1, 5]] });
    1.82 +
    1.83 +  verify("(\n{\nfoo\n:\n(\n)\n=>\n{\n}\n}\n)", e => e.type == "ArrowExpression",
    1.84 +    { name: "foo", chain: [], loc: [[3, 0], [3, 3]] });
    1.85 +
    1.86 +  verify("({foo:{bar:()=>{}}})", e => e.type == "ArrowExpression",
    1.87 +    { name: "bar", chain: ["foo"], loc: [[1, 7], [1, 10]] });
    1.88 +
    1.89 +  verify("(\n{\nfoo\n:\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n}\n)", e => e.type == "ArrowExpression",
    1.90 +    { name: "bar", chain: ["foo"], loc: [[6, 0], [6, 3]] });
    1.91 +
    1.92 +  // AssignmentExpression + ObjectExpression
    1.93 +
    1.94 +  verify("foo={bar:()=>{}}", e => e.type == "ArrowExpression",
    1.95 +    { name: "bar", chain: ["foo"], loc: [[1, 5], [1, 8]] });
    1.96 +
    1.97 +  verify("\nfoo\n=\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n", e => e.type == "ArrowExpression",
    1.98 +    { name: "bar", chain: ["foo"], loc: [[5, 0], [5, 3]] });
    1.99 +
   1.100 +  verify("foo={bar:{baz:()=>{}}}", e => e.type == "ArrowExpression",
   1.101 +    { name: "baz", chain: ["foo", "bar"], loc: [[1, 10], [1, 13]] });
   1.102 +
   1.103 +  verify("\nfoo\n=\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n", e => e.type == "ArrowExpression",
   1.104 +    { name: "baz", chain: ["foo", "bar"], loc: [[8, 0], [8, 3]] });
   1.105 +
   1.106 +  verify("nested.foo={bar:()=>{}}", e => e.type == "ArrowExpression",
   1.107 +    { name: "bar", chain: ["nested", "foo"], loc: [[1, 12], [1, 15]] });
   1.108 +
   1.109 +  verify("\nnested.foo\n=\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n", e => e.type == "ArrowExpression",
   1.110 +    { name: "bar", chain: ["nested", "foo"], loc: [[5, 0], [5, 3]] });
   1.111 +
   1.112 +  verify("nested.foo={bar:{baz:()=>{}}}", e => e.type == "ArrowExpression",
   1.113 +    { name: "baz", chain: ["nested", "foo", "bar"], loc: [[1, 17], [1, 20]] });
   1.114 +
   1.115 +  verify("\nnested.foo\n=\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n", e => e.type == "ArrowExpression",
   1.116 +    { name: "baz", chain: ["nested", "foo", "bar"], loc: [[8, 0], [8, 3]] });
   1.117 +
   1.118 +  verify("this.foo={bar:()=>{}}", e => e.type == "ArrowExpression",
   1.119 +    { name: "bar", chain: ["this", "foo"], loc: [[1, 10], [1, 13]] });
   1.120 +
   1.121 +  verify("\nthis.foo\n=\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n", e => e.type == "ArrowExpression",
   1.122 +    { name: "bar", chain: ["this", "foo"], loc: [[5, 0], [5, 3]] });
   1.123 +
   1.124 +  verify("this.foo={bar:{baz:()=>{}}}", e => e.type == "ArrowExpression",
   1.125 +    { name: "baz", chain: ["this", "foo", "bar"], loc: [[1, 15], [1, 18]] });
   1.126 +
   1.127 +  verify("\nthis.foo\n=\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n", e => e.type == "ArrowExpression",
   1.128 +    { name: "baz", chain: ["this", "foo", "bar"], loc: [[8, 0], [8, 3]] });
   1.129 +
   1.130 +  verify("this.nested.foo={bar:()=>{}}", e => e.type == "ArrowExpression",
   1.131 +    { name: "bar", chain: ["this", "nested", "foo"], loc: [[1, 17], [1, 20]] });
   1.132 +
   1.133 +  verify("\nthis.nested.foo\n=\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n", e => e.type == "ArrowExpression",
   1.134 +    { name: "bar", chain: ["this", "nested", "foo"], loc: [[5, 0], [5, 3]] });
   1.135 +
   1.136 +  verify("this.nested.foo={bar:{baz:()=>{}}}", e => e.type == "ArrowExpression",
   1.137 +    { name: "baz", chain: ["this", "nested", "foo", "bar"], loc: [[1, 22], [1, 25]] });
   1.138 +
   1.139 +  verify("\nthis.nested.foo\n=\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n", e => e.type == "ArrowExpression",
   1.140 +    { name: "baz", chain: ["this", "nested", "foo", "bar"], loc: [[8, 0], [8, 3]] });
   1.141 +
   1.142 +  verify("nested.this.foo={bar:()=>{}}", e => e.type == "ArrowExpression",
   1.143 +    { name: "bar", chain: ["nested", "this", "foo"], loc: [[1, 17], [1, 20]] });
   1.144 +
   1.145 +  verify("\nnested.this.foo\n=\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n", e => e.type == "ArrowExpression",
   1.146 +    { name: "bar", chain: ["nested", "this", "foo"], loc: [[5, 0], [5, 3]] });
   1.147 +
   1.148 +  verify("nested.this.foo={bar:{baz:()=>{}}}", e => e.type == "ArrowExpression",
   1.149 +    { name: "baz", chain: ["nested", "this", "foo", "bar"], loc: [[1, 22], [1, 25]] });
   1.150 +
   1.151 +  verify("\nnested.this.foo\n=\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n", e => e.type == "ArrowExpression",
   1.152 +    { name: "baz", chain: ["nested", "this", "foo", "bar"], loc: [[8, 0], [8, 3]] });
   1.153 +
   1.154 +  // VariableDeclarator + AssignmentExpression + ObjectExpression
   1.155 +
   1.156 +  verify("let foo={bar:()=>{}}", e => e.type == "ArrowExpression",
   1.157 +    { name: "bar", chain: ["foo"], loc: [[1, 9], [1, 12]] });
   1.158 +
   1.159 +  verify("\nlet\nfoo\n=\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n", e => e.type == "ArrowExpression",
   1.160 +    { name: "bar", chain: ["foo"], loc: [[6, 0], [6, 3]] });
   1.161 +
   1.162 +  verify("let foo={bar:{baz:()=>{}}}", e => e.type == "ArrowExpression",
   1.163 +    { name: "baz", chain: ["foo", "bar"], loc: [[1, 14], [1, 17]] });
   1.164 +
   1.165 +  verify("\nlet\nfoo\n=\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n", e => e.type == "ArrowExpression",
   1.166 +    { name: "baz", chain: ["foo", "bar"], loc: [[9, 0], [9, 3]] });
   1.167 +
   1.168 +  // New/CallExpression + AssignmentExpression + ObjectExpression
   1.169 +
   1.170 +  verify("foo({bar:()=>{}})", e => e.type == "ArrowExpression",
   1.171 +    { name: "bar", chain: [], loc: [[1, 5], [1, 8]] });
   1.172 +
   1.173 +  verify("\nfoo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.174 +    { name: "bar", chain: [], loc: [[5, 0], [5, 3]] });
   1.175 +
   1.176 +  verify("foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression",
   1.177 +    { name: "baz", chain: ["bar"], loc: [[1, 10], [1, 13]] });
   1.178 +
   1.179 +  verify("\nfoo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.180 +    { name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] });
   1.181 +
   1.182 +  verify("nested.foo({bar:()=>{}})", e => e.type == "ArrowExpression",
   1.183 +    { name: "bar", chain: [], loc: [[1, 12], [1, 15]] });
   1.184 +
   1.185 +  verify("\nnested.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.186 +    { name: "bar", chain: [], loc: [[5, 0], [5, 3]] });
   1.187 +
   1.188 +  verify("nested.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression",
   1.189 +    { name: "baz", chain: ["bar"], loc: [[1, 17], [1, 20]] });
   1.190 +
   1.191 +  verify("\nnested.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.192 +    { name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] });
   1.193 +
   1.194 +  verify("this.foo({bar:()=>{}})", e => e.type == "ArrowExpression",
   1.195 +    { name: "bar", chain: [], loc: [[1, 10], [1, 13]] });
   1.196 +
   1.197 +  verify("\nthis.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.198 +    { name: "bar", chain: [], loc: [[5, 0], [5, 3]] });
   1.199 +
   1.200 +  verify("this.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression",
   1.201 +    { name: "baz", chain: ["bar"], loc: [[1, 15], [1, 18]] });
   1.202 +
   1.203 +  verify("\nthis.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.204 +    { name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] });
   1.205 +
   1.206 +  verify("this.nested.foo({bar:()=>{}})", e => e.type == "ArrowExpression",
   1.207 +    { name: "bar", chain: [], loc: [[1, 17], [1, 20]] });
   1.208 +
   1.209 +  verify("\nthis.nested.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.210 +    { name: "bar", chain: [], loc: [[5, 0], [5, 3]] });
   1.211 +
   1.212 +  verify("this.nested.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression",
   1.213 +    { name: "baz", chain: ["bar"], loc: [[1, 22], [1, 25]] });
   1.214 +
   1.215 +  verify("\nthis.nested.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.216 +    { name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] });
   1.217 +
   1.218 +  verify("nested.this.foo({bar:()=>{}})", e => e.type == "ArrowExpression",
   1.219 +    { name: "bar", chain: [], loc: [[1, 17], [1, 20]] });
   1.220 +
   1.221 +  verify("\nnested.this.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.222 +    { name: "bar", chain: [], loc: [[5, 0], [5, 3]] });
   1.223 +
   1.224 +  verify("nested.this.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression",
   1.225 +    { name: "baz", chain: ["bar"], loc: [[1, 22], [1, 25]] });
   1.226 +
   1.227 +  verify("\nnested.this.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.228 +    { name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] });
   1.229 +
   1.230 +  // New/CallExpression + VariableDeclarator + AssignmentExpression + ObjectExpression
   1.231 +
   1.232 +  verify("let target=foo({bar:()=>{}})", e => e.type == "ArrowExpression",
   1.233 +    { name: "bar", chain: ["target"], loc: [[1, 16], [1, 19]] });
   1.234 +
   1.235 +  verify("\nlet\ntarget=\nfoo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.236 +    { name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] });
   1.237 +
   1.238 +  verify("let target=foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression",
   1.239 +    { name: "baz", chain: ["target", "bar"], loc: [[1, 21], [1, 24]] });
   1.240 +
   1.241 +  verify("\nlet\ntarget=\nfoo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.242 +    { name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] });
   1.243 +
   1.244 +  verify("let target=nested.foo({bar:()=>{}})", e => e.type == "ArrowExpression",
   1.245 +    { name: "bar", chain: ["target"], loc: [[1, 23], [1, 26]] });
   1.246 +
   1.247 +  verify("\nlet\ntarget=\nnested.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.248 +    { name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] });
   1.249 +
   1.250 +  verify("let target=nested.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression",
   1.251 +    { name: "baz", chain: ["target", "bar"], loc: [[1, 28], [1, 31]] });
   1.252 +
   1.253 +  verify("\nlet\ntarget=\nnested.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.254 +    { name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] });
   1.255 +
   1.256 +  verify("let target=this.foo({bar:()=>{}})", e => e.type == "ArrowExpression",
   1.257 +    { name: "bar", chain: ["target"], loc: [[1, 21], [1, 24]] });
   1.258 +
   1.259 +  verify("\nlet\ntarget=\nthis.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.260 +    { name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] });
   1.261 +
   1.262 +  verify("let target=this.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression",
   1.263 +    { name: "baz", chain: ["target", "bar"], loc: [[1, 26], [1, 29]] });
   1.264 +
   1.265 +  verify("\nlet\ntarget=\nthis.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.266 +    { name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] });
   1.267 +
   1.268 +  verify("let target=this.nested.foo({bar:()=>{}})", e => e.type == "ArrowExpression",
   1.269 +    { name: "bar", chain: ["target"], loc: [[1, 28], [1, 31]] });
   1.270 +
   1.271 +  verify("\nlet\ntarget=\nthis.nested.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.272 +    { name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] });
   1.273 +
   1.274 +  verify("let target=this.nested.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression",
   1.275 +    { name: "baz", chain: ["target", "bar"], loc: [[1, 33], [1, 36]] });
   1.276 +
   1.277 +  verify("\nlet\ntarget=\nthis.nested.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.278 +    { name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] });
   1.279 +
   1.280 +  verify("let target=nested.this.foo({bar:()=>{}})", e => e.type == "ArrowExpression",
   1.281 +    { name: "bar", chain: ["target"], loc: [[1, 28], [1, 31]] });
   1.282 +
   1.283 +  verify("\nlet\ntarget=\nnested.this.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.284 +    { name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] });
   1.285 +
   1.286 +  verify("let target=nested.this.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression",
   1.287 +    { name: "baz", chain: ["target", "bar"], loc: [[1, 33], [1, 36]] });
   1.288 +
   1.289 +  verify("\nlet\ntarget=\nnested.this.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression",
   1.290 +    { name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] });
   1.291 +
   1.292 +  finish();
   1.293 +}

mercurial