michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: /** michael@0: * Test that inferring anonymous function information is done correctly michael@0: * from arrow expressions. michael@0: */ michael@0: michael@0: function test() { michael@0: let { Parser, ParserHelpers, SyntaxTreeVisitor } = michael@0: Cu.import("resource:///modules/devtools/Parser.jsm", {}); michael@0: michael@0: function verify(source, predicate, details) { michael@0: let { name, chain } = details; michael@0: let [[sline, scol], [eline, ecol]] = details.loc; michael@0: let ast = Parser.reflectionAPI.parse(source); michael@0: let node = SyntaxTreeVisitor.filter(ast, predicate).pop(); michael@0: let info = ParserHelpers.inferFunctionExpressionInfo(node); michael@0: michael@0: is(info.name, name, michael@0: "The function expression assignment property name is correct."); michael@0: is(chain ? info.chain.toSource() : info.chain, chain ? chain.toSource() : chain, michael@0: "The function expression assignment property chain is correct."); michael@0: is(info.loc.start.toSource(), { line: sline, column: scol }.toSource(), michael@0: "The start location was correct for the identifier in: '" + source + "'."); michael@0: is(info.loc.end.toSource(), { line: eline, column: ecol }.toSource(), michael@0: "The end location was correct for the identifier in: '" + source + "'."); michael@0: } michael@0: michael@0: // VariableDeclarator michael@0: michael@0: verify("var foo=()=>{}", e => e.type == "ArrowExpression", { michael@0: name: "foo", michael@0: chain: null, michael@0: loc: [[1, 4], [1, 7]] michael@0: }); michael@0: verify("\nvar\nfoo\n=\n(\n)\n=>\n{\n}\n", e => e.type == "ArrowExpression", { michael@0: name: "foo", michael@0: chain: null, michael@0: loc: [[3, 0], [3, 3]] michael@0: }); michael@0: michael@0: // AssignmentExpression michael@0: michael@0: verify("foo=()=>{}", e => e.type == "ArrowExpression", michael@0: { name: "foo", chain: [], loc: [[1, 0], [1, 3]] }); michael@0: michael@0: verify("\nfoo\n=\n(\n)\n=>\n{\n}\n", e => e.type == "ArrowExpression", michael@0: { name: "foo", chain: [], loc: [[2, 0], [2, 3]] }); michael@0: michael@0: verify("foo.bar=()=>{}", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["foo"], loc: [[1, 0], [1, 7]] }); michael@0: michael@0: verify("\nfoo.bar\n=\n(\n)\n=>\n{\n}\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["foo"], loc: [[2, 0], [2, 7]] }); michael@0: michael@0: verify("this.foo=()=>{}", e => e.type == "ArrowExpression", michael@0: { name: "foo", chain: ["this"], loc: [[1, 0], [1, 8]] }); michael@0: michael@0: verify("\nthis.foo\n=\n(\n)\n=>\n{\n}\n", e => e.type == "ArrowExpression", michael@0: { name: "foo", chain: ["this"], loc: [[2, 0], [2, 8]] }); michael@0: michael@0: verify("this.foo.bar=()=>{}", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["this", "foo"], loc: [[1, 0], [1, 12]] }); michael@0: michael@0: verify("\nthis.foo.bar\n=\n(\n)\n=>\n{\n}\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["this", "foo"], loc: [[2, 0], [2, 12]] }); michael@0: michael@0: verify("foo.this.bar=()=>{}", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["foo", "this"], loc: [[1, 0], [1, 12]] }); michael@0: michael@0: verify("\nfoo.this.bar\n=\n(\n)\n=>\n{\n}\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["foo", "this"], loc: [[2, 0], [2, 12]] }); michael@0: michael@0: // ObjectExpression michael@0: michael@0: verify("({foo:()=>{}})", e => e.type == "ArrowExpression", michael@0: { name: "foo", chain: [], loc: [[1, 2], [1, 5]] }); michael@0: michael@0: verify("(\n{\nfoo\n:\n(\n)\n=>\n{\n}\n}\n)", e => e.type == "ArrowExpression", michael@0: { name: "foo", chain: [], loc: [[3, 0], [3, 3]] }); michael@0: michael@0: verify("({foo:{bar:()=>{}}})", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["foo"], loc: [[1, 7], [1, 10]] }); michael@0: michael@0: verify("(\n{\nfoo\n:\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n}\n)", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["foo"], loc: [[6, 0], [6, 3]] }); michael@0: michael@0: // AssignmentExpression + ObjectExpression michael@0: michael@0: verify("foo={bar:()=>{}}", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["foo"], loc: [[1, 5], [1, 8]] }); michael@0: michael@0: verify("\nfoo\n=\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["foo"], loc: [[5, 0], [5, 3]] }); michael@0: michael@0: verify("foo={bar:{baz:()=>{}}}", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["foo", "bar"], loc: [[1, 10], [1, 13]] }); michael@0: michael@0: verify("\nfoo\n=\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["foo", "bar"], loc: [[8, 0], [8, 3]] }); michael@0: michael@0: verify("nested.foo={bar:()=>{}}", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["nested", "foo"], loc: [[1, 12], [1, 15]] }); michael@0: michael@0: verify("\nnested.foo\n=\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["nested", "foo"], loc: [[5, 0], [5, 3]] }); michael@0: michael@0: verify("nested.foo={bar:{baz:()=>{}}}", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["nested", "foo", "bar"], loc: [[1, 17], [1, 20]] }); michael@0: michael@0: verify("\nnested.foo\n=\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["nested", "foo", "bar"], loc: [[8, 0], [8, 3]] }); michael@0: michael@0: verify("this.foo={bar:()=>{}}", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["this", "foo"], loc: [[1, 10], [1, 13]] }); michael@0: michael@0: verify("\nthis.foo\n=\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["this", "foo"], loc: [[5, 0], [5, 3]] }); michael@0: michael@0: verify("this.foo={bar:{baz:()=>{}}}", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["this", "foo", "bar"], loc: [[1, 15], [1, 18]] }); michael@0: michael@0: verify("\nthis.foo\n=\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["this", "foo", "bar"], loc: [[8, 0], [8, 3]] }); michael@0: michael@0: verify("this.nested.foo={bar:()=>{}}", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["this", "nested", "foo"], loc: [[1, 17], [1, 20]] }); michael@0: michael@0: verify("\nthis.nested.foo\n=\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["this", "nested", "foo"], loc: [[5, 0], [5, 3]] }); michael@0: michael@0: verify("this.nested.foo={bar:{baz:()=>{}}}", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["this", "nested", "foo", "bar"], loc: [[1, 22], [1, 25]] }); michael@0: michael@0: verify("\nthis.nested.foo\n=\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["this", "nested", "foo", "bar"], loc: [[8, 0], [8, 3]] }); michael@0: michael@0: verify("nested.this.foo={bar:()=>{}}", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["nested", "this", "foo"], loc: [[1, 17], [1, 20]] }); michael@0: michael@0: verify("\nnested.this.foo\n=\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["nested", "this", "foo"], loc: [[5, 0], [5, 3]] }); michael@0: michael@0: verify("nested.this.foo={bar:{baz:()=>{}}}", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["nested", "this", "foo", "bar"], loc: [[1, 22], [1, 25]] }); michael@0: michael@0: verify("\nnested.this.foo\n=\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["nested", "this", "foo", "bar"], loc: [[8, 0], [8, 3]] }); michael@0: michael@0: // VariableDeclarator + AssignmentExpression + ObjectExpression michael@0: michael@0: verify("let foo={bar:()=>{}}", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["foo"], loc: [[1, 9], [1, 12]] }); michael@0: michael@0: verify("\nlet\nfoo\n=\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["foo"], loc: [[6, 0], [6, 3]] }); michael@0: michael@0: verify("let foo={bar:{baz:()=>{}}}", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["foo", "bar"], loc: [[1, 14], [1, 17]] }); michael@0: michael@0: verify("\nlet\nfoo\n=\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["foo", "bar"], loc: [[9, 0], [9, 3]] }); michael@0: michael@0: // New/CallExpression + AssignmentExpression + ObjectExpression michael@0: michael@0: verify("foo({bar:()=>{}})", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: [], loc: [[1, 5], [1, 8]] }); michael@0: michael@0: verify("\nfoo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: [], loc: [[5, 0], [5, 3]] }); michael@0: michael@0: verify("foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["bar"], loc: [[1, 10], [1, 13]] }); michael@0: michael@0: verify("\nfoo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] }); michael@0: michael@0: verify("nested.foo({bar:()=>{}})", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: [], loc: [[1, 12], [1, 15]] }); michael@0: michael@0: verify("\nnested.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: [], loc: [[5, 0], [5, 3]] }); michael@0: michael@0: verify("nested.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["bar"], loc: [[1, 17], [1, 20]] }); michael@0: michael@0: verify("\nnested.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] }); michael@0: michael@0: verify("this.foo({bar:()=>{}})", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: [], loc: [[1, 10], [1, 13]] }); michael@0: michael@0: verify("\nthis.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: [], loc: [[5, 0], [5, 3]] }); michael@0: michael@0: verify("this.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["bar"], loc: [[1, 15], [1, 18]] }); michael@0: michael@0: verify("\nthis.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] }); michael@0: michael@0: verify("this.nested.foo({bar:()=>{}})", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: [], loc: [[1, 17], [1, 20]] }); michael@0: michael@0: verify("\nthis.nested.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: [], loc: [[5, 0], [5, 3]] }); michael@0: michael@0: verify("this.nested.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["bar"], loc: [[1, 22], [1, 25]] }); michael@0: michael@0: verify("\nthis.nested.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] }); michael@0: michael@0: verify("nested.this.foo({bar:()=>{}})", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: [], loc: [[1, 17], [1, 20]] }); michael@0: michael@0: verify("\nnested.this.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: [], loc: [[5, 0], [5, 3]] }); michael@0: michael@0: verify("nested.this.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["bar"], loc: [[1, 22], [1, 25]] }); michael@0: michael@0: verify("\nnested.this.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] }); michael@0: michael@0: // New/CallExpression + VariableDeclarator + AssignmentExpression + ObjectExpression michael@0: michael@0: verify("let target=foo({bar:()=>{}})", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["target"], loc: [[1, 16], [1, 19]] }); michael@0: michael@0: verify("\nlet\ntarget=\nfoo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] }); michael@0: michael@0: verify("let target=foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["target", "bar"], loc: [[1, 21], [1, 24]] }); michael@0: michael@0: verify("\nlet\ntarget=\nfoo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] }); michael@0: michael@0: verify("let target=nested.foo({bar:()=>{}})", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["target"], loc: [[1, 23], [1, 26]] }); michael@0: michael@0: verify("\nlet\ntarget=\nnested.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] }); michael@0: michael@0: verify("let target=nested.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["target", "bar"], loc: [[1, 28], [1, 31]] }); michael@0: michael@0: 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", michael@0: { name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] }); michael@0: michael@0: verify("let target=this.foo({bar:()=>{}})", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["target"], loc: [[1, 21], [1, 24]] }); michael@0: michael@0: verify("\nlet\ntarget=\nthis.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] }); michael@0: michael@0: verify("let target=this.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["target", "bar"], loc: [[1, 26], [1, 29]] }); michael@0: michael@0: 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", michael@0: { name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] }); michael@0: michael@0: verify("let target=this.nested.foo({bar:()=>{}})", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["target"], loc: [[1, 28], [1, 31]] }); michael@0: michael@0: verify("\nlet\ntarget=\nthis.nested.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] }); michael@0: michael@0: verify("let target=this.nested.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["target", "bar"], loc: [[1, 33], [1, 36]] }); michael@0: michael@0: 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", michael@0: { name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] }); michael@0: michael@0: verify("let target=nested.this.foo({bar:()=>{}})", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["target"], loc: [[1, 28], [1, 31]] }); michael@0: michael@0: verify("\nlet\ntarget=\nnested.this.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", michael@0: { name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] }); michael@0: michael@0: verify("let target=nested.this.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", michael@0: { name: "baz", chain: ["target", "bar"], loc: [[1, 33], [1, 36]] }); michael@0: michael@0: 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", michael@0: { name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] }); michael@0: michael@0: finish(); michael@0: }