From d896d3f8a91d19814c35103244a9e17e01cafe87 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 1 Nov 2016 11:05:09 -0700 Subject: [PATCH] Add test cases to report errors for decorators in js file --- src/compiler/program.ts | 6 ++++++ src/compiler/utilities.ts | 4 ++++ .../baselines/reference/decoratorInJsFile.symbols | 12 ++++++++++++ tests/baselines/reference/decoratorInJsFile.types | 14 ++++++++++++++ .../reference/decoratorInJsFile1.errors.txt | 13 +++++++++++++ tests/cases/compiler/decoratorInJsFile.ts | 12 ++++++++++++ tests/cases/compiler/decoratorInJsFile1.ts | 10 ++++++++++ 7 files changed, 71 insertions(+) create mode 100644 tests/baselines/reference/decoratorInJsFile.symbols create mode 100644 tests/baselines/reference/decoratorInJsFile.types create mode 100644 tests/baselines/reference/decoratorInJsFile1.errors.txt create mode 100644 tests/cases/compiler/decoratorInJsFile.ts create mode 100644 tests/cases/compiler/decoratorInJsFile1.ts diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 726df380e81..1eeeded5d36 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -943,6 +943,12 @@ namespace ts { return false; } + + // Since these are syntactic diagnostics, parent might not have been set + // this means the sourceFile cannot be infered from the node + function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): Diagnostic { + return createDiagnosticForNodeInSourceFile(sourceFile, node, message, arg0, arg1, arg2); + } }); } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index d78418e1976..84c27f6397b 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -498,6 +498,10 @@ namespace ts { export function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): Diagnostic { const sourceFile = getSourceFileOfNode(node); + return createDiagnosticForNodeInSourceFile(sourceFile, node, message, arg0, arg1, arg2); + } + + export function createDiagnosticForNodeInSourceFile(sourceFile: SourceFile, node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): Diagnostic { const span = getErrorSpanForNode(sourceFile, node); return createFileDiagnostic(sourceFile, span.start, span.length, message, arg0, arg1, arg2); } diff --git a/tests/baselines/reference/decoratorInJsFile.symbols b/tests/baselines/reference/decoratorInJsFile.symbols new file mode 100644 index 00000000000..1ad477095c1 --- /dev/null +++ b/tests/baselines/reference/decoratorInJsFile.symbols @@ -0,0 +1,12 @@ +=== tests/cases/compiler/a.js === + +@SomeDecorator +class SomeClass { +>SomeClass : Symbol(SomeClass, Decl(a.js, 0, 0)) + + foo(x: number) { +>foo : Symbol(SomeClass.foo, Decl(a.js, 2, 17)) +>x : Symbol(x, Decl(a.js, 3, 8)) + + } +} diff --git a/tests/baselines/reference/decoratorInJsFile.types b/tests/baselines/reference/decoratorInJsFile.types new file mode 100644 index 00000000000..49403979460 --- /dev/null +++ b/tests/baselines/reference/decoratorInJsFile.types @@ -0,0 +1,14 @@ +=== tests/cases/compiler/a.js === + +@SomeDecorator +>SomeDecorator : any + +class SomeClass { +>SomeClass : SomeClass + + foo(x: number) { +>foo : (x: number) => void +>x : number + + } +} diff --git a/tests/baselines/reference/decoratorInJsFile1.errors.txt b/tests/baselines/reference/decoratorInJsFile1.errors.txt new file mode 100644 index 00000000000..a90aa922f96 --- /dev/null +++ b/tests/baselines/reference/decoratorInJsFile1.errors.txt @@ -0,0 +1,13 @@ +tests/cases/compiler/a.js(2,1): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning. + + +==== tests/cases/compiler/a.js (1 errors) ==== + + @SomeDecorator + ~~~~~~~~~~~~~~ +!!! error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning. + class SomeClass { + foo(x: number) { + + } + } \ No newline at end of file diff --git a/tests/cases/compiler/decoratorInJsFile.ts b/tests/cases/compiler/decoratorInJsFile.ts new file mode 100644 index 00000000000..611be319209 --- /dev/null +++ b/tests/cases/compiler/decoratorInJsFile.ts @@ -0,0 +1,12 @@ +// @experimentaldecorators: true +// @emitdecoratormetadata: true +// @allowjs: true +// @noEmit: true + +// @filename: a.js +@SomeDecorator +class SomeClass { + foo(x: number) { + + } +} \ No newline at end of file diff --git a/tests/cases/compiler/decoratorInJsFile1.ts b/tests/cases/compiler/decoratorInJsFile1.ts new file mode 100644 index 00000000000..6bbe38cb835 --- /dev/null +++ b/tests/cases/compiler/decoratorInJsFile1.ts @@ -0,0 +1,10 @@ +// @allowjs: true +// @noEmit: true + +// @filename: a.js +@SomeDecorator +class SomeClass { + foo(x: number) { + + } +} \ No newline at end of file