01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package com.google.gwt.dev.js;
17:
18: import com.google.gwt.dev.js.ast.JsName;
19: import com.google.gwt.dev.js.ast.JsProgram;
20: import com.google.gwt.dev.js.ast.JsRootScope;
21: import com.google.gwt.dev.js.ast.JsScope;
22:
23: import java.util.Iterator;
24: import java.util.List;
25:
26: /**
27: * A namer that uses long, fully qualified names for maximum unambiguous
28: * debuggability.
29: */
30: public class JsVerboseNamer {
31:
32: public static void exec(JsProgram program) {
33: new JsVerboseNamer(program).execImpl();
34: }
35:
36: private final JsProgram program;
37:
38: public JsVerboseNamer(JsProgram program) {
39: this .program = program;
40: }
41:
42: private void execImpl() {
43: visit(program.getRootScope());
44: }
45:
46: private boolean isLegal(String newIdent) {
47: // only keywords are forbidden
48: return !JsKeywords.isKeyword(newIdent);
49: }
50:
51: private void visit(JsScope scope) {
52: // Visit children.
53: List<JsScope> children = scope.getChildren();
54: for (Iterator<JsScope> it = children.iterator(); it.hasNext();) {
55: visit(it.next());
56: }
57:
58: JsRootScope rootScope = program.getRootScope();
59: if (scope == rootScope) {
60: return;
61: }
62:
63: // Visit all my idents.
64: for (Iterator<JsName> it = scope.getAllNames(); it.hasNext();) {
65: JsName name = it.next();
66: if (!name.isObfuscatable()) {
67: // Unobfuscatable names become themselves.
68: name.setShortIdent(name.getIdent());
69: continue;
70: }
71:
72: String fullIdent = name.getIdent();
73: if (!isLegal(fullIdent)) {
74: String checkIdent = fullIdent;
75: for (int i = 0; true; ++i) {
76: checkIdent = fullIdent + "_" + i;
77: if (isLegal(checkIdent)) {
78: break;
79: }
80: }
81: name.setShortIdent(checkIdent);
82: } else {
83: // set each name's short ident to its full ident
84: name.setShortIdent(fullIdent);
85: }
86: }
87: }
88: }
|