Cleanup in js modules

This commit is contained in:
Ilya Gorbunov
2015-12-29 04:03:42 +03:00
parent 80916d5ed7
commit da4b1ae0fb
23 changed files with 59 additions and 59 deletions
@@ -92,7 +92,7 @@ public class JsCallChecker(
val parserScope = JsFunctionScope(JsRootScope(JsProgram("<js checker>")), "<js fun>")
val statements = parse(code, errorReporter, parserScope)
if (statements.size() == 0) {
if (statements.size == 0) {
context.trace.report(ErrorsJs.JSCODE_NO_JAVASCRIPT_PRODUCED.on(argument))
}
} catch (e: AbortParsingException) {
@@ -153,8 +153,8 @@ private fun String.offsetOf(position: CodePosition): Int {
var lineCount = 0
var offsetInLine = 0
while (i < length()) {
val c = charAt(i)
while (i < length) {
val c = this[i]
if (lineCount == position.line && offsetInLine == position.offset) {
return i
@@ -170,7 +170,7 @@ private fun String.offsetOf(position: CodePosition): Int {
}
}
return length()
return length
}
private val KtExpression.isConstantStringLiteral: Boolean
@@ -68,7 +68,7 @@ public fun String.underlineAsText(from: Int, to: Int): String {
var lineWasMarked = false
for (i in indices) {
val c = charAt(i)
val c = this[i]
val mark: Char
mark = when (i) {
@@ -105,7 +105,7 @@ public fun String.underlineAsHtml(from: Int, to: Int): String {
val underlineEnd = "</u>"
for (i in indices) {
val c = charAt(i)
val c = this[i]
val mark = when (i) {
from -> {
@@ -74,7 +74,7 @@ internal abstract class AbstractNativeIndexerChecker(
override fun additionalCheck(declaration: KtNamedFunction, descriptor: FunctionDescriptor, diagnosticHolder: DiagnosticSink) {
val parameters = descriptor.getValueParameters()
val builtIns = descriptor.builtIns
if (parameters.size() > 0) {
if (parameters.size > 0) {
val firstParamClassDescriptor = DescriptorUtils.getClassDescriptorForType(parameters.get(0).getType())
if (firstParamClassDescriptor != builtIns.string &&
!DescriptorUtils.isSubclass(firstParamClassDescriptor, builtIns.number)
@@ -83,7 +83,7 @@ internal abstract class AbstractNativeIndexerChecker(
}
}
if (parameters.size() != requiredParametersCount) {
if (parameters.size != requiredParametersCount) {
diagnosticHolder.report(ErrorsJs.NATIVE_INDEXER_WRONG_PARAMETER_COUNT.on(declaration, requiredParametersCount, indexerKind))
}
@@ -114,7 +114,7 @@ internal class NativeSetterChecker : AbstractNativeIndexerChecker(PredefinedAnno
if (returnType == null || KotlinBuiltIns.isUnit(returnType)) return
val parameters = descriptor.getValueParameters()
if (parameters.size() < 2) return
if (parameters.size < 2) return
val secondParameterType = parameters.get(1).getType()
if (secondParameterType.isSubtypeOf(returnType)) return
@@ -440,7 +440,7 @@ private fun JsNode.withParentsOfNodes(nodes: Set<JsNode>): Set<JsNode> {
addAllUntilMatchedOrStatement(stack)
}
stack.remove(stack.lastIndex)
stack.removeAt(stack.lastIndex)
}
fun addAllUntilMatchedOrStatement(nodesOnStack: List<JsNode>) {
@@ -459,7 +459,7 @@ private fun JsNode.withParentsOfNodes(nodes: Set<JsNode>): Set<JsNode> {
}
private fun List<JsStatement>.toStatement(): JsStatement =
when (size()) {
when (size) {
0 -> JsEmpty
1 -> get(0)
else -> JsBlock(this)
@@ -84,7 +84,7 @@ private constructor(
private fun removeStatementsAfterTopReturn() {
val statements = body.getStatements()
val statementsSize = statements.size()
val statementsSize = statements.size
for (i in 0..statementsSize - 1) {
val statement = statements.get(i)
@@ -101,7 +101,7 @@ private constructor(
return
}
val returnCount = collectInstances(javaClass<JsReturn>(), body).size()
val returnCount = collectInstances(JsReturn::class.java, body).size
if (returnCount == 0) {
// TODO return Unit (KT-5647)
resultExpr = JsLiteral.UNDEFINED
@@ -114,7 +114,7 @@ private constructor(
if (lastTopLevelStatement is JsReturn) {
resultExpr = lastTopLevelStatement.getExpression()
statements.remove(statements.lastIndex)
statements.removeAt(statements.lastIndex)
return
}
}
@@ -139,7 +139,7 @@ private constructor(
val last = statements.lastOrNull() as? JsBreak
if (last?.getLabel()?.getName() === breakLabel?.getName()) {
statements.remove(statements.lastIndex)
statements.removeAt(statements.lastIndex)
}
}
@@ -175,7 +175,7 @@ private constructor(
// var a = expr1 + call() is ok, but we don't want to reuse 'a' for result,
// as it means to replace every 'return expr2' to 'a = expr1 + expr2'.
// If there is more than one return, expr1 copies are undesirable.
if (variable.initExpression !== call || vars.size() > 1) return null
if (variable.initExpression !== call || vars.size > 1) return null
val varName = variable.getName()
with (inliningContext.statementContext) {
@@ -189,7 +189,7 @@ private constructor(
private fun getArguments(): List<JsExpression> {
val arguments = call.getArguments()
if (isCallInvocation(call)) {
return arguments.subList(1, arguments.size())
return arguments.subList(1, arguments.size)
}
return arguments
@@ -257,7 +257,7 @@ private constructor(
}
private fun hasThisReference(body: JsBlock): Boolean {
val thisRefs = collectInstances(javaClass<JsLiteral.JsThisRef>(), body)
val thisRefs = collectInstances(JsLiteral.JsThisRef::class.java, body)
return !thisRefs.isEmpty()
}
@@ -268,7 +268,7 @@ private constructor(
private fun canBeExpression(body: JsBlock): Boolean {
val statements = body.getStatements()
return statements.size() == 1 && statements.get(0) is JsReturn
return statements.size == 1 && statements.get(0) is JsReturn
}
}
}
@@ -112,8 +112,8 @@ public class FunctionReader(private val context: TranslationContext) {
if (index < 0) return null
// + 1 for closing quote
var offset = index + tag.length() + 1
while (offset < source.length() && source.charAt(offset).isWhitespaceOrComma) {
var offset = index + tag.length + 1
while (offset < source.length && source[offset].isWhitespaceOrComma) {
offset++
}
@@ -37,7 +37,7 @@ public fun removeUnusedFunctionDefinitions(root: JsNode, functions: Map<JsName,
removableFunctions
}
NodeRemover(javaClass<JsPropertyInitializer>()) {
NodeRemover(JsPropertyInitializer::class.java) {
val function = it.getValueExpr() as? JsFunction
function != null && function in removable
}.accept(root)
@@ -34,7 +34,7 @@ public fun removeUnusedLocalFunctionDeclarations(root: JsNode) {
removableDeclarations
}
NodeRemover(javaClass<JsStatement>()) {
NodeRemover(JsStatement::class.java) {
it in removable
}.accept(root)
}
@@ -57,7 +57,7 @@ abstract class FunctionContext(
}
public fun declareFunctionConstructorCalls(arguments: List<JsExpression>) {
val calls = ContainerUtil.findAll<JsExpression, JsInvocation>(arguments, javaClass<JsInvocation>())
val calls = ContainerUtil.findAll<JsExpression, JsInvocation>(arguments, JsInvocation::class.java)
for (call in calls) {
val callName = getSimpleName(call)
@@ -41,7 +41,7 @@ public fun collectLocalNames(function: JsFunction): List<JsName> {
return with(NameCollector(functionScope)) {
accept(function.getBody())
names.values().toList()
names.values.toList()
}
}
@@ -35,7 +35,7 @@ public fun isFunctionCreator(outer: JsFunction): Boolean =
*/
public fun JsFunction.getInnerFunction(): JsFunction? {
val statements = getBody().getStatements()
if (statements.size() != 1) return null
if (statements.size != 1) return null
val statement = statements.get(0)
val returnExpr = (statement as? JsReturn)?.getExpression()
@@ -42,7 +42,7 @@ public fun aliasArgumentsIfNeeded(
context.replaceName(paramName, replacement)
}
val defaultParams = parameters.subList(arguments.size(), parameters.size())
val defaultParams = parameters.subList(arguments.size, parameters.size)
for (defaultParam in defaultParams) {
val paramName = defaultParam.getName()
val freshName = context.getFreshName(paramName)
@@ -85,7 +85,7 @@ public object KotlinJavascriptSerializationUtil {
public fun contentMapToByteArray(contentMap: Map<String, ByteArray>): ByteArray {
val contentBuilder = JsProtoBuf.Library.newBuilder()
contentMap.forEach {
val entry = JsProtoBuf.Library.FileEntry.newBuilder().setPath(it.getKey()).setContent(ByteString.copyFrom(it.getValue())).build()
val entry = JsProtoBuf.Library.FileEntry.newBuilder().setPath(it.key).setContent(ByteString.copyFrom(it.value)).build()
contentBuilder.addEntry(entry)
}
@@ -201,7 +201,7 @@ public object KotlinJavascriptSerializationUtil {
}
private fun getPackages(contentMap: Map<String, ByteArray>): Set<String> {
val keys = contentMap.keySet().map { (if (it.startsWith('/')) it else "/" + it).substringBeforeLast('/') }.toSet()
val keys = contentMap.keys.map { (if (it.startsWith('/')) it else "/" + it).substringBeforeLast('/') }.toSet()
val result = hashSetOf<String>()
@@ -222,7 +222,7 @@ public object KotlinJavascriptSerializationUtil {
}
public fun KotlinJavascriptMetadata.forEachFile(operation: (filePath: String, fileContent: ByteArray) -> Unit): Unit =
this.body.toContentMap().forEach { operation(it.getKey(), it.getValue()) }
this.body.toContentMap().forEach { operation(it.key, it.value) }
private fun ByteArray.toContentMap(): Map<String, ByteArray> {
val gzipInputStream = GZIPInputStream(ByteArrayInputStream(this))
@@ -75,7 +75,7 @@ public abstract class MultipleModulesTranslationTest(main: String) : BasicTest(m
val dirName = getTestName(true)
assert(dependencies != null) { "dependencies should not be null" }
for (moduleName in dependencies!!.keySet()) {
for (moduleName in dependencies!!.keys) {
if (moduleName != MAIN_MODULE_NAME) {
result.add(getOutputFilePath(getModuleDirectoryName(dirName, moduleName), ecmaVersion))
}
@@ -92,7 +92,7 @@ public abstract class MultipleModulesTranslationTest(main: String) : BasicTest(m
for (line in dependenciesTxt.readLines()) {
val split = line.split("->")
val module = split[0]
val dependencies = if (split.size() > 1) split[1] else ""
val dependencies = if (split.size > 1) split[1] else ""
val dependencyList = dependencies.split(",").filterNot { it.isEmpty() }
result[module] = dependencyList
@@ -36,7 +36,7 @@ import org.jetbrains.kotlin.util.OperatorNameConventions
import java.util.ArrayList
public fun CallArgumentTranslator.ArgumentsInfo.argsWithReceiver(receiver: JsExpression): List<JsExpression> {
val allArguments = ArrayList<JsExpression>(1 + reifiedArguments.size() + valueArguments.size())
val allArguments = ArrayList<JsExpression>(1 + reifiedArguments.size + valueArguments.size)
allArguments.addAll(reifiedArguments)
allArguments.add(receiver)
allArguments.addAll(valueArguments)
@@ -165,7 +165,7 @@ object InvokeIntrinsic : FunctionCallCase() {
val callableDescriptor = callInfo.callableDescriptor
if (callableDescriptor.getName() != OperatorNameConventions.INVOKE)
return false
val parameterCount = callableDescriptor.getValueParameters().size()
val parameterCount = callableDescriptor.getValueParameters().size
val funDeclaration = callableDescriptor.getContainingDeclaration()
val reflectionTypes = callInfo.context.getReflectionTypes()
@@ -82,7 +82,7 @@ public fun UsageTracker.getNameForCapturedDescriptor(descriptor: DeclarationDesc
public fun UsageTracker.hasCapturedExceptContaining(): Boolean {
val hasNotCaptured =
capturedDescriptorToJsName.isEmpty() ||
(capturedDescriptorToJsName.size() == 1 && capturedDescriptorToJsName.containsKey(containingDescriptor))
(capturedDescriptorToJsName.size == 1 && capturedDescriptorToJsName.containsKey(containingDescriptor))
return !hasNotCaptured
}
@@ -180,7 +180,7 @@ public class ClassTranslator private constructor(
if (supertypes.isEmpty()) {
return emptyList()
}
if (supertypes.size() == 1) {
if (supertypes.size == 1) {
val type = supertypes.get(0)
val supertypeDescriptor = getClassDescriptorForType(type)
return listOf<JsExpression>(getClassReference(supertypeDescriptor))
@@ -232,9 +232,9 @@ public class ClassTranslator private constructor(
}
private fun generateBridgesToTraitImpl(properties: MutableList<JsPropertyInitializer>) {
for (entry in CodegenUtil.getNonPrivateTraitMethods(descriptor).entrySet()) {
if (!areNamesEqual(entry.getKey(), entry.getValue())) {
properties.add(generateDelegateCall(entry.getValue(), entry.getKey(), JsLiteral.THIS, context()))
for (entry in CodegenUtil.getNonPrivateTraitMethods(descriptor).entries) {
if (!areNamesEqual(entry.key, entry.value)) {
properties.add(generateDelegateCall(entry.value, entry.key, JsLiteral.THIS, context()))
}
}
}
@@ -138,7 +138,7 @@ public class DelegationTranslator(
val jsFunction = JsFunction(context().getScopeForDescriptor(setterDescriptor.getContainingDeclaration()),
"setter for " + setterDescriptor.getName().asString())
assert(setterDescriptor.getValueParameters().size() == 1) { "Setter must have 1 parameter" }
assert(setterDescriptor.getValueParameters().size == 1) { "Setter must have 1 parameter" }
val defaultParameter = JsParameter(jsFunction.getScope().declareTemporary())
val defaultParameterRef = defaultParameter.getName().makeRef()
@@ -177,7 +177,7 @@ private class PropertyTranslator(
val containingScope = context().getScopeForDescriptor(setterDescriptor.getContainingDeclaration())
val function = JsFunction(containingScope, JsBlock(), accessorDescription(setterDescriptor))
assert(setterDescriptor.getValueParameters().size() == 1) { "Setter must have 1 parameter" }
assert(setterDescriptor.getValueParameters().size == 1) { "Setter must have 1 parameter" }
val correspondingPropertyName = setterDescriptor.getCorrespondingProperty().getName().asString()
val valueParameter = function.addParameter(correspondingPropertyName).getName()
val withAliased = context().innerContextWithAliased(setterDescriptor.getValueParameters().get(0), valueParameter.makeRef())
@@ -44,7 +44,7 @@ public class InlineMetadata(val tag: JsStringLiteral, val function: JsFunction)
if (Namer.CREATE_INLINE_FUNCTION != call.getQualifier()) return null
val arguments = call.getArguments()
if (arguments.size() != METADATA_PROPERTIES_COUNT) return null
if (arguments.size != METADATA_PROPERTIES_COUNT) return null
val tag = arguments[0] as? JsStringLiteral
val function = arguments[1] as? JsFunction
@@ -71,7 +71,7 @@ public object LongOperationFIF : FunctionIntrinsicFactory {
class BaseBinaryIntrinsic(val applyFun: (left: JsExpression, right: JsExpression) -> JsExpression) : FunctionIntrinsic() {
override fun apply(receiver: JsExpression?, arguments: List<JsExpression>, context: TranslationContext): JsExpression {
assert(receiver != null)
assert(arguments.size() == 1)
assert(arguments.size == 1)
return applyFun(receiver!!, arguments.get(0))
}
}
@@ -65,7 +65,7 @@ public object NumberAndCharConversionFIF : CompositeFIF() {
class ConversionUnaryIntrinsic(val applyFun: (receiver: JsExpression) -> JsExpression) : FunctionIntrinsic() {
override fun apply(receiver: JsExpression?, arguments: List<JsExpression>, context: TranslationContext): JsExpression {
assert(receiver != null)
assert(arguments.size() == 0)
assert(arguments.size == 0)
return applyFun(receiver!!)
}
}
@@ -61,7 +61,7 @@ public class CallArgumentTranslator private constructor(
private val isNativeFunctionCall = AnnotationsUtils.isNativeObject(resolvedCall.getCandidateDescriptor())
private fun removeLastUndefinedArguments(result: MutableList<JsExpression>) {
var i = result.size() - 1
var i = result.size - 1
while (i >= 0) {
if (result.get(i) != context().namer().getUndefinedExpression()) {
@@ -70,7 +70,7 @@ public class CallArgumentTranslator private constructor(
i--
}
result.subList(i + 1, result.size()).clear()
result.subList(i + 1, result.size).clear()
}
private fun translate(): ArgumentsInfo {
@@ -81,7 +81,7 @@ public class CallArgumentTranslator private constructor(
var hasSpreadOperator = false
var cachedReceiver: TemporaryConstVariable? = null
var result: MutableList<JsExpression> = ArrayList(valueParameters.size())
var result: MutableList<JsExpression> = ArrayList(valueParameters.size)
val valueArgumentsByIndex = resolvedCall.getValueArgumentsByIndex()
if (valueArgumentsByIndex == null) {
throw IllegalStateException("Failed to arrange value arguments by index: " + resolvedCall.getResultingDescriptor())
@@ -101,7 +101,7 @@ public class CallArgumentTranslator private constructor(
val arguments = actualArgument.getArguments()
val size = arguments.size()
val size = arguments.size
var i = 0
while (i != size) {
if (arguments.get(i).getSpreadElement() != null) {
@@ -199,7 +199,7 @@ public class CallArgumentTranslator private constructor(
}
assert(actualArgument is ExpressionValueArgument)
assert(valueArguments.size() == 1)
assert(valueArguments.size == 1)
val argumentExpression = valueArguments.get(0).getArgumentExpression()
assert(argumentExpression != null)
@@ -226,7 +226,7 @@ public class CallArgumentTranslator private constructor(
val list: MutableList<JsExpression>
if (shouldWrapVarargInArray) {
list = if (arguments.size() == 1) SmartList<JsExpression>() else ArrayList<JsExpression>(arguments.size())
list = if (arguments.size == 1) SmartList<JsExpression>() else ArrayList<JsExpression>(arguments.size)
}
else {
list = result
@@ -269,10 +269,10 @@ public class CallArgumentTranslator private constructor(
}
private fun concatArgumentsIfNeeded(concatArguments: List<JsExpression>): JsExpression {
assert(concatArguments.size() > 0) { "concatArguments.size should not be 0" }
assert(concatArguments.size > 0) { "concatArguments.size should not be 0" }
if (concatArguments.size() > 1) {
return JsInvocation(JsNameRef("concat", concatArguments.get(0)), concatArguments.subList(1, concatArguments.size()))
if (concatArguments.size > 1) {
return JsInvocation(JsNameRef("concat", concatArguments.get(0)), concatArguments.subList(1, concatArguments.size))
}
else {
@@ -281,19 +281,19 @@ public class CallArgumentTranslator private constructor(
}
private fun prepareConcatArguments(arguments: List<ValueArgument>, list: List<JsExpression>): MutableList<JsExpression> {
assert(arguments.size() != 0) { "arguments.size should not be 0" }
assert(arguments.size() == list.size()) { "arguments.size: " + arguments.size() + " != list.size: " + list.size() }
assert(arguments.size != 0) { "arguments.size should not be 0" }
assert(arguments.size == list.size) { "arguments.size: " + arguments.size + " != list.size: " + list.size }
val concatArguments = SmartList<JsExpression>()
var lastArrayContent: MutableList<JsExpression> = SmartList()
val size = arguments.size()
val size = arguments.size
for (index in 0..size - 1) {
val valueArgument = arguments.get(index)
val expressionArgument = list.get(index)
if (valueArgument.getSpreadElement() != null) {
if (lastArrayContent.size() > 0) {
if (lastArrayContent.size > 0) {
concatArguments.add(JsArrayLiteral(lastArrayContent))
concatArguments.add(expressionArgument)
lastArrayContent = SmartList<JsExpression>()
@@ -306,7 +306,7 @@ public class CallArgumentTranslator private constructor(
lastArrayContent.add(expressionArgument)
}
}
if (lastArrayContent.size() > 0) {
if (lastArrayContent.size > 0) {
concatArguments.add(JsArrayLiteral(lastArrayContent))
}
@@ -344,7 +344,7 @@ private fun Map<TypeParameterDescriptor, KotlinType>.addReifiedTypeArgsTo(
val reifiedTypeArguments = SmartList<JsExpression>()
val patternTranslator = PatternTranslator.newInstance(context)
for (param in keySet().sortedBy { it.getIndex() }) {
for (param in keys.sortedBy { it.getIndex() }) {
if (!param.isReified()) continue
val argumentType = get(param)