org.openrewrite.javascript.internal.tsc.TSCConversions Maven / Gradle / Ivy
/*
* Copyright 2023 the original author or 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
*
* https://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.openrewrite.javascript.internal.tsc;
import com.caoccao.javet.exceptions.JavetException;
import com.caoccao.javet.values.V8Value;
import com.caoccao.javet.values.primitive.*;
import com.caoccao.javet.values.reference.IV8ValueArray;
import com.caoccao.javet.values.reference.V8ValueArray;
import com.caoccao.javet.values.reference.V8ValueFunction;
import com.caoccao.javet.values.reference.V8ValueObject;
import org.openrewrite.javascript.internal.tsc.generated.TSCSignatureKind;
import org.openrewrite.javascript.internal.tsc.generated.TSCSyntaxKind;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.IntFunction;
public final class TSCConversions {
private TSCConversions() {
}
public static final TSCConversion STRING = (context, value) -> {
if (!(value instanceof V8ValueString)) {
throw new IllegalArgumentException("expected V8 string");
}
return ((V8ValueString) value).getValue();
};
public static final TSCConversion BOOLEAN = (context, value) -> {
if (value instanceof V8ValueBoolean) {
return ((V8ValueBoolean) value).getValue();
}
throw new IllegalArgumentException("expected V8 boolean");
};
public static final TSCConversion INTEGER = (context, value) -> {
if (value instanceof V8ValueInteger) {
return ((V8ValueInteger) value).getValue();
}
throw new IllegalArgumentException("expected V8 integer");
};
public static final TSCConversion LONG = (context, value) -> {
if (value instanceof V8ValueLong) {
return ((V8ValueLong) value).getValue();
} else if (value instanceof V8ValueInteger) {
return ((V8ValueInteger) value).getValue().longValue();
}
throw new IllegalArgumentException("expected V8 long or integer");
};
public static final TSCConversion INDEX_INFO = builder(TSCIndexInfo::fromJS);
public static final TSCConversion> INDEX_INFO_LIST = list(INDEX_INFO);
public static final TSCConversion SYNTAX_KIND = enumeration(TSCSyntaxKind::fromCode);
public static final TSCConversion SIGNATURE_KIND = enumeration(TSCSignatureKind::fromCode);
public static final TSCConversion TYPE = cached(context -> context.typeCache);
public static final TSCConversion> TYPE_LIST = list(TYPE);
public static final TSCConversion NODE = cached(context -> context.nodeCache);
static final TSCConversion NODE_LIST = cached(context -> context.nodeListCache);
public static final TSCConversion TYPE_NODE = cast(NODE, TSCNode.TypeNode.class);
public static final TSCConversion SYNTAX_LIST_NODE = cast(NODE, TSCSyntaxListNode.class);
public static final TSCConversion SYMBOL = cached(context -> context.symbolCache);
public static final TSCConversion> SYMBOL_LIST = list(SYMBOL);
public static final TSCConversion SIGNATURE = cached(context -> context.signatureCache);
public static final TSCConversion> SIGNATURE_LIST = list(SIGNATURE);
public static final TSCConversion