From c5a2365c012b1c9e05d55c765acd5a688c2efb2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Kautler?= Date: Wed, 18 Sep 2024 12:41:52 +0200 Subject: [PATCH] Always use ReadonlyArray for arrays --- src/converter/plugins/convertArrayType.ts | 22 +++++++++++++++---- .../plugins/convertReadonlyArrayType.ts | 22 ------------------- src/defaultPlugins.ts | 2 -- .../functional/base/generated/array/simple.kt | 2 +- .../base/generated/function/vararg.kt | 2 +- .../generated/indexedAccessType/simple.kt | 4 ++-- .../base/generated/intersection/simple.kt | 2 +- .../typeLiteral/callbackParameter.kt | 8 +++---- .../typeLiteral/functionParameter.kt | 8 +++---- .../generated/CreateMemoryRouterOpts.kt | 2 +- .../MyClassWithTypeLiteralMethodOptions.kt | 2 +- .../top-level/generated/createMemoryRouter.kt | 2 +- 12 files changed, 34 insertions(+), 44 deletions(-) delete mode 100644 src/converter/plugins/convertReadonlyArrayType.ts diff --git a/src/converter/plugins/convertArrayType.ts b/src/converter/plugins/convertArrayType.ts index 3e7c623..cef4e4f 100644 --- a/src/converter/plugins/convertArrayType.ts +++ b/src/converter/plugins/convertArrayType.ts @@ -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(checkCoverageServiceKey) + checkCoverageService?.cover(node) + checkCoverageService?.cover(node.type) - const checkCoverageService = context.lookupService(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(checkCoverageServiceKey) + checkCoverageService?.cover(node) + + return `js.array.ReadonlyArray<${render(node.elementType)}>` + } + + return null }) diff --git a/src/converter/plugins/convertReadonlyArrayType.ts b/src/converter/plugins/convertReadonlyArrayType.ts deleted file mode 100644 index ea4a04a..0000000 --- a/src/converter/plugins/convertReadonlyArrayType.ts +++ /dev/null @@ -1,22 +0,0 @@ -import ts from "typescript"; -import {createSimplePlugin} from "../plugin.js"; -import {CheckCoverageService, checkCoverageServiceKey} from "./CheckCoveragePlugin.js"; - -export const convertReadonlyArrayType = createSimplePlugin((node, context, render) => { - if ( - ts.isTypeOperatorNode(node) && - node.operator === ts.SyntaxKind.ReadonlyKeyword && - ts.isArrayTypeNode(node.type) - ) { - - const checkCoverageService = context.lookupService(checkCoverageServiceKey) - checkCoverageService?.cover(node) - checkCoverageService?.cover(node.type) - - return `js.array.ReadonlyArray<${render(node.type.elementType)}>` - } - - - - return null; -}) diff --git a/src/defaultPlugins.ts b/src/defaultPlugins.ts index 72d463b..2696273 100644 --- a/src/defaultPlugins.ts +++ b/src/defaultPlugins.ts @@ -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"; @@ -139,7 +138,6 @@ export const createPlugins = ( convertFunctionType, convertLiteralType, convertArrayType, - convertReadonlyArrayType, convertTypeAliasDeclaration, convertEnumDeclaration, convertEnumMember, diff --git a/test/functional/base/generated/array/simple.kt b/test/functional/base/generated/array/simple.kt index 5c9622e..6809ed1 100644 --- a/test/functional/base/generated/array/simple.kt +++ b/test/functional/base/generated/array/simple.kt @@ -7,6 +7,6 @@ package sandbox.base.array -typealias SimpleArray = Array +typealias SimpleArray = js.array.ReadonlyArray typealias SimpleReadonlyArray = js.array.ReadonlyArray diff --git a/test/functional/base/generated/function/vararg.kt b/test/functional/base/generated/function/vararg.kt index 26c42a9..f9ecfb3 100644 --- a/test/functional/base/generated/function/vararg.kt +++ b/test/functional/base/generated/function/vararg.kt @@ -9,6 +9,6 @@ package sandbox.base.function external fun simpleVararg(vararg args: String): Unit -external fun > genericVararg(vararg args: Any? /* T */): Unit +external fun > genericVararg(vararg args: Any? /* T */): Unit external fun callbackVararg(fn: Function /* (...args: string[]) => void */): Unit diff --git a/test/functional/base/generated/indexedAccessType/simple.kt b/test/functional/base/generated/indexedAccessType/simple.kt index 7c78844..ffff0e0 100644 --- a/test/functional/base/generated/indexedAccessType/simple.kt +++ b/test/functional/base/generated/indexedAccessType/simple.kt @@ -9,11 +9,11 @@ package sandbox.base.indexedAccessType external interface AgnosticDataRouteMatch { var params: Any /* string | number */ -var matches: Array +var matches: js.array.ReadonlyArray } external interface MyTypeWithAccessType { var currentUrl: URL var currentParams: Any /* string | number */ -var matches: Array +var matches: js.array.ReadonlyArray } diff --git a/test/functional/base/generated/intersection/simple.kt b/test/functional/base/generated/intersection/simple.kt index a8ed1d4..f942a82 100644 --- a/test/functional/base/generated/intersection/simple.kt +++ b/test/functional/base/generated/intersection/simple.kt @@ -8,6 +8,6 @@ package sandbox.base.intersection external interface AgnosticNonIndexRouteObject : AgnosticBaseRouteObject { -var children: Array? +var children: js.array.ReadonlyArray? var index: Boolean /* false */? } diff --git a/test/functional/base/generated/typeLiteral/callbackParameter.kt b/test/functional/base/generated/typeLiteral/callbackParameter.kt index e1be7ce..d49920a 100644 --- a/test/functional/base/generated/typeLiteral/callbackParameter.kt +++ b/test/functional/base/generated/typeLiteral/callbackParameter.kt @@ -18,20 +18,20 @@ fun method(cb: (options: MyInterfaceMethodCbOptions) -> Unit): String } external interface MyClassConflictHandlerConflictType { var third: Boolean -var fourth: Array +var fourth: js.array.ReadonlyArray } external interface MyClassMethodCbOptions { var third: Boolean -var fourth: Array +var fourth: js.array.ReadonlyArray } external interface MyInterfaceConflictHandlerConflictType { var first: Boolean -var second: Array +var second: js.array.ReadonlyArray } external interface MyInterfaceMethodCbOptions { var first: Boolean -var second: Array +var second: js.array.ReadonlyArray } \ No newline at end of file diff --git a/test/functional/base/generated/typeLiteral/functionParameter.kt b/test/functional/base/generated/typeLiteral/functionParameter.kt index 3a7ca6c..f19da28 100644 --- a/test/functional/base/generated/typeLiteral/functionParameter.kt +++ b/test/functional/base/generated/typeLiteral/functionParameter.kt @@ -9,7 +9,7 @@ package sandbox.base.typeLiteral typealias HydrationState = String -external fun createMemoryRouter(routes: Array, opts: CreateMemoryRouterOpts = definedExternally): RemixRouter +external fun createMemoryRouter(routes: js.array.ReadonlyArray, opts: CreateMemoryRouterOpts = definedExternally): RemixRouter external class MyClass { constructor (options: MyClassOptions) @@ -29,7 +29,7 @@ typealias BlockerFunction = (args: BlockerFunctionArgs) -> Boolean external interface CreateMemoryRouterOpts { var basename: String? var hydrationData: HydrationState? -var initialEntries: Array? +var initialEntries: js.array.ReadonlyArray? var initialIndex: Double? } @@ -40,12 +40,12 @@ var second: Double external interface MyClassMethodOptions { var third: Boolean -var fourth: Array +var fourth: js.array.ReadonlyArray } external interface MyInterfaceMethodOptions { var first: Boolean -var second: Array +var second: js.array.ReadonlyArray } external interface ShouldRevalidateFunctionArgs { diff --git a/test/functional/top-level/generated/CreateMemoryRouterOpts.kt b/test/functional/top-level/generated/CreateMemoryRouterOpts.kt index b7da0cc..d7630dd 100644 --- a/test/functional/top-level/generated/CreateMemoryRouterOpts.kt +++ b/test/functional/top-level/generated/CreateMemoryRouterOpts.kt @@ -5,6 +5,6 @@ package sandbox.top.level external interface CreateMemoryRouterOpts { var basename: String? var hydrationData: HydrationState? -var initialEntries: Array? +var initialEntries: js.array.ReadonlyArray? var initialIndex: Double? } diff --git a/test/functional/top-level/generated/MyClassWithTypeLiteralMethodOptions.kt b/test/functional/top-level/generated/MyClassWithTypeLiteralMethodOptions.kt index 7bc1402..ee7128d 100644 --- a/test/functional/top-level/generated/MyClassWithTypeLiteralMethodOptions.kt +++ b/test/functional/top-level/generated/MyClassWithTypeLiteralMethodOptions.kt @@ -4,5 +4,5 @@ package sandbox.top.level external interface MyClassWithTypeLiteralMethodOptions { var third: Boolean -var fourth: Array +var fourth: js.array.ReadonlyArray } diff --git a/test/functional/top-level/generated/createMemoryRouter.kt b/test/functional/top-level/generated/createMemoryRouter.kt index 0bb5b44..1f01cf1 100644 --- a/test/functional/top-level/generated/createMemoryRouter.kt +++ b/test/functional/top-level/generated/createMemoryRouter.kt @@ -4,4 +4,4 @@ package sandbox.top.level -external fun createMemoryRouter(routes: Array, opts: CreateMemoryRouterOpts = definedExternally): RemixRouter +external fun createMemoryRouter(routes: js.array.ReadonlyArray, opts: CreateMemoryRouterOpts = definedExternally): RemixRouter