Skip to content

Commit

Permalink
Always use ReadonlyArray for arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Vampire authored and sgrishchenko committed Sep 23, 2024
1 parent 065cea6 commit c5a2365
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 44 deletions.
22 changes: 18 additions & 4 deletions src/converter/plugins/convertArrayType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,24 @@ import {createSimplePlugin} from "../plugin.js";
import {CheckCoverageService, checkCoverageServiceKey} from "./CheckCoveragePlugin.js";

export const convertArrayType = createSimplePlugin((node, context, render) => {
if (!ts.isArrayTypeNode(node)) return null
if (
ts.isTypeOperatorNode(node) &&
node.operator === ts.SyntaxKind.ReadonlyKeyword &&
ts.isArrayTypeNode(node.type)
) {
const checkCoverageService = context.lookupService<CheckCoverageService>(checkCoverageServiceKey)
checkCoverageService?.cover(node)
checkCoverageService?.cover(node.type)

const checkCoverageService = context.lookupService<CheckCoverageService>(checkCoverageServiceKey)
checkCoverageService?.cover(node)
return `js.array.ReadonlyArray<${render(node.type.elementType)}>`
}

return `Array<${render(node.elementType)}>`
if (ts.isArrayTypeNode(node)) {
const checkCoverageService = context.lookupService<CheckCoverageService>(checkCoverageServiceKey)
checkCoverageService?.cover(node)

return `js.array.ReadonlyArray<${render(node.elementType)}>`
}

return null
})
22 changes: 0 additions & 22 deletions src/converter/plugins/convertReadonlyArrayType.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/defaultPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {convertConstructorDeclaration} from "./converter/plugins/convertConstruc
import {convertFunctionType} from "./converter/plugins/convertFunctionType.js";
import {convertLiteralType} from "./converter/plugins/convertLiteralType.js";
import {convertArrayType} from "./converter/plugins/convertArrayType.js";
import {convertReadonlyArrayType} from "./converter/plugins/convertReadonlyArrayType.js";
import {convertTypeAliasDeclaration} from "./converter/plugins/convertTypeAliasDeclaration.js";
import {convertEnumDeclaration} from "./converter/plugins/convertEnumDeclaration.js";
import {convertEnumMember} from "./converter/plugins/convertEnumMember.js";
Expand Down Expand Up @@ -139,7 +138,6 @@ export const createPlugins = (
convertFunctionType,
convertLiteralType,
convertArrayType,
convertReadonlyArrayType,
convertTypeAliasDeclaration,
convertEnumDeclaration,
convertEnumMember,
Expand Down
2 changes: 1 addition & 1 deletion test/functional/base/generated/array/simple.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

package sandbox.base.array

typealias SimpleArray = Array<String>
typealias SimpleArray = js.array.ReadonlyArray<String>

typealias SimpleReadonlyArray = js.array.ReadonlyArray<String>
2 changes: 1 addition & 1 deletion test/functional/base/generated/function/vararg.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ package sandbox.base.function

external fun simpleVararg(vararg args: String): Unit

external fun <T : Array<String>> genericVararg(vararg args: Any? /* T */): Unit
external fun <T : js.array.ReadonlyArray<String>> genericVararg(vararg args: Any? /* T */): Unit

external fun callbackVararg(fn: Function<Unit> /* (...args: string[]) => void */): Unit
4 changes: 2 additions & 2 deletions test/functional/base/generated/indexedAccessType/simple.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ package sandbox.base.indexedAccessType

external interface AgnosticDataRouteMatch {
var params: Any /* string | number */
var matches: Array<String>
var matches: js.array.ReadonlyArray<String>
}

external interface MyTypeWithAccessType {
var currentUrl: URL
var currentParams: Any /* string | number */
var matches: Array<String>
var matches: js.array.ReadonlyArray<String>
}
2 changes: 1 addition & 1 deletion test/functional/base/generated/intersection/simple.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
package sandbox.base.intersection

external interface AgnosticNonIndexRouteObject : AgnosticBaseRouteObject {
var children: Array<AgnosticRouteObject>?
var children: js.array.ReadonlyArray<AgnosticRouteObject>?
var index: Boolean /* false */?
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ fun method(cb: (options: MyInterfaceMethodCbOptions) -> Unit): String
}
external interface MyClassConflictHandlerConflictType {
var third: Boolean
var fourth: Array<String>
var fourth: js.array.ReadonlyArray<String>
}

external interface MyClassMethodCbOptions {
var third: Boolean
var fourth: Array<String>
var fourth: js.array.ReadonlyArray<String>
}

external interface MyInterfaceConflictHandlerConflictType {
var first: Boolean
var second: Array<String>
var second: js.array.ReadonlyArray<String>
}

external interface MyInterfaceMethodCbOptions {
var first: Boolean
var second: Array<String>
var second: js.array.ReadonlyArray<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package sandbox.base.typeLiteral

typealias HydrationState = String

external fun createMemoryRouter(routes: Array<RouteObject>, opts: CreateMemoryRouterOpts = definedExternally): RemixRouter
external fun createMemoryRouter(routes: js.array.ReadonlyArray<RouteObject>, opts: CreateMemoryRouterOpts = definedExternally): RemixRouter

external class MyClass {
constructor (options: MyClassOptions)
Expand All @@ -29,7 +29,7 @@ typealias BlockerFunction = (args: BlockerFunctionArgs) -> Boolean
external interface CreateMemoryRouterOpts {
var basename: String?
var hydrationData: HydrationState?
var initialEntries: Array<String>?
var initialEntries: js.array.ReadonlyArray<String>?
var initialIndex: Double?
}

Expand All @@ -40,12 +40,12 @@ var second: Double

external interface MyClassMethodOptions {
var third: Boolean
var fourth: Array<String>
var fourth: js.array.ReadonlyArray<String>
}

external interface MyInterfaceMethodOptions {
var first: Boolean
var second: Array<String>
var second: js.array.ReadonlyArray<String>
}

external interface ShouldRevalidateFunctionArgs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ package sandbox.top.level
external interface CreateMemoryRouterOpts {
var basename: String?
var hydrationData: HydrationState?
var initialEntries: Array<String>?
var initialEntries: js.array.ReadonlyArray<String>?
var initialIndex: Double?
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ package sandbox.top.level

external interface MyClassWithTypeLiteralMethodOptions {
var third: Boolean
var fourth: Array<String>
var fourth: js.array.ReadonlyArray<String>
}
2 changes: 1 addition & 1 deletion test/functional/top-level/generated/createMemoryRouter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

package sandbox.top.level

external fun createMemoryRouter(routes: Array<RouteObject>, opts: CreateMemoryRouterOpts = definedExternally): RemixRouter
external fun createMemoryRouter(routes: js.array.ReadonlyArray<RouteObject>, opts: CreateMemoryRouterOpts = definedExternally): RemixRouter

0 comments on commit c5a2365

Please sign in to comment.