browser_dbg_parser-06.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/ */
  4. /**
  5. * Check that some potentially problematic identifier nodes have the
  6. * right location information attached.
  7. */
  8. function test() {
  9. let { Parser, ParserHelpers, SyntaxTreeVisitor } =
  10. Cu.import("resource://devtools/shared/Parser.jsm", {});
  11. function verify(source, predicate, [sline, scol], [eline, ecol]) {
  12. let ast = Parser.reflectionAPI.parse(source);
  13. let node = SyntaxTreeVisitor.filter(ast, predicate).pop();
  14. let loc = ParserHelpers.getNodeLocation(node);
  15. is(loc.start.toSource(), { line: sline, column: scol }.toSource(),
  16. "The start location was correct for the identifier in: '" + source + "'.");
  17. is(loc.end.toSource(), { line: eline, column: ecol }.toSource(),
  18. "The end location was correct for the identifier in: '" + source + "'.");
  19. }
  20. // FunctionDeclarations and FunctionExpressions.
  21. // The location is unavailable for the identifier node "foo".
  22. verify("function foo(){}", e => e.name == "foo", [1, 9], [1, 12]);
  23. verify("\nfunction\nfoo\n(\n)\n{\n}\n", e => e.name == "foo", [3, 0], [3, 3]);
  24. verify("({bar:function foo(){}})", e => e.name == "foo", [1, 15], [1, 18]);
  25. verify("(\n{\nbar\n:\nfunction\nfoo\n(\n)\n{\n}\n}\n)", e => e.name == "foo", [6, 0], [6, 3]);
  26. // Just to be sure, check the identifier node "bar" as well.
  27. verify("({bar:function foo(){}})", e => e.name == "bar", [1, 2], [1, 5]);
  28. verify("(\n{\nbar\n:\nfunction\nfoo\n(\n)\n{\n}\n}\n)", e => e.name == "bar", [3, 0], [3, 3]);
  29. // MemberExpressions.
  30. // The location is unavailable for the identifier node "bar".
  31. verify("foo.bar", e => e.name == "bar", [1, 4], [1, 7]);
  32. verify("\nfoo\n.\nbar\n", e => e.name == "bar", [4, 0], [4, 3]);
  33. // Just to be sure, check the identifier node "foo" as well.
  34. verify("foo.bar", e => e.name == "foo", [1, 0], [1, 3]);
  35. verify("\nfoo\n.\nbar\n", e => e.name == "foo", [2, 0], [2, 3]);
  36. // VariableDeclarator
  37. // The location is incorrect for the identifier node "foo".
  38. verify("let foo = bar", e => e.name == "foo", [1, 4], [1, 7]);
  39. verify("\nlet\nfoo\n=\nbar\n", e => e.name == "foo", [3, 0], [3, 3]);
  40. // Just to be sure, check the identifier node "bar" as well.
  41. verify("let foo = bar", e => e.name == "bar", [1, 10], [1, 13]);
  42. verify("\nlet\nfoo\n=\nbar\n", e => e.name == "bar", [5, 0], [5, 3]);
  43. // Just to be sure, check AssignmentExpreesions as well.
  44. verify("foo = bar", e => e.name == "foo", [1, 0], [1, 3]);
  45. verify("\nfoo\n=\nbar\n", e => e.name == "foo", [2, 0], [2, 3]);
  46. verify("foo = bar", e => e.name == "bar", [1, 6], [1, 9]);
  47. verify("\nfoo\n=\nbar\n", e => e.name == "bar", [4, 0], [4, 3]);
  48. // LabeledStatement, BreakStatement and ContinueStatement, because it's 1968 again
  49. verify("foo: bar", e => e.name == "foo", [1, 0], [1, 3]);
  50. verify("\nfoo\n:\nbar\n", e => e.name == "foo", [2, 0], [2, 3]);
  51. verify("foo: for(;;) break foo", e => e.name == "foo", [1, 19], [1, 22]);
  52. verify("\nfoo\n:\nfor(\n;\n;\n)\nbreak\nfoo\n", e => e.name == "foo", [9, 0], [9, 3]);
  53. verify("foo: bar", e => e.name == "foo", [1, 0], [1, 3]);
  54. verify("\nfoo\n:\nbar\n", e => e.name == "foo", [2, 0], [2, 3]);
  55. verify("foo: for(;;) continue foo", e => e.name == "foo", [1, 22], [1, 25]);
  56. verify("\nfoo\n:\nfor(\n;\n;\n)\ncontinue\nfoo\n", e => e.name == "foo", [9, 0], [9, 3]);
  57. finish();
  58. }