JS: lazy load special functions and source maps

This commit is contained in:
Anton Bannykh
2019-04-11 16:15:24 +03:00
parent a3aca662c1
commit fbd59ba68a
@@ -74,16 +74,22 @@ class FunctionReader(
val fileContent: String, val fileContent: String,
val moduleVariable: String, val moduleVariable: String,
val kotlinVariable: String, val kotlinVariable: String,
val specialFunctions: Map<String, SpecialFunction>, specialFunctionsProvider: () -> Map<String, SpecialFunction>,
offsetToSourceMappingProvider: () -> OffsetToSourceMapping, offsetToSourceMappingProvider: () -> OffsetToSourceMapping,
val sourceMap: SourceMap?, sourceMapProvider: () -> SourceMap?,
val outputDir: File? val outputDir: File?
) { ) {
val specialFunctions: Map<String, SpecialFunction> by lazy(specialFunctionsProvider)
val offsetToSourceMapping by lazy(offsetToSourceMappingProvider) val offsetToSourceMapping by lazy(offsetToSourceMappingProvider)
val wrapFunctionRegex = specialFunctions.entries val sourceMap: SourceMap? by lazy(sourceMapProvider)
.singleOrNull { (_, v) -> v == SpecialFunction.WRAP_FUNCTION }?.key
?.let { Regex("\\s*$it\\s*\\(\\s*").toPattern() } val wrapFunctionRegex by lazy {
specialFunctions.entries
.singleOrNull { (_, v) -> v == SpecialFunction.WRAP_FUNCTION }?.key
?.let { Regex("\\s*$it\\s*\\(\\s*").toPattern() }
}
} }
private val moduleNameToInfo by lazy { private val moduleNameToInfo by lazy {
@@ -105,21 +111,26 @@ class FunctionReader(
val moduleVariable = preciseMatcher.group(4) val moduleVariable = preciseMatcher.group(4)
val kotlinVariable = preciseMatcher.group(1) val kotlinVariable = preciseMatcher.group(1)
val matcher = SPECIAL_FUNCTION_PATTERN.matcher(content) val specialFunctionsProvider = {
val specialFunctions = mutableMapOf<String, SpecialFunction>() val matcher = SPECIAL_FUNCTION_PATTERN.matcher(content)
while (matcher.find()) { val specialFunctions = mutableMapOf<String, SpecialFunction>()
if (matcher.group(2) == kotlinVariable) { while (matcher.find()) {
specialFunctions[matcher.group(1)] = specialFunctionsByName[matcher.group(3)]!! if (matcher.group(2) == kotlinVariable) {
specialFunctions[matcher.group(1)] = specialFunctionsByName[matcher.group(3)]!!
}
} }
specialFunctions
} }
val sourceMap = sourceMapContent?.let { val sourceMapProvider = {
val sourceMapResult = SourceMapParser.parse(it) sourceMapContent?.let {
when (sourceMapResult) { val sourceMapResult = SourceMapParser.parse(it)
is SourceMapSuccess -> sourceMapResult.value when (sourceMapResult) {
is SourceMapError -> { is SourceMapSuccess -> sourceMapResult.value
reporter.warning("Error parsing source map file for $path: ${sourceMapResult.message}") is SourceMapError -> {
null reporter.warning("Error parsing source map file for $path: ${sourceMapResult.message}")
null
}
} }
} }
} }
@@ -129,9 +140,9 @@ class FunctionReader(
fileContent = content, fileContent = content,
moduleVariable = moduleVariable, moduleVariable = moduleVariable,
kotlinVariable = kotlinVariable, kotlinVariable = kotlinVariable,
specialFunctions = specialFunctions, specialFunctionsProvider = specialFunctionsProvider,
offsetToSourceMappingProvider = { OffsetToSourceMapping(content) }, offsetToSourceMappingProvider = { OffsetToSourceMapping(content) },
sourceMap = sourceMap, sourceMapProvider = sourceMapProvider,
outputDir = file?.parentFile outputDir = file?.parentFile
) )