Skip to content

Commit

Permalink
fix: correct types for attribute 'set_' setter function
Browse files Browse the repository at this point in the history
  • Loading branch information
isaac-mason committed Aug 13, 2023
1 parent 1a42e55 commit a7dfc9f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-sloths-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"webidl-dts-gen": minor
---

fix: correct types for attribute 'set\_' setter function
36 changes: 34 additions & 2 deletions packages/webidl-dts-gen/src/convert-idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,44 @@ function createEmscriptenAttributeGetter(value: webidl2.AttributeMemberType) {
}

function createEmscriptenAttributeSetter(value: webidl2.AttributeMemberType) {
const parameter = ts.factory.createParameterDeclaration([], undefined, value.name, undefined, convertType(value.idlType))
let idlType: webidl2.IDLTypeDescription
let parameters: ts.ParameterDeclaration[]

if (isFrozenArrayAttribute(value)) {
idlType = (value.idlType.idlType[0] as unknown as webidl2.IDLTypeDescription)
parameters = [
ts.factory.createParameterDeclaration(
[],
undefined,
'index',
undefined,
ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
),
ts.factory.createParameterDeclaration(
[],
undefined,
'value',
undefined,
convertType(idlType),
),
]
} else {
idlType = value.idlType
parameters = [
ts.factory.createParameterDeclaration(
[],
undefined,
'value',
undefined,
convertType(idlType),
),
]
}

return createMethod({
name: 'set_' + value.name,
type: ts.factory.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword),
parameters: [parameter],
parameters,
emscripten: true,
})
}
Expand Down
22 changes: 21 additions & 1 deletion packages/webidl-dts-gen/tst/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,26 @@ describe('convert', () => {
)
})

it('supports non array attributes', async () => {
const idl = multiLine(
'interface Foo {', //
' attribute float position;', //
'};', //
)

const ts = await convert(idl, { emscripten: true })

expect(ts).toBe(
withDefaultEmscriptenOutput(
'class Foo {', //
' get_position(): number;', //
' set_position(value: number): void;', //
' position: number;', //
'}', //
),
)
})

it('supports array attributes', async () => {
const idl = multiLine(
'interface Foo {', //
Expand All @@ -208,7 +228,7 @@ describe('convert', () => {
withDefaultEmscriptenOutput(
'class Foo {', //
' get_position(index: number): number;', //
' set_position(position: ReadonlyArray<number>): void;', //
' set_position(index: number, value: number): void;', //
' position: number;', //
'}', //
),
Expand Down

0 comments on commit a7dfc9f

Please sign in to comment.