From 80db0f2f166d0a3547319fce789c31604e8fc83b Mon Sep 17 00:00:00 2001 From: Yui Date: Tue, 19 Jul 2016 15:16:27 -0700 Subject: [PATCH] [Release-2.0] Fix 9782: do not report blocked-scope-used-before-declaration error in ambient context (#9789) (#9830) * Do not report block-scoped-used-before-declaration in ambient context * Add tests and baselines --- src/compiler/checker.ts | 2 +- .../noUsedBeforeDefinedErrorInAmbientContext1.symbols | 9 +++++++++ .../noUsedBeforeDefinedErrorInAmbientContext1.types | 9 +++++++++ .../noUsedBeforeDefinedErrorInAmbientContext1.ts | 4 ++++ 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/noUsedBeforeDefinedErrorInAmbientContext1.symbols create mode 100644 tests/baselines/reference/noUsedBeforeDefinedErrorInAmbientContext1.types create mode 100644 tests/cases/compiler/noUsedBeforeDefinedErrorInAmbientContext1.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d769cc4e3fc..1cebd469388 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -974,7 +974,7 @@ namespace ts { Debug.assert(declaration !== undefined, "Block-scoped variable declaration is undefined"); - if (!isBlockScopedNameDeclaredBeforeUse(getAncestor(declaration, SyntaxKind.VariableDeclaration), errorLocation)) { + if (!isInAmbientContext(declaration) && !isBlockScopedNameDeclaredBeforeUse(getAncestor(declaration, SyntaxKind.VariableDeclaration), errorLocation)) { error(errorLocation, Diagnostics.Block_scoped_variable_0_used_before_its_declaration, declarationNameToString(declaration.name)); } } diff --git a/tests/baselines/reference/noUsedBeforeDefinedErrorInAmbientContext1.symbols b/tests/baselines/reference/noUsedBeforeDefinedErrorInAmbientContext1.symbols new file mode 100644 index 00000000000..690a19ef18f --- /dev/null +++ b/tests/baselines/reference/noUsedBeforeDefinedErrorInAmbientContext1.symbols @@ -0,0 +1,9 @@ +=== tests/cases/compiler/test.d.ts === + +declare var S: typeof A; // no error +>S : Symbol(S, Decl(test.d.ts, 1, 11)) +>A : Symbol(A, Decl(test.d.ts, 2, 13)) + +declare const A: number; +>A : Symbol(A, Decl(test.d.ts, 2, 13)) + diff --git a/tests/baselines/reference/noUsedBeforeDefinedErrorInAmbientContext1.types b/tests/baselines/reference/noUsedBeforeDefinedErrorInAmbientContext1.types new file mode 100644 index 00000000000..dfacda30915 --- /dev/null +++ b/tests/baselines/reference/noUsedBeforeDefinedErrorInAmbientContext1.types @@ -0,0 +1,9 @@ +=== tests/cases/compiler/test.d.ts === + +declare var S: typeof A; // no error +>S : number +>A : number + +declare const A: number; +>A : number + diff --git a/tests/cases/compiler/noUsedBeforeDefinedErrorInAmbientContext1.ts b/tests/cases/compiler/noUsedBeforeDefinedErrorInAmbientContext1.ts new file mode 100644 index 00000000000..834356e88b4 --- /dev/null +++ b/tests/cases/compiler/noUsedBeforeDefinedErrorInAmbientContext1.ts @@ -0,0 +1,4 @@ +// @filename: test.d.ts + +declare var S: typeof A; // no error +declare const A: number; \ No newline at end of file