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

don't throw on unrecognized cases by default #29

Merged
merged 4 commits into from
Nov 28, 2019
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
test/index.js
test/index.d.ts
test/out
*.zip
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ haxe:

install:
- haxelib install hxnodejs
- haxelib dev hxtsdgen .

script:
- haxe test.hxml
- node test
- sh scripts/test.sh
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ to TypeScript.

#### No automatic export

Currently all the types have to be explictly exposed; types used by functions won't be
Currently all the types have to be explicitly exposed; types used by functions won't be
automatically exported so everything must be currently annotated for export.

#### Abstract enums

`hxtsdgen` can generate TypeScript [const enums](https://www.typescriptlang.org/docs/handbook/enums.html),
which are a pure compiler construction, substitued at compile time with their value.
which are a pure compiler construction, substituted at compile time with their value.

However `.ts` code can be compiled by 2 compilers: TypeScript and Babel.

Expand Down
2 changes: 1 addition & 1 deletion haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"contributors": ["elsassph", "nadako"],
"version": "0.2.0",
"releasenote": "Initial release",
"tags": ["js", "extern"],
"tags": ["js", "extern", "JavaScript", "TypeScript"],
"classPath": "src"
}
1 change: 1 addition & 0 deletions scripts/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rm -rf test/out test/index.js test/index.d.ts hxtsdgen.zip
2 changes: 2 additions & 0 deletions scripts/pack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sh scripts/clean.sh
zip -r hxtsdgen.zip src extraParams.hxml haxelib.json README.md
3 changes: 3 additions & 0 deletions scripts/submit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
sh scripts/pack.sh
haxelib submit hxtsdgen.zip
8 changes: 8 additions & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
sh scripts/clean.sh && \
sh scripts/pack.sh && \
haxelib install hxtsdgen.zip && \
haxe test.hxml && \
node test/index.js && \
sh scripts/clean.sh
# node test/index.js packages.txt
9 changes: 6 additions & 3 deletions src/hxtsdgen/CodeGen.hx
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ class CodeGen {
}

