com.google.javascript.jscomp.gwt.client.Util Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of closure-compiler-linter Show documentation
Show all versions of closure-compiler-linter Show documentation
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.
/*
* 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 java.util.AbstractList;
import java.util.List;
import jsinterop.annotations.JsMethod;
import jsinterop.annotations.JsOverlay;
import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsProperty;
import jsinterop.annotations.JsType;
import jsinterop.base.Js;
import jsinterop.base.JsArrayLike;
import jsinterop.base.JsPropertyMap;
/**
* GWT/J2CL utilities to abstract out JS interop.
*/
public class Util {
/** Wraps native RegExp. */
@JsType(isNative = true, name = "RegExp", namespace = JsPackage.GLOBAL)
public static class JsRegExp {
public JsRegExp(String regex, String flags) {}
@JsProperty
public native int getLastIndex();
@JsProperty
public native boolean getIgnoreCase();
@JsProperty
public native boolean getGlobal();
@JsProperty
public native boolean getMultiline();
@JsProperty
public native String getSource();
public native boolean test(String str);
public native Match exec(String str);
/** Return type for RegExp.prototype.exec. */
@JsType(isNative = true)
public static class Match extends JsArray {
protected Match() {}
@JsProperty
public native int getIndex();
@JsProperty
public native String getInput();
}
}
/** Wraps native Object. */
@JsType(isNative = true, name = "Object", namespace = JsPackage.GLOBAL)
public static class JsObject {
public JsObject() {}
@JsOverlay
public final T get(String key) {
return asPropertyMap().get(key);
}
@JsOverlay
public final JsObject set(String key, T value) {
asPropertyMap().set(key, value);
return this;
}
@JsOverlay
private final JsPropertyMap asPropertyMap() {
return Js.uncheckedCast(this);
}
}
/** Wraps native Array. */
@JsType(isNative = true, name = "Array", namespace = JsPackage.GLOBAL)
public static class JsArray extends JsObject {
public JsArray() {}
@JsProperty
public native int getLength();
@JsProperty
public native void setLength(int length);
public native void push(T obj);
public native JsArray slice();
public native JsArray slice(int start);
public native JsArray slice(int start, int end);
@SuppressWarnings("unusable-by-js")
public native JsArray splice(int start, int end, T... elems);
@JsOverlay
public final T get(int i) {
return asArrayLike().getAt(i);
}
@JsOverlay
public final void set(int i, T value) {
asArrayLike().setAt(i, value);
}
@JsOverlay
public final List asList() {
return new JsArrayList<>(this);
}
@JsOverlay
private final JsArrayLike asArrayLike() {
return Js.uncheckedCast(this);
}
@JsOverlay
public static JsArray of(T... elems) {
return slice(elems);
}
@SuppressWarnings("unusable-by-js")
@JsMethod(name = "call", namespace = "Array.prototype.slice")
public static native JsArray slice(T[] elems);
@JsOverlay
public static JsArray copyOf(Iterable extends T> elems) {
JsArray arr = of();
for (T elem : elems) {
arr.push(elem);
}
return arr;
}
/** List implementation for {@link JsArray}. */
private static class JsArrayList extends AbstractList {
final JsArray array;
JsArrayList(JsArray array) {
this.array = array;
}
void checkBounds(int index) {
if (index < 0 || index >= array.getLength()) {
throw new IndexOutOfBoundsException();
}
}
@Override public T get(int index) {
checkBounds(index);
return array.get(index);
}
@Override public T set(int index, T elem) {
checkBounds(index);
T prev = array.get(index);
array.set(index, elem);
return prev;
}
@Override public T remove(int index) {
checkBounds(index);
T prev = array.get(index);
array.splice(index, 1);
return prev;
}
@Override public boolean add(T elem) {
array.push(elem);
return true;
}
@Override public int size() {
return array.getLength();
}
}
}
private Util() {}
}