Exemplo n.º 1
0
  public void testParseObjectLiteral3() {
    environment.setLanguageVersion(Context.VERSION_1_8);
    environment.setReservedKeywordAsIdentifier(true);
    parse("var {get} = {get:1};");

    environment.setReservedKeywordAsIdentifier(false);
    parse("var {get} = {get:1};");
    expectParseErrors("var {get} = {if:1};", new String[] {"invalid property id"});
  }
Exemplo n.º 2
0
  public void testParseObjectLiteral2() {
    // keywords, fail
    environment.setReservedKeywordAsIdentifier(false);
    expectParseErrors("({function:1});", new String[] {"invalid property id"});

    environment.setReservedKeywordAsIdentifier(true);

    // keywords ok
    parse("({function:1});");
  }
Exemplo n.º 3
0
  public void testParseKeywordPropertyAccess() {
    environment.setReservedKeywordAsIdentifier(true);

    // keywords ok
    parse("({function:1}).function;");

    // reserved words ok.
    parse("({import:1}).import;");
  }
Exemplo n.º 4
0
  public void testParseObjectLiteral1() {
    environment.setReservedKeywordAsIdentifier(true);

    parse("({a:1});");
    parse("({'a':1});");
    parse("({0:1});");

    // property getter and setter definitions accept string and number
    parse("({get a() {return 1}});");
    parse("({get 'a'() {return 1}});");
    parse("({get 0() {return 1}});");

    parse("({set a(a) {return 1}});");
    parse("({set 'a'(a) {return 1}});");
    parse("({set 0(a) {return 1}});");

    // keywords ok
    parse("({function:1});");
    // reserved words ok
    parse("({float:1});");
  }
Exemplo n.º 5
0
  @Override
  public void filter(Reader reader, Writer writer) {
    try {
      CompilerEnvirons config = new CompilerEnvirons();
      // 'float' is otherwise considered a reserved keyword (usage: element.style.float = 'left').
      config.setReservedKeywordAsIdentifier(true);
      // Force the parser to build the parent scope chain.
      config.setIdeMode(true);
      Parser parser = new Parser(config);
      AstRoot root = parser.parse(reader, null, 0);

      // Filter the AST.
      for (NodeVisitor filter : filters) {
        root.visit(filter);
      }

      // Back to source.
      writer.write(root.toSource());
    } catch (IOException e) {
      LOGGER.error("Failed to rewrite JavaScript code.", e);
    }
  }