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

com.google.javascript.jscomp.gwt.client.JsfileParserMain 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.gwt.client;

import com.google.common.collect.Multimap;
import com.google.javascript.jscomp.deps.JsFileFullParser;
import elemental2.core.JsArray;
import java.util.Map;
import javax.annotation.Nullable;
import jsinterop.annotations.JsFunction;
import jsinterop.annotations.JsMethod;
import jsinterop.base.JsPropertyMap;

/**
 * GWT module to parse files for dependency and {@literal @}{@code fileoverview} annotation
 * information.
 */
public class JsfileParserMain {
  /**
   * Exports the {@link #gjd} method via JSNI.
   *
   * 

This will be placed on {@code module.exports.gjd} or the global {@code jscomp.gjd}. */ public static native void exportGjd() /*-{ var fn = $entry(@com.google.javascript.jscomp.gwt.client.JsfileParserMain::gjd(*)); if (typeof module !== 'undefined' && module.exports) { module.exports.gjd = fn; } }-*/; /** * Method exported to JS to parse a file for dependencies and annotations. * *

The result is a JSON object: * *

 {@code {
   *   "custom_annotations": {?Array<[string, string]>},  @.*
   *   "goog": {?bool},  whether 'goog' is implicitly required
   *   "has_soy_delcalls": {?Array},  @fileoverview @hassoydelcall {.*}
   *   "has_soy_deltemplates": {?Array},  @fileoverview @hassoydeltemplate {.*}
   *   "imported_modules": {?Array},  import ... from .*
   *   "is_config": {?bool},  @fileoverview @config
   *   "is_externs": {?bool},  @fileoverview @externs
   *   "load_flags": {?Array<[string, string]>},
   *   "mod_name": {?Array},  @fileoverview @modName .*, @modName {.*}
   *   "mods": {?Array},  @fileoverview @mods {.*}
   *   "provide_goog": {?bool},  @fileoverview @provideGoog
   *   "provides": {?Array},
   *   "requires": {?Array},  note: look for goog.* for 'goog'
   *   "requires_css": {?Array},  @fileoverview @requirecss {.*}
   *   "testonly": {?bool},  goog.setTestOnly
   *   "type_requires": {?Array},
   *   "visibility: {?Array},  @fileoverview @visibility {.*}
   * }}
* *

Any trivial values are omitted. */ @JsMethod public static JsPropertyMap gjd( String code, String filename, @Nullable Reporter reporter) { JsFileFullParser.FileInfo info = JsFileFullParser.parse(code, filename, adaptReporter(reporter)); if (info.provideGoog) { info.provides.add("goog"); } else if (info.goog) { info.requires.add("goog"); } return new SparseObject() .set("custom_annotations", info.customAnnotations) .set("goog", info.goog) .set("has_soy_delcalls", info.hasSoyDelcalls) .set("has_soy_deltemplates", info.hasSoyDeltemplates) .set("imported_modules", info.importedModules) .set("is_config", info.isConfig) .set("is_externs", info.isExterns) .set("load_flags", info.loadFlags) .set("modName", info.modName) .set("mods", info.mods) .set("provide_goog", info.provideGoog) .set("provides", info.provides) .set("requires", info.requires) .set("requiresCss", info.requiresCss) .set("testonly", info.testonly) .set("type_requires", info.typeRequires) .set("visibility", info.visibility) .object; } /** JS function interface for reporting errors. */ @JsFunction public interface Reporter { void report(boolean fatal, String message, String sourceName, int line, int lineOffset); } private static JsFileFullParser.Reporter adaptReporter(@Nullable Reporter r) { if (r == null) { return null; } return (fatal, message, sourceName, line, lineOffset) -> { r.report(fatal, message, sourceName, line, lineOffset); }; } /** Sparse object helper class: only adds non-trivial values. */ private static class SparseObject { final JsPropertyMap object = JsPropertyMap.of(); SparseObject set(String key, Iterable iterable) { JsArray array = new JsArray<>(); for (String s : iterable) { array.push(s); } if (array.getLength() > 0) { object.set(key, array); } return this; } SparseObject set(String key, Multimap map) { JsArray> array = new JsArray<>(); for (Map.Entry entry : map.entries()) { array.push(new JsArray<>(entry.getKey(), entry.getValue())); } if (array.getLength() > 0) { object.set(key, array); } return this; } SparseObject set(String key, String value) { if (value != null && !value.isEmpty()) { object.set(key, value); } return this; } SparseObject set(String key, boolean value) { if (value) { object.set(key, value); } return this; } } private JsfileParserMain() {} }