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 creating an evaluation string for certain nodes works properly. 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, string) { michael@0: let ast = Parser.reflectionAPI.parse(source); michael@0: let node = SyntaxTreeVisitor.filter(ast, predicate).pop(); michael@0: let info = ParserHelpers.getIdentifierEvalString(node); michael@0: is(info, string, "The identifier evaluation string is correct."); michael@0: } michael@0: michael@0: // Indentifier or Literal michael@0: michael@0: verify("foo", e => e.type == "Identifier", "foo"); michael@0: verify("undefined", e => e.type == "Identifier", "undefined"); michael@0: verify("null", e => e.type == "Literal", "null"); michael@0: verify("42", e => e.type == "Literal", "42"); michael@0: verify("true", e => e.type == "Literal", "true"); michael@0: verify("\"nasu\"", e => e.type == "Literal", "\"nasu\""); michael@0: michael@0: // MemberExpression or ThisExpression michael@0: michael@0: verify("this", e => e.type == "ThisExpression", "this"); michael@0: verify("foo.bar", e => e.name == "foo", "foo"); michael@0: verify("foo.bar", e => e.name == "bar", "foo.bar"); michael@0: michael@0: // MemberExpression + ThisExpression michael@0: michael@0: verify("this.foo.bar", e => e.type == "ThisExpression", "this"); michael@0: verify("this.foo.bar", e => e.name == "foo", "this.foo"); michael@0: verify("this.foo.bar", e => e.name == "bar", "this.foo.bar"); michael@0: michael@0: verify("foo.this.bar", e => e.name == "foo", "foo"); michael@0: verify("foo.this.bar", e => e.name == "this", "foo.this"); michael@0: verify("foo.this.bar", e => e.name == "bar", "foo.this.bar"); michael@0: michael@0: // ObjectExpression + VariableDeclarator michael@0: michael@0: verify("let foo={bar:baz}", e => e.name == "baz", "baz"); michael@0: verify("let foo={bar:undefined}", e => e.name == "undefined", "undefined"); michael@0: verify("let foo={bar:null}", e => e.type == "Literal", "null"); michael@0: verify("let foo={bar:42}", e => e.type == "Literal", "42"); michael@0: verify("let foo={bar:true}", e => e.type == "Literal", "true"); michael@0: verify("let foo={bar:\"nasu\"}", e => e.type == "Literal", "\"nasu\""); michael@0: verify("let foo={bar:this}", e => e.type == "ThisExpression", "this"); michael@0: michael@0: verify("let foo={bar:{nested:baz}}", e => e.name == "baz", "baz"); michael@0: verify("let foo={bar:{nested:undefined}}", e => e.name == "undefined", "undefined"); michael@0: verify("let foo={bar:{nested:null}}", e => e.type == "Literal", "null"); michael@0: verify("let foo={bar:{nested:42}}", e => e.type == "Literal", "42"); michael@0: verify("let foo={bar:{nested:true}}", e => e.type == "Literal", "true"); michael@0: verify("let foo={bar:{nested:\"nasu\"}}", e => e.type == "Literal", "\"nasu\""); michael@0: verify("let foo={bar:{nested:this}}", e => e.type == "ThisExpression", "this"); michael@0: michael@0: verify("let foo={bar:baz}", e => e.name == "bar", "foo.bar"); michael@0: verify("let foo={bar:baz}", e => e.name == "foo", "foo"); michael@0: michael@0: verify("let foo={bar:{nested:baz}}", e => e.name == "nested", "foo.bar.nested"); michael@0: verify("let foo={bar:{nested:baz}}", e => e.name == "bar", "foo.bar"); michael@0: verify("let foo={bar:{nested:baz}}", e => e.name == "foo", "foo"); michael@0: michael@0: // ObjectExpression + MemberExpression michael@0: michael@0: verify("parent.foo={bar:baz}", e => e.name == "bar", "parent.foo.bar"); michael@0: verify("parent.foo={bar:baz}", e => e.name == "foo", "parent.foo"); michael@0: verify("parent.foo={bar:baz}", e => e.name == "parent", "parent"); michael@0: michael@0: verify("parent.foo={bar:{nested:baz}}", e => e.name == "nested", "parent.foo.bar.nested"); michael@0: verify("parent.foo={bar:{nested:baz}}", e => e.name == "bar", "parent.foo.bar"); michael@0: verify("parent.foo={bar:{nested:baz}}", e => e.name == "foo", "parent.foo"); michael@0: verify("parent.foo={bar:{nested:baz}}", e => e.name == "parent", "parent"); michael@0: michael@0: verify("this.foo={bar:{nested:baz}}", e => e.name == "nested", "this.foo.bar.nested"); michael@0: verify("this.foo={bar:{nested:baz}}", e => e.name == "bar", "this.foo.bar"); michael@0: verify("this.foo={bar:{nested:baz}}", e => e.name == "foo", "this.foo"); michael@0: verify("this.foo={bar:{nested:baz}}", e => e.type == "ThisExpression", "this"); michael@0: michael@0: verify("this.parent.foo={bar:{nested:baz}}", e => e.name == "nested", "this.parent.foo.bar.nested"); michael@0: verify("this.parent.foo={bar:{nested:baz}}", e => e.name == "bar", "this.parent.foo.bar"); michael@0: verify("this.parent.foo={bar:{nested:baz}}", e => e.name == "foo", "this.parent.foo"); michael@0: verify("this.parent.foo={bar:{nested:baz}}", e => e.name == "parent", "this.parent"); michael@0: verify("this.parent.foo={bar:{nested:baz}}", e => e.type == "ThisExpression", "this"); michael@0: michael@0: verify("parent.this.foo={bar:{nested:baz}}", e => e.name == "nested", "parent.this.foo.bar.nested"); michael@0: verify("parent.this.foo={bar:{nested:baz}}", e => e.name == "bar", "parent.this.foo.bar"); michael@0: verify("parent.this.foo={bar:{nested:baz}}", e => e.name == "foo", "parent.this.foo"); michael@0: verify("parent.this.foo={bar:{nested:baz}}", e => e.name == "this", "parent.this"); michael@0: verify("parent.this.foo={bar:{nested:baz}}", e => e.name == "parent", "parent"); michael@0: michael@0: // FunctionExpression michael@0: michael@0: verify("function foo(){}", e => e.name == "foo", "foo"); michael@0: verify("var foo=function(){}", e => e.name == "foo", "foo"); michael@0: verify("var foo=function bar(){}", e => e.name == "bar", "bar"); michael@0: michael@0: // New/CallExpression michael@0: michael@0: verify("foo()", e => e.name == "foo", "foo"); michael@0: verify("new foo()", e => e.name == "foo", "foo"); michael@0: michael@0: verify("foo(bar)", e => e.name == "bar", "bar"); michael@0: verify("foo(bar, baz)", e => e.name == "baz", "baz"); michael@0: verify("foo(undefined)", e => e.name == "undefined", "undefined"); michael@0: verify("foo(null)", e => e.type == "Literal", "null"); michael@0: verify("foo(42)", e => e.type == "Literal", "42"); michael@0: verify("foo(true)", e => e.type == "Literal", "true"); michael@0: verify("foo(\"nasu\")", e => e.type == "Literal", "\"nasu\""); michael@0: verify("foo(this)", e => e.type == "ThisExpression", "this"); michael@0: michael@0: // New/CallExpression + ObjectExpression + MemberExpression michael@0: michael@0: verify("fun(this.parent.foo={bar:{nested:baz}})", e => e.name == "nested", "this.parent.foo.bar.nested"); michael@0: verify("fun(this.parent.foo={bar:{nested:baz}})", e => e.name == "bar", "this.parent.foo.bar"); michael@0: verify("fun(this.parent.foo={bar:{nested:baz}})", e => e.name == "foo", "this.parent.foo"); michael@0: verify("fun(this.parent.foo={bar:{nested:baz}})", e => e.name == "parent", "this.parent"); michael@0: verify("fun(this.parent.foo={bar:{nested:baz}})", e => e.type == "ThisExpression", "this"); michael@0: verify("fun(this.parent.foo={bar:{nested:baz}})", e => e.name == "fun", "fun"); michael@0: michael@0: finish(); michael@0: }