Skip to content

Commit

Permalink
Merge pull request #762 from kohlschutter/ck/TsNoCheck
Browse files Browse the repository at this point in the history
Add support @tsnocheck annotation, adds "// @ts-nocheck" to TypeScript
  • Loading branch information
lgrignon authored Oct 10, 2023
2 parents 03c89fa + 7b5569d commit 618d16b
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
35 changes: 35 additions & 0 deletions core-lib/es5/src/main/java/jsweet/lang/TsNoCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* JSweet - http://www.jsweet.org
* Copyright (C) 2015 CINCHEO SAS <renaud.pawlak@cincheo.fr>
*
* 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 jsweet.lang;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* This annotation tells the transpiler to emit {@code // @ts-nocheck} at the beginning of
* the file.
*
* @author Christian Kohlschütter
*/
@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.TYPE })
@Documented
public @interface TsNoCheck {
}
35 changes: 35 additions & 0 deletions core-lib/es6/src/main/java/jsweet/lang/TsNoCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* JSweet - http://www.jsweet.org
* Copyright (C) 2015 CINCHEO SAS <renaud.pawlak@cincheo.fr>
*
* 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 jsweet.lang;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* This annotation tells the transpiler to emit {@code // @ts-nocheck} at the beginning of
* the file.
*
* @author Christian Kohlschütter
*/
@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.TYPE })
@Documented
public @interface TsNoCheck {
}
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,22 @@ public Void visitCompilationUnit(final CompilationUnitTree compilationUnit, fina
packageElement, getCompilationUnit());
}

// TypeScript access a line marked "// @ts-nocheck" at the beginning of the file
// to suppress certain warnings. Java won't allow us to annotate the "package" statement,
// so let's check all types.
checkTsNoCheck : for (Tree def : compilationUnit.getTypeDecls()) {
if (def instanceof ClassTree) {
for (AnnotationTree at : ((ClassTree)def).getModifiers().getAnnotations()) {
// NOTE This supports TsNoCheck being an annotation in any package
String typeStr = at.getAnnotationType().toString();
if ("TsNoCheck".equals(typeStr) || typeStr.endsWith(".TsNoCheck")) {
print("// @ts-nocheck\n");
break checkTsNoCheck;
}
}
}
}

PackageElement rootPackage = context.getFirstEnclosingRootPackage(packageElement);
if (rootPackage != null) {
if (!checkRootPackageParent(compilationUnit, rootPackage,
Expand Down
4 changes: 4 additions & 0 deletions transpiler/src/test/java/source/nativestructures/Numbers.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import static def.js.Globals.isNaN;

import def.js.Array;
import jsweet.lang.TsNoCheck;

//without this annotation, TypeScript would complain about the asserts in line 44/45
// ("[ts] This condition will always return 'true'")
@TsNoCheck
public class Numbers {

static Array<String> trace = new Array<>();
Expand Down

0 comments on commit 618d16b

Please sign in to comment.