1 | // Copyright (C) 2008 Google Inc. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package com.google.caja.parser.quasiliteral; |
16 | |
17 | import com.google.caja.parser.ParseTreeNode; |
18 | import com.google.caja.parser.js.Identifier; |
19 | import com.google.caja.parser.js.SyntheticNodes; |
20 | import com.google.caja.reporting.MessageQueue; |
21 | |
22 | /** |
23 | * Checks for non-{@link SyntheticNodes#SYNTHETIC synthetic} identifiers in the |
24 | * reserved namespace. |
25 | * |
26 | * @author ihab.awad@gmail.com (Ihab Awad) |
27 | */ |
28 | @RulesetDescription( |
29 | name="IlegalReferenceCheckRewriter", |
30 | synopsis="Checks that any reserved references are synthetic") |
31 | public class IllegalReferenceCheckRewriter extends Rewriter { |
32 | final public Rule[] cajaRules = { |
33 | new Rule () { |
34 | @Override |
35 | @RuleDescription( |
36 | name="identifierUnderscores", |
37 | synopsis="Check that any identifier ending with '__' is synthetic", |
38 | reason="Double underscore identifiers may not be mentioned by Caja " |
39 | + "code") |
40 | public ParseTreeNode fire(ParseTreeNode node, Scope scope) { |
41 | if (node instanceof Identifier |
42 | && !node.getAttributes().is(SyntheticNodes.SYNTHETIC)) { |
43 | String name = ((Identifier)node).getValue(); |
44 | if (name != null && name.endsWith("__")) { |
45 | mq.addMessage( |
46 | RewriterMessageType.ILLEGAL_IDENTIFIER_LEFT_OVER, |
47 | node.getFilePosition(), node); |
48 | return node; |
49 | } |
50 | } |
51 | return NONE; |
52 | } |
53 | }, |
54 | |
55 | new Rule () { |
56 | @Override |
57 | @RuleDescription( |
58 | name="recurse", |
59 | synopsis="Recurse into children", |
60 | reason="") |
61 | public ParseTreeNode fire(ParseTreeNode node, Scope scope) { |
62 | return expandAll(node, scope); |
63 | } |
64 | }, |
65 | }; |
66 | |
67 | public IllegalReferenceCheckRewriter(MessageQueue mq) { |
68 | this(mq, true); |
69 | } |
70 | |
71 | public IllegalReferenceCheckRewriter(MessageQueue mq, boolean logging) { |
72 | super(mq, false, logging); |
73 | addRules(cajaRules); |
74 | } |
75 | } |