All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.javascript.jscomp.debugger.gwt.DebuggerGwtMain Maven / Gradle / Ivy

Go to download

Closure Compiler is a JavaScript optimizing compiler. It parses your JavaScript, analyzes it, removes dead code and rewrites and minimizes what's left. It also checks syntax, variable references, and types, and warns about common JavaScript pitfalls. It is used in many of Google's JavaScript apps, including Gmail, Google Web Search, Google Maps, and Google Docs. This binary checks for style issues such as incorrect or missing JSDoc usage, and missing goog.require() statements. It does not do more advanced checks such as typechecking.

There is a newer version: v20200830
Show newest version
/*
 * Copyright 2015 The Closure Compiler Authors.
 *
 * 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 com.google.javascript.jscomp.debugger.gwt;

import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.CellPanel;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.javascript.jscomp.Compiler;
import com.google.javascript.jscomp.CompilerOptions;
import com.google.javascript.jscomp.Result;
import com.google.javascript.jscomp.SourceFile;
import com.google.javascript.jscomp.debugger.common.CompilationParam;

/**
 * A GWT-based version of the Closure Compiler debugger
 *
 * @author [email protected] (Michael Zhou)
 * @author [email protected] (Ben Lickly)
 */
public class DebuggerGwtMain implements EntryPoint {
  final CompilerOptions options = new CompilerOptions();
  final TextArea externs = new TextArea();
  final TextArea input0 = new TextArea();
  final VerticalPanel rightPane = new VerticalPanel();

  private void doCompile() {
    SourceFile externFile = SourceFile.fromCode("externs", externs.getValue());
    SourceFile srcFile = SourceFile.fromCode("input0", input0.getValue());
    Compiler compiler = new Compiler();
    try {
      Result result = compiler.compile(externFile, srcFile, options);
      updateUi(compiler, result);
    } catch (Exception e) {
      updateUiException(e);
    }
  }

  private void updateUi(Compiler compiler, Result result) {
    rightPane.clear();
    rightPane.add(new HTML("

Output

")); String outputCode = compiler.toSource(); rightPane.add(new Label(outputCode)); rightPane.add(new HTML("

Warnings

")); rightPane.add( new Label(Joiner.on("\n\n").join(Iterables.concat(result.errors, result.warnings)))); rightPane.add(new HTML("

AST

")); String outputAst = compiler.getRoot().toStringTree(); rightPane.add(new Label(outputAst)); } private void updateUiException(Exception e) { rightPane.clear(); rightPane.add(new HTML("

Exception

")); rightPane.add(new Label(e.toString())); } @Override public void onModuleLoad() { externs.setCharacterWidth(80); externs.setVisibleLines(5); externs.setText("function Symbol() {}\n"); externs.addKeyUpHandler( new KeyUpHandler() { @Override public void onKeyUp(KeyUpEvent event) { doCompile(); } }); input0.setCharacterWidth(80); input0.setVisibleLines(25); input0.addKeyUpHandler( new KeyUpHandler() { @Override public void onKeyUp(KeyUpEvent event) { doCompile(); } }); HorizontalPanel panel = new HorizontalPanel(); VerticalPanel leftPane = new VerticalPanel(); leftPane.add(new HTML("

Externs

")); leftPane.add(externs); leftPane.add(new HTML("

Input

")); leftPane.add(input0); leftPane.add(new HTML("

Options

")); createCheckboxes(leftPane); panel.add(leftPane); panel.add(rightPane); RootPanel.get().add(panel); } private void createCheckboxes(CellPanel checkboxPanel) { for (CompilationParam.ParamGroup group : CompilationParam.ParamGroup.values()) { SafeHtmlBuilder builder = new SafeHtmlBuilder(); builder.appendHtmlConstant(""); builder.appendEscaped(group.name); builder.appendHtmlConstant(""); checkboxPanel.add(new HTML(builder.toSafeHtml())); for (final CompilationParam param : CompilationParam.getGroupedSortedValues().get(group)) { CheckBox cb = new CheckBox(param.toString()); if (param.getJavaInfo() != null) { cb.setTitle("Java API equivalent: " + param.getJavaInfo()); } cb.setValue(param.getDefaultValue()); param.apply(options, param.getDefaultValue()); cb.addClickHandler( new ClickHandler() { @Override public void onClick(ClickEvent event) { boolean checked = ((CheckBox) event.getSource()).getValue(); param.apply(options, checked); doCompile(); } }); checkboxPanel.add(cb); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy