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: * Check that some potentially problematic identifier nodes have the michael@0: * right location information attached. 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, [sline, scol], [eline, ecol]) { michael@0: let ast = Parser.reflectionAPI.parse(source); michael@0: let node = SyntaxTreeVisitor.filter(ast, predicate).pop(); michael@0: let loc = ParserHelpers.getNodeLocation(node); michael@0: michael@0: is(loc.start.toSource(), { line: sline, column: scol }.toSource(), michael@0: "The start location was correct for the identifier in: '" + source + "'."); michael@0: is(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: // FunctionDeclarations and FunctionExpressions. michael@0: michael@0: // The location is unavailable for the identifier node "foo". michael@0: verify("function foo(){}", e => e.name == "foo", [1, 9], [1, 12]); michael@0: verify("\nfunction\nfoo\n(\n)\n{\n}\n", e => e.name == "foo", [3, 0], [3, 3]); michael@0: michael@0: verify("({bar:function foo(){}})", e => e.name == "foo", [1, 15], [1, 18]); michael@0: verify("(\n{\nbar\n:\nfunction\nfoo\n(\n)\n{\n}\n}\n)", e => e.name == "foo", [6, 0], [6, 3]); michael@0: michael@0: // Just to be sure, check the identifier node "bar" as well. michael@0: verify("({bar:function foo(){}})", e => e.name == "bar", [1, 2], [1, 5]); michael@0: verify("(\n{\nbar\n:\nfunction\nfoo\n(\n)\n{\n}\n}\n)", e => e.name == "bar", [3, 0], [3, 3]); michael@0: michael@0: // MemberExpressions. michael@0: michael@0: // The location is unavailable for the identifier node "bar". michael@0: verify("foo.bar", e => e.name == "bar", [1, 4], [1, 7]); michael@0: verify("\nfoo\n.\nbar\n", e => e.name == "bar", [4, 0], [4, 3]); michael@0: michael@0: // Just to be sure, check the identifier node "foo" as well. michael@0: verify("foo.bar", e => e.name == "foo", [1, 0], [1, 3]); michael@0: verify("\nfoo\n.\nbar\n", e => e.name == "foo", [2, 0], [2, 3]); michael@0: michael@0: // VariableDeclarator michael@0: michael@0: // The location is incorrect for the identifier node "foo". michael@0: verify("let foo = bar", e => e.name == "foo", [1, 4], [1, 7]); michael@0: verify("\nlet\nfoo\n=\nbar\n", e => e.name == "foo", [3, 0], [3, 3]); michael@0: michael@0: // Just to be sure, check the identifier node "bar" as well. michael@0: verify("let foo = bar", e => e.name == "bar", [1, 10], [1, 13]); michael@0: verify("\nlet\nfoo\n=\nbar\n", e => e.name == "bar", [5, 0], [5, 3]); michael@0: michael@0: // Just to be sure, check AssignmentExpreesions as well. michael@0: verify("foo = bar", e => e.name == "foo", [1, 0], [1, 3]); michael@0: verify("\nfoo\n=\nbar\n", e => e.name == "foo", [2, 0], [2, 3]); michael@0: verify("foo = bar", e => e.name == "bar", [1, 6], [1, 9]); michael@0: verify("\nfoo\n=\nbar\n", e => e.name == "bar", [4, 0], [4, 3]); michael@0: michael@0: // LabeledStatement and ContinueStatement, because it's 1968 again michael@0: michael@0: verify("foo: bar", e => e.name == "foo", [1, 0], [1, 3]); michael@0: verify("\nfoo\n:\nbar\n", e => e.name == "foo", [2, 0], [2, 3]); michael@0: michael@0: verify("foo: for(;;) continue foo", e => e.name == "foo", [1, 22], [1, 25]); michael@0: verify("\nfoo\n:\nfor(\n;\n;\n)\ncontinue\nfoo\n", e => e.name == "foo", [9, 0], [9, 3]); michael@0: michael@0: finish(); michael@0: }