function generateConstructor(cl:ClassType, isInterface:Bool, indent:String, parts:Array<String>) {
// Heads up! constructors never declared as private since that will prevent inheritance in TS
// but haxe allows to extend a class even if it has a single explicitly private constructor.
// Example: `class haxe.io.BytesInput extends haxe.io.InputExample`
var privateCtor = true;
if (cl.constructor != null) {
var ctor = cl.constructor.get();
Expand All @@ -320,13 +323,13 @@ class CodeGen {
parts.push(renderDoc(ctor.doc, indent));
switch (ctor.type) {
case TFun(args, _):
var prefix = if (ctor.isPublic) "" else "private "; // TODO: should this really be protected?
var prefix = if (ctor.isPublic) "" else "protected ";
parts.push('${indent}${prefix}constructor(${renderArgs(selector, args)});');
default:
throw "wtf";
throw 'Invalid constructor type ${ctor.type.toString()}';
}
} else if (!isInterface) {
parts.push('${indent}private constructor();');
parts.push('${indent}protected constructor();');
}
}

Expand Down
1 change: 1 addition & 0 deletions src/hxtsdgen/Generator.hx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enum ExposeKind {
class Generator {

public static inline var GEN_ENUM_TS = #if hxtsdgen_enums_ts true #else false #end;
public static inline var THROW_ON_UNKNOWN = #if hxtsdgen_throw_on_unknown true #else false #end;
public static var SKIP_HEADER = #if hxtsdgen_skip_header true #else false #end;
public static var HEADER = "// Generated by Haxe TypeScript Declaration Generator :)";
public static var NO_EXPOSE_HINT = "// No types were @:expose'd.\n// Read more at http://haxe.org/manual/target-javascript-expose.html";
Expand Down
8 changes: 7 additions & 1 deletion src/hxtsdgen/TypeRenderer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ class TypeRenderer {
'{ [key: string]: ${renderType(ctx, elemT)} }';

default:
throw 'Cannot render type ${t.toString()} into a TypeScript declaration (TODO?)';
var msg = 'Cannot render type ${t.toString()} into a TypeScript declaration (TODO?)';
if(Generator.THROW_ON_UNKNOWN)
throw msg;
else {
haxe.macro.Context.warning(msg, haxe.macro.Context.currentPos());
return 'any';
}
}
}

Expand Down
4 changes: 0 additions & 4 deletions submit.sh

This file was deleted.

2 changes: 1 addition & 1 deletion test.hxml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-cp test
-cp src
-main Main
-lib hxnodejs
-lib hxtsdgen
--macro hxtsdgen.Generator.setHeader('/* tslint:disable */')
-js test/index.js
4 changes: 0 additions & 4 deletions test.sh

This file was deleted.

3 changes: 3 additions & 0 deletions test/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using StringTools;

@:expose
class Main {
static var failFast = false;
static public function main() {
var programDir = haxe.io.Path.directory(Sys.programPath());
var total = 0, failed = 0;
Expand All @@ -20,6 +21,7 @@ class Main {
println("Haxe compilation failed!");
println("---");
failed++;
if(failFast) throw 'failFast is enabled so aborting test.';
} else if (testCase.dts != tsOut.dts || testCase.ets != tsOut.ets) {
println("Output is different!");
println('Expected:\n[d.ts]\n${testCase.dts}');
Expand All @@ -28,6 +30,7 @@ class Main {
if (tsOut.ets != null) println('\n---\n[enums.ts]\n${tsOut.ets}');
failed++;
println("---");
if(failFast) throw 'failFast is enabled so aborting test.';
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/cases/basic-types.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class C {
----

export class C {
private constructor();
protected constructor();
static int(): number;
static float(): number;
static string(): string;
Expand Down
2 changes: 1 addition & 1 deletion test/cases/callback.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class C {
----

export class C {
private constructor();
protected constructor();
static f(cb: () => void): void;
static f2(cb: (arg0: string, arg1?: number) => number): void;
static f3(cb: (arg0: string, arg1: number, arg2: string) => void): void;
Expand Down
4 changes: 2 additions & 2 deletions test/cases/ctors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class E {


export class C {
private constructor();
protected constructor();
}

export class D {
private constructor(v: number);
protected constructor(v: number);
}

export class E {
Expand Down
2 changes: 1 addition & 1 deletion test/cases/doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class C {
* This label starts with a stupid asterisk
*/
export class Asterisk {
private constructor();
protected constructor();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/cases/eithertype.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import haxe.extern.EitherType as Or;
----

export class C {
private constructor();
protected constructor();
v1: number | string;
v2: number | string | boolean;
}
6 changes: 3 additions & 3 deletions test/cases/expose.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ class F {
----

export class C {
private constructor();
protected constructor();
}

export class some {
private constructor();
protected constructor();
}

export namespace my.pack {
export class A {
private constructor();
protected constructor();
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/cases/function.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class C {
----

export class C {
private constructor();
protected constructor();
static f(): void;
static f2(a: string, b?: number): number;
static f3(a: string, b: number, c: string): void;
Expand Down
4 changes: 2 additions & 2 deletions test/cases/generic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ export class C<A, B, C> {
}

export class D {
private constructor();
protected constructor();
load(): Promise<string>;
}

export class E<T> {
private constructor();
protected constructor();
load(): Promise<T>;
}
2 changes: 1 addition & 1 deletion test/cases/interfaces.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ export interface F {
}

export interface G {
set_field(value: string): string;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field order change fails in CI.

get_field(): string;
set_field(value: string): string;
}
8 changes: 4 additions & 4 deletions test/cases/native.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ class H {
----

export class B {
private constructor();
protected constructor();
}

export class E {
private constructor();
protected constructor();
}

export class H {
private constructor();
protected constructor();
}

export class G {
private constructor();
protected constructor();
}

export function i(a0: G, a1: HTMLHtmlElement): void;
22 changes: 11 additions & 11 deletions test/cases/properties.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,63 +70,63 @@ class K {
----

export class A {
private constructor();
protected constructor();
readonly field: string;
}

export class B {
private constructor();
protected constructor();
readonly field: string;
}

export class C {
private constructor();
protected constructor();
get_field(): string;
}

export class D {
private constructor();
protected constructor();
readonly field: string;
set_field(value: string): string;
}

export class E {
private constructor();
protected constructor();
readonly field: string;
set_field(v: string): string;
}

export class F {
private constructor();
protected constructor();
get_field(): string;
set_field(value: string): string;
}

export class G {
private constructor();
protected constructor();
get_field(): string;
set_field(value: string): string;
}

export class H {
private constructor();
protected constructor();
static readonly field: string;
}

export class I {
private constructor();
protected constructor();
static get_field(): string;
static set_field(value: string): string;
}

export class J<T> {
private constructor();
protected constructor();
readonly field: T;
set_field(value: T): T;
}

export class K {
private constructor();
protected constructor();
get_field(): string;
set_field(v: string): string;
}
4 changes: 2 additions & 2 deletions test/cases/typedef.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ export type Z = {
}

export class A {
private constructor();
protected constructor();
f(): X;
}

export class B {
private constructor();
protected constructor();
f(): Y2;
}

Expand Down