browser/devtools/debugger/test/browser_dbg_parser-06.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-06.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,72 @@
     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 + * Check that some potentially problematic identifier nodes have the
     1.9 + * right location information attached.
    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, [sline, scol], [eline, ecol]) {
    1.17 +    let ast = Parser.reflectionAPI.parse(source);
    1.18 +    let node = SyntaxTreeVisitor.filter(ast, predicate).pop();
    1.19 +    let loc = ParserHelpers.getNodeLocation(node);
    1.20 +
    1.21 +    is(loc.start.toSource(), { line: sline, column: scol }.toSource(),
    1.22 +      "The start location was correct for the identifier in: '" + source + "'.");
    1.23 +    is(loc.end.toSource(), { line: eline, column: ecol }.toSource(),
    1.24 +      "The end location was correct for the identifier in: '" + source + "'.");
    1.25 +  }
    1.26 +
    1.27 +  // FunctionDeclarations and FunctionExpressions.
    1.28 +
    1.29 +  // The location is unavailable for the identifier node "foo".
    1.30 +  verify("function foo(){}", e => e.name == "foo", [1, 9], [1, 12]);
    1.31 +  verify("\nfunction\nfoo\n(\n)\n{\n}\n", e => e.name == "foo", [3, 0], [3, 3]);
    1.32 +
    1.33 +  verify("({bar:function foo(){}})", e => e.name == "foo", [1, 15], [1, 18]);
    1.34 +  verify("(\n{\nbar\n:\nfunction\nfoo\n(\n)\n{\n}\n}\n)", e => e.name == "foo", [6, 0], [6, 3]);
    1.35 +
    1.36 +  // Just to be sure, check the identifier node "bar" as well.
    1.37 +  verify("({bar:function foo(){}})", e => e.name == "bar", [1, 2], [1, 5]);
    1.38 +  verify("(\n{\nbar\n:\nfunction\nfoo\n(\n)\n{\n}\n}\n)", e => e.name == "bar", [3, 0], [3, 3]);
    1.39 +
    1.40 +  // MemberExpressions.
    1.41 +
    1.42 +  // The location is unavailable for the identifier node "bar".
    1.43 +  verify("foo.bar", e => e.name == "bar", [1, 4], [1, 7]);
    1.44 +  verify("\nfoo\n.\nbar\n", e => e.name == "bar", [4, 0], [4, 3]);
    1.45 +
    1.46 +  // Just to be sure, check the identifier node "foo" as well.
    1.47 +  verify("foo.bar", e => e.name == "foo", [1, 0], [1, 3]);
    1.48 +  verify("\nfoo\n.\nbar\n", e => e.name == "foo", [2, 0], [2, 3]);
    1.49 +
    1.50 +  // VariableDeclarator
    1.51 +
    1.52 +  // The location is incorrect for the identifier node "foo".
    1.53 +  verify("let foo = bar", e => e.name == "foo", [1, 4], [1, 7]);
    1.54 +  verify("\nlet\nfoo\n=\nbar\n", e => e.name == "foo", [3, 0], [3, 3]);
    1.55 +
    1.56 +  // Just to be sure, check the identifier node "bar" as well.
    1.57 +  verify("let foo = bar", e => e.name == "bar", [1, 10], [1, 13]);
    1.58 +  verify("\nlet\nfoo\n=\nbar\n", e => e.name == "bar", [5, 0], [5, 3]);
    1.59 +
    1.60 +  // Just to be sure, check AssignmentExpreesions as well.
    1.61 +  verify("foo = bar", e => e.name == "foo", [1, 0], [1, 3]);
    1.62 +  verify("\nfoo\n=\nbar\n", e => e.name == "foo", [2, 0], [2, 3]);
    1.63 +  verify("foo = bar", e => e.name == "bar", [1, 6], [1, 9]);
    1.64 +  verify("\nfoo\n=\nbar\n", e => e.name == "bar", [4, 0], [4, 3]);
    1.65 +
    1.66 +  // LabeledStatement and ContinueStatement, because it's 1968 again
    1.67 +
    1.68 +  verify("foo: bar", e => e.name == "foo", [1, 0], [1, 3]);
    1.69 +  verify("\nfoo\n:\nbar\n", e => e.name == "foo", [2, 0], [2, 3]);
    1.70 +
    1.71 +  verify("foo: for(;;) continue foo", e => e.name == "foo", [1, 22], [1, 25]);
    1.72 +  verify("\nfoo\n:\nfor(\n;\n;\n)\ncontinue\nfoo\n", e => e.name == "foo", [9, 0], [9, 3]);
    1.73 +
    1.74 +  finish();
    1.75 +}

mercurial