NamingScope.java 3.2 KB
Newer Older
P
Pavel Talanov 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jetbrains.k2js.translate.context;

import com.google.dart.compiler.backend.js.ast.JsName;
import com.google.dart.compiler.backend.js.ast.JsScope;
import org.jetbrains.annotations.NotNull;

P
pTalanov 已提交
23 24
import java.util.Iterator;

P
Pavel Talanov 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/**
 * @author Pavel Talanov
 *         <p/>
 *         Basically a wrapper around JsScope.
 */
public final class NamingScope {

    @NotNull
    public static NamingScope rootScope(@NotNull JsScope rootScope) {
        return new NamingScope(rootScope);
    }

    @NotNull
    private final JsScope scope;

    private NamingScope(@NotNull JsScope correspondingScope) {
        this.scope = correspondingScope;
    }

    @NotNull
    public NamingScope innerScope(@NotNull String scopeName) {
        JsScope innerJsScope = new JsScope(jsScope(), scopeName);
P
pTalanov 已提交
47
        return innerScope(innerJsScope);
P
Pavel Talanov 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    }

    @NotNull
    public NamingScope innerScope(@NotNull JsScope correspondingScope) {
        return new NamingScope(correspondingScope);
    }

    @NotNull
        /*package*/ JsName declareUnobfuscatableName(@NotNull String name) {
        JsName declaredName = scope.declareName(name);
        declaredName.setObfuscatable(false);
        return declaredName;
    }

    @NotNull
        /*package*/ JsName declareObfuscatableName(@NotNull String name) {
        return scope.declareName(mayBeObfuscateName(name, true));
    }

    //TODO: temporary solution
    @NotNull
    private String mayBeObfuscateName(@NotNull String name, boolean shouldObfuscate) {
        if (!shouldObfuscate) {
            return name;
        }
        return doObfuscate(name);
    }

P
pTalanov 已提交
76
    //TODO: refactor
P
Pavel Talanov 已提交
77 78 79 80 81 82
    @NotNull
    private String doObfuscate(@NotNull String name) {
        int obfuscate = 0;
        String result = name;
        while (true) {
            JsName existingNameWithSameIdent = scope.findExistingName(result);
P
pTalanov 已提交
83
            boolean isDuplicate = (existingNameWithSameIdent != null) && ownsName(scope, existingNameWithSameIdent);
P
Pavel Talanov 已提交
84 85 86 87 88 89 90 91 92

            if (!isDuplicate) break;

            result = name + "$" + obfuscate;
            obfuscate++;
        }
        return result;
    }

P
pTalanov 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105
    //TODO: util?
    private static boolean ownsName(@NotNull JsScope scope, @NotNull JsName name) {
        Iterator<JsName> nameIterator = scope.getAllNames();
        while (nameIterator.hasNext()) {
            if (nameIterator.next() == name) {
                return true;
            }
        }
        return false;
    }


    @NotNull
P
Pavel Talanov 已提交
106 107 108 109 110 111 112 113 114
    public JsName declareTemporary() {
        return scope.declareTemporary();
    }

    @NotNull
    public JsScope jsScope() {
        return scope;
    }
}