Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly emit protected constructors #92

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -752,18 +752,11 @@ public Optional<TypeMirror> getClassValueFromAnnotation(

public boolean requiresProtectedConstructor() {
if (isJsType()) {
List<TsElement> constructors =
element.getEnclosedElements().stream()
.map(enclosedElement -> TsElement.of(enclosedElement, env))
.filter(TsElement::isConstructor)
.collect(Collectors.toList());
boolean allIgnored =
!constructors.isEmpty() && constructors.stream().allMatch(TsElement::isIgnored);

if (allIgnored) {
return true;
}

return element.getEnclosedElements().stream()
.map(enclosedElement -> TsElement.of(enclosedElement, env))
.filter(TsElement::isConstructor)
.filter(tsElement -> !tsElement.isPrivate())
.allMatch(TsElement::isIgnored);
} else {
Optional<TsElement> jsConstructor =
element.getEnclosedElements().stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright © 2023 Vertispan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.vertispan.tsdefs.tests.constructors;

import jsinterop.annotations.JsIgnore;
import jsinterop.annotations.JsType;

@JsType
public class JsTypeWithPrivateAndIgnoredConstructors {

public String property1;
public String property2;

private JsTypeWithPrivateAndIgnoredConstructors(String property) {
this.property1 = property;
this.property2 = property;
}

@JsIgnore
public JsTypeWithPrivateAndIgnoredConstructors(String property1, String property2) {
this.property1 = property1;
this.property2 = property2;
}
}
17 changes: 15 additions & 2 deletions jsinterop-ts-defs-test/src/test/resources/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ import JsInterfaceWithIgnoredMembers = com.vertispan.tsdefs.tests.tsignore.JsInt

import JsTypeGrandChild = com.vertispan.tsdefs.tests.inheritance.JsTypeGrandChild;
import JsTypeGrandChild2 = com.vertispan.tsdefs.tests.inheritance.JsTypeGrandChild2;

import JsTypeWithPrivateAndIgnoredConstructors = com.vertispan.tsdefs.tests.constructors.JsTypeWithPrivateAndIgnoredConstructors;
// ---------- Properties tests -------------------------
const jsTypeWithProperties = new JsTypeWithProperties();

Expand Down Expand Up @@ -1037,4 +1037,17 @@ class ImplementsUnionTypeApiUndefined implements UnionTypeApi {
arraysFunction(param1: Array<number | Array<number | undefined | null>>, param2: Array<number | Array<number | undefined | null>>): number | Array<Array<number | Array<number | undefined | null>>> | undefined | null {
return undefined;
}
}
}

// @ts-expect-error
const jsTypeWithPrivateAndIgnoredConstructors= new JsTypeWithPrivateAndIgnoredConstructors();
// @ts-expect-error
const jsTypeWithPrivateAndIgnoredConstructors= new JsTypeWithPrivateAndIgnoredConstructors("property");
// @ts-expect-error
const jsTypeWithPrivateAndIgnoredConstructors= new JsTypeWithPrivateAndIgnoredConstructors("property1", "property2");

class ExtendsFromJsTypeWithPrivateAndIgnoredConstructors extends JsTypeWithPrivateAndIgnoredConstructors {
constructor() {
super();
}
}
Loading