From 0aeeaef0448f5ad836533dd28c77c351fb384d98 Mon Sep 17 00:00:00 2001 From: ark120202 Date: Tue, 3 Sep 2019 00:32:44 +0500 Subject: [PATCH 1/5] Upgrade TypeScript to v3.6 --- package-lock.json | 6 ++--- package.json | 6 ++--- src/LuaTransformer.ts | 2 +- src/TSHelper.ts | 21 ++++++++++++++--- src/typescript-internal.ts | 7 ++++++ test/unit/decorators/luaIterator.spec.ts | 30 ++++++++++++++++-------- test/unit/loops.spec.ts | 6 +++-- test/unit/tshelper.spec.ts | 7 +++--- 8 files changed, 60 insertions(+), 25 deletions(-) create mode 100644 src/typescript-internal.ts diff --git a/package-lock.json b/package-lock.json index 7837795a6..8b6468a77 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5179,9 +5179,9 @@ } }, "typescript": { - "version": "3.5.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.5.2.tgz", - "integrity": "sha512-7KxJovlYhTX5RaRbUdkAXN1KUZ8PwWlTzQdHV6xNqvuFOs7+WBo10TQUqT19Q/Jz2hk5v9TQDIhyLhhJY4p5AA==" + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.6.2.tgz", + "integrity": "sha512-lmQ4L+J6mnu3xweP8+rOrUwzmN+MRAj7TgtJtDaXE5PMyX2kCrklhg3rvOsOIfNeAWMQWO2F1GPc1kMD2vLAfw==" }, "uglify-js": { "version": "3.6.0", diff --git a/package.json b/package.json index 13ae2cce3..768745cb9 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,8 @@ "types": "dist/index.d.ts", "scripts": { "build": "tsc -p tsconfig.json && npm run build-lualib", - "build-lualib": "ts-node ./build_lualib.ts", - "pretest": "npm run lint && ts-node --transpile-only ./build_lualib.ts", + "build-lualib": "ts-node --files ./build_lualib.ts", + "pretest": "npm run lint && ts-node --files --transpile-only ./build_lualib.ts", "test": "jest", "lint": "npm run lint:tslint && npm run lint:prettier", "lint:prettier": "prettier --check \"**/*.{js,ts,yml,json,md}\" || (echo 'Run `npm run fix:prettier` to fix it.' && exit 1)", @@ -41,7 +41,7 @@ "dependencies": { "resolve": "^1.11.0", "source-map": "^0.7.3", - "typescript": "3.5.x" + "typescript": "^3.6.2" }, "devDependencies": { "@types/glob": "^7.1.1", diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index 3c9fb2c89..a88484d41 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -4458,7 +4458,7 @@ export class LuaTransformer { } protected transformArguments( - params: ts.NodeArray | ts.Expression[], + params: readonly ts.Expression[], sig?: ts.Signature, context?: T ): tstl.Expression[] { diff --git a/src/TSHelper.ts b/src/TSHelper.ts index 903b7f069..c68c07298 100644 --- a/src/TSHelper.ts +++ b/src/TSHelper.ts @@ -404,7 +404,7 @@ export function getCustomDecorators(type: ts.Type, checker: ts.TypeChecker): Map return decMap; } -export function getCustomNodeDirectives(node: ts.Node): Map { +function getCustomNodeDirectives(node: ts.Node): Map { const directivesMap = new Map(); ts.getJSDocTags(node).forEach(tag => { @@ -419,10 +419,25 @@ export function getCustomNodeDirectives(node: ts.Node): Map { + const directivesMap = new Map(); + if (file.statements.length > 0) { - return getCustomNodeDirectives(file.statements[0]); + // Manually collect jsDoc because `getJSDocTags` includes tags only from closest comment + const jsDoc = file.statements[0].jsDoc; + if (jsDoc) { + // TODO(refactor): flatMap + const tags = jsDoc.reduce((acc, x) => [...acc, ...(x.tags || [])], []); + tags.forEach(tag => { + const tagName = tag.tagName.text; + if (Decorator.isValid(tagName)) { + const dec = new Decorator(tagName, tag.comment ? tag.comment.split(" ") : []); + directivesMap.set(dec.kind, dec); + } + }); + } } - return new Map(); + + return directivesMap; } export function getCustomSignatureDirectives( diff --git a/src/typescript-internal.ts b/src/typescript-internal.ts new file mode 100644 index 000000000..ddff96eaf --- /dev/null +++ b/src/typescript-internal.ts @@ -0,0 +1,7 @@ +import * as ts from "typescript"; + +declare module "typescript" { + interface Statement { + jsDoc?: ts.JSDoc[]; + } +} diff --git a/test/unit/decorators/luaIterator.spec.ts b/test/unit/decorators/luaIterator.spec.ts index 5aa24d8ae..6f25320d7 100644 --- a/test/unit/decorators/luaIterator.spec.ts +++ b/test/unit/decorators/luaIterator.spec.ts @@ -119,8 +119,10 @@ test("forof lua iterator destructuring with existing variables", () => { test("forof lua iterator tuple-return", () => { const code = ` const arr = ["a", "b", "c"]; - /** @luaIterator */ - /** @tupleReturn */ + /** + * @luaIterator + * @tupleReturn + */ interface Iter extends Iterable<[string, string]> {} function luaIter(): Iter { let i = 0; @@ -144,8 +146,10 @@ test("forof lua iterator tuple-return", () => { test("forof lua iterator tuple-return with existing variables", () => { const code = ` const arr = ["a", "b", "c"]; - /** @luaIterator */ - /** @tupleReturn */ + /** + * @luaIterator + * @tupleReturn + */ interface Iter extends Iterable<[string, string]> {} function luaIter(): Iter { let i = 0; @@ -170,8 +174,10 @@ test("forof lua iterator tuple-return with existing variables", () => { test("forof lua iterator tuple-return single variable", () => { const code = ` - /** @luaIterator */ - /** @tupleReturn */ + /** + * @luaIterator + * @tupleReturn + */ interface Iter extends Iterable<[string, string]> {} declare function luaIter(): Iter; for (let x of luaIter()) {} @@ -188,8 +194,10 @@ test("forof lua iterator tuple-return single variable", () => { test("forof lua iterator tuple-return single existing variable", () => { const code = ` - /** @luaIterator */ - /** @tupleReturn */ + /** + * @luaIterator + * @tupleReturn + */ interface Iter extends Iterable<[string, string]> {} declare function luaIter(): Iter; let x: [string, string]; @@ -235,8 +243,10 @@ test("forof forwarded lua iterator", () => { test("forof forwarded lua iterator with tupleReturn", () => { const code = ` const arr = ["a", "b", "c"]; - /** @luaIterator */ - /** @tupleReturn */ + /** + * @luaIterator + * @tupleReturn + */ interface Iter extends Iterable<[string, string]> {} function luaIter(): Iter { let i = 0; diff --git a/test/unit/loops.spec.ts b/test/unit/loops.spec.ts index 4d50dca20..5e137bdc5 100644 --- a/test/unit/loops.spec.ts +++ b/test/unit/loops.spec.ts @@ -547,8 +547,10 @@ describe("for...of empty destructuring", () => { test("luaIterator+tupleReturn", () => { const code = ` const arr = [["a", "b"], ["c", "d"], ["e", "f"]]; - /** @luaIterator */ - /** @tupleReturn */ + /** + * @luaIterator + * @tupleReturn + */ interface Iter extends Iterable<[string, string]> {} function luaIter(): Iter { let it = 0; diff --git a/test/unit/tshelper.spec.ts b/test/unit/tshelper.spec.ts index 7202ff2e4..6cc1a809b 100644 --- a/test/unit/tshelper.spec.ts +++ b/test/unit/tshelper.spec.ts @@ -114,9 +114,10 @@ test("GetCustomDecorators multiple jsdoc", () => { test("GetCustomDecorators multiple default jsdoc", () => { const source = ` /** - * @description abc - * @phantom - * @compileMembersOnly */ + * @description abc + * @phantom + * @compileMembersOnly + */ enum TestEnum { val1 = 0, val2 = 2, From fb88a8c0929b30948861457a4b3ed5b8dc8069a8 Mon Sep 17 00:00:00 2001 From: ark120202 Date: Tue, 3 Sep 2019 04:54:26 +0500 Subject: [PATCH 2/5] Add explicit reference to typescript-internal file --- src/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 4da0e6639..6f0bac937 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,10 +7,10 @@ import { emitTranspiledFiles, OutputFile } from "./Emit"; import { transpile, TranspiledFile, TranspileResult } from "./Transpile"; export { + createDiagnosticReporter, parseCommandLine, ParsedCommandLine, updateParsedConfigFile, - createDiagnosticReporter, } from "./CommandLineParser"; export * from "./CompilerOptions"; export * from "./Emit"; @@ -21,6 +21,8 @@ export * from "./LuaTransformer"; export * from "./Transpile"; export * from "./TranspileError"; +/// + export interface TranspileFilesResult { diagnostics: ts.Diagnostic[]; emitResult: OutputFile[]; From c979e1864b099191a6c8751d2d6e718d284e4c8b Mon Sep 17 00:00:00 2001 From: ark120202 Date: Sat, 7 Sep 2019 07:56:13 +0500 Subject: [PATCH 3/5] Include main project in test config includes --- src/index.ts | 2 -- test/tsconfig.json | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6f0bac937..17226cf7f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,8 +21,6 @@ export * from "./LuaTransformer"; export * from "./Transpile"; export * from "./TranspileError"; -/// - export interface TranspileFilesResult { diagnostics: ts.Diagnostic[]; emitResult: OutputFile[]; diff --git a/test/tsconfig.json b/test/tsconfig.json index 700c9d291..860c37709 100644 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -13,5 +13,6 @@ "noEmit": true, "module": "commonjs" }, + "include": [".", "../src"], "exclude": ["translation/transformation", "cli/errors", "cli/watch", "transpile/directories", "transpile/outFile"] } From a248af815993a86455abae47f927170cc46cf25e Mon Sep 17 00:00:00 2001 From: ark120202 Date: Sat, 7 Sep 2019 09:35:10 +0500 Subject: [PATCH 4/5] Exclude lualib from test tsconfig --- test/tsconfig.json | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/tsconfig.json b/test/tsconfig.json index 860c37709..63542f392 100644 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -14,5 +14,12 @@ "module": "commonjs" }, "include": [".", "../src"], - "exclude": ["translation/transformation", "cli/errors", "cli/watch", "transpile/directories", "transpile/outFile"] + "exclude": [ + "translation/transformation", + "cli/errors", + "cli/watch", + "transpile/directories", + "transpile/outFile", + "../src/lualib" + ] } From a23de2d70bb702b7e6eb74b307354b4a3b68e963 Mon Sep 17 00:00:00 2001 From: ark120202 Date: Mon, 9 Sep 2019 15:49:35 +0500 Subject: [PATCH 5/5] Add noSelfInFile with other annotations test --- .../functionExpressionTypeInference.spec.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/unit/functions/validation/functionExpressionTypeInference.spec.ts b/test/unit/functions/validation/functionExpressionTypeInference.spec.ts index 767fe0b30..e475f2216 100644 --- a/test/unit/functions/validation/functionExpressionTypeInference.spec.ts +++ b/test/unit/functions/validation/functionExpressionTypeInference.spec.ts @@ -16,6 +16,17 @@ test.each(["noSelf", "noSelfInFile"])("noSelf function method argument (%p)", no expect(util.transpileAndExecute(code, undefined, undefined, header)).toBe("foo"); }); +test("noSelfInFile works when first statement has other annotations", () => { + util.testModule` + /** @noSelfInFile */ + + /** @internal */ + function foo() {} + + const test: (this: void) => void = foo; + `.expectToHaveNoDiagnostics(); +}); + test.each(["(this: void, s: string) => string", "(this: any, s: string) => string", "(s: string) => string"])( "Function expression type inference in binary operator (%p)", funcType => {