diff --git a/js/js.inliner/src/org/jetbrains/k2js/inline/FunctionCollector.java b/js/js.inliner/src/org/jetbrains/k2js/inline/FunctionCollector.java new file mode 100644 index 00000000000..782d3511dfc --- /dev/null +++ b/js/js.inliner/src/org/jetbrains/k2js/inline/FunctionCollector.java @@ -0,0 +1,47 @@ +/* + * Copyright 2010-2014 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.inline; + +import com.google.dart.compiler.backend.js.ast.*; + +import java.util.IdentityHashMap; + +public class FunctionCollector extends RecursiveJsVisitor { + + private final IdentityHashMap functions = new IdentityHashMap(); + + public static IdentityHashMap collectFunctions(JsNode root) { + FunctionCollector collector = new FunctionCollector(); + collector.accept(root); + + return collector.functions; + } + + @Override + public void visitPropertyInitializer(JsPropertyInitializer x) { + super.visitPropertyInitializer(x); + + JsExpression label = x.getLabelExpr(); + JsExpression value = x.getValueExpr(); + + if (label instanceof JsNameRef && value instanceof JsFunction) { + JsName name = ((JsNameRef) label).getName(); + JsFunction function = (JsFunction) value; + functions.put(name, function); + } + } +} diff --git a/js/js.inliner/src/org/jetbrains/k2js/inline/JsInliner.java b/js/js.inliner/src/org/jetbrains/k2js/inline/JsInliner.java index da2a591e6fc..db275430cad 100644 --- a/js/js.inliner/src/org/jetbrains/k2js/inline/JsInliner.java +++ b/js/js.inliner/src/org/jetbrains/k2js/inline/JsInliner.java @@ -31,7 +31,7 @@ public class JsInliner extends JsVisitorWithContextImpl { private final JsProgram program; public static JsProgram process(JsProgram program) { - IdentityHashMap functions = new IdentityHashMap(); + IdentityHashMap functions = FunctionCollector.collectFunctions(program); JsInliner inliner = new JsInliner(program, functions); return inliner.process(); }