From 17cf4357adbb363ba796a810150fef28d8bac10e Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 18 Oct 2016 12:24:41 -0700 Subject: [PATCH 1/3] Add testcase when error is reported about unused react --- .../reference/unusedImports13.errors.txt | 25 +++++++++++++ tests/baselines/reference/unusedImports13.js | 27 ++++++++++++++ tests/baselines/reference/unusedImports14.js | 27 ++++++++++++++ .../reference/unusedImports14.symbols | 36 ++++++++++++++++++ .../baselines/reference/unusedImports14.types | 37 +++++++++++++++++++ tests/cases/compiler/unusedImports13.ts | 23 ++++++++++++ tests/cases/compiler/unusedImports14.ts | 23 ++++++++++++ 7 files changed, 198 insertions(+) create mode 100644 tests/baselines/reference/unusedImports13.errors.txt create mode 100644 tests/baselines/reference/unusedImports13.js create mode 100644 tests/baselines/reference/unusedImports14.js create mode 100644 tests/baselines/reference/unusedImports14.symbols create mode 100644 tests/baselines/reference/unusedImports14.types create mode 100644 tests/cases/compiler/unusedImports13.ts create mode 100644 tests/cases/compiler/unusedImports14.ts diff --git a/tests/baselines/reference/unusedImports13.errors.txt b/tests/baselines/reference/unusedImports13.errors.txt new file mode 100644 index 00000000000..0e870795eb6 --- /dev/null +++ b/tests/baselines/reference/unusedImports13.errors.txt @@ -0,0 +1,25 @@ +tests/cases/compiler/foo.tsx(2,8): error TS6133: 'React' is declared but never used. + + +==== tests/cases/compiler/foo.tsx (1 errors) ==== + + import React = require("react"); + ~~~~~ +!!! error TS6133: 'React' is declared but never used. + + export const FooComponent =
+ +==== tests/cases/compiler/node_modules/@types/react/index.d.ts (0 errors) ==== + export = React; + export as namespace React; + + declare namespace React { + function createClass(spec); + } + declare global { + namespace JSX { + } + } + + + \ No newline at end of file diff --git a/tests/baselines/reference/unusedImports13.js b/tests/baselines/reference/unusedImports13.js new file mode 100644 index 00000000000..01e91abc14b --- /dev/null +++ b/tests/baselines/reference/unusedImports13.js @@ -0,0 +1,27 @@ +//// [tests/cases/compiler/unusedImports13.ts] //// + +//// [foo.tsx] + +import React = require("react"); + +export const FooComponent =
+ +//// [index.d.ts] +export = React; +export as namespace React; + +declare namespace React { + function createClass(spec); +} +declare global { + namespace JSX { + } +} + + + + +//// [foo.jsx] +"use strict"; +var React = require("react"); +exports.FooComponent =
; diff --git a/tests/baselines/reference/unusedImports14.js b/tests/baselines/reference/unusedImports14.js new file mode 100644 index 00000000000..f3c51590e49 --- /dev/null +++ b/tests/baselines/reference/unusedImports14.js @@ -0,0 +1,27 @@ +//// [tests/cases/compiler/unusedImports14.ts] //// + +//// [foo.tsx] + +import React = require("react"); + +export const FooComponent =
+ +//// [index.d.ts] +export = React; +export as namespace React; + +declare namespace React { + function createClass(spec); +} +declare global { + namespace JSX { + } +} + + + + +//// [foo.js] +"use strict"; +var React = require("react"); +exports.FooComponent = React.createElement("div", null); diff --git a/tests/baselines/reference/unusedImports14.symbols b/tests/baselines/reference/unusedImports14.symbols new file mode 100644 index 00000000000..3ac44d17533 --- /dev/null +++ b/tests/baselines/reference/unusedImports14.symbols @@ -0,0 +1,36 @@ +=== tests/cases/compiler/foo.tsx === + +import React = require("react"); +>React : Symbol(React, Decl(foo.tsx, 0, 0)) + +export const FooComponent =
+>FooComponent : Symbol(FooComponent, Decl(foo.tsx, 3, 12)) +>div : Symbol(unknown) +>div : Symbol(unknown) + +=== tests/cases/compiler/node_modules/@types/react/index.d.ts === +export = React; +>React : Symbol(React, Decl(index.d.ts, 1, 26)) + +export as namespace React; +>React : Symbol(React, Decl(index.d.ts, 0, 15)) + +declare namespace React { +>React : Symbol(React, Decl(index.d.ts, 1, 26)) + + function createClass(spec); +>createClass : Symbol(createClass, Decl(index.d.ts, 3, 25)) +>P : Symbol(P, Decl(index.d.ts, 4, 25)) +>S : Symbol(S, Decl(index.d.ts, 4, 27)) +>spec : Symbol(spec, Decl(index.d.ts, 4, 31)) +} +declare global { +>global : Symbol(global, Decl(index.d.ts, 5, 1)) + + namespace JSX { +>JSX : Symbol(JSX, Decl(index.d.ts, 6, 16)) + } +} + + + diff --git a/tests/baselines/reference/unusedImports14.types b/tests/baselines/reference/unusedImports14.types new file mode 100644 index 00000000000..b19531b3e2d --- /dev/null +++ b/tests/baselines/reference/unusedImports14.types @@ -0,0 +1,37 @@ +=== tests/cases/compiler/foo.tsx === + +import React = require("react"); +>React : typeof React + +export const FooComponent =
+>FooComponent : any +>
: any +>div : any +>div : any + +=== tests/cases/compiler/node_modules/@types/react/index.d.ts === +export = React; +>React : typeof React + +export as namespace React; +>React : typeof React + +declare namespace React { +>React : typeof React + + function createClass(spec); +>createClass : (spec: any) => any +>P : P +>S : S +>spec : any +} +declare global { +>global : any + + namespace JSX { +>JSX : any + } +} + + + diff --git a/tests/cases/compiler/unusedImports13.ts b/tests/cases/compiler/unusedImports13.ts new file mode 100644 index 00000000000..875e36abab2 --- /dev/null +++ b/tests/cases/compiler/unusedImports13.ts @@ -0,0 +1,23 @@ +//@noUnusedLocals:true +//@noUnusedParameters:true +//@module: commonjs +//@jsx: preserve + +// @filename: foo.tsx +import React = require("react"); + +export const FooComponent =
+ +// @filename: node_modules/@types/react/index.d.ts +export = React; +export as namespace React; + +declare namespace React { + function createClass(spec); +} +declare global { + namespace JSX { + } +} + + diff --git a/tests/cases/compiler/unusedImports14.ts b/tests/cases/compiler/unusedImports14.ts new file mode 100644 index 00000000000..89e4b31cb13 --- /dev/null +++ b/tests/cases/compiler/unusedImports14.ts @@ -0,0 +1,23 @@ +//@noUnusedLocals:true +//@noUnusedParameters:true +//@module: commonjs +//@jsx: react + +// @filename: foo.tsx +import React = require("react"); + +export const FooComponent =
+ +// @filename: node_modules/@types/react/index.d.ts +export = React; +export as namespace React; + +declare namespace React { + function createClass(spec); +} +declare global { + namespace JSX { + } +} + + From 96a7b7b00f108cad190257c019d07d60abd83b41 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 18 Oct 2016 12:24:41 -0700 Subject: [PATCH 2/3] Mark local "react" symbol as referenced since it might not be marked if there was no error message being displayed Fixes #10312 --- src/compiler/checker.ts | 10 ++++- .../reference/unusedImports13.errors.txt | 25 ------------- .../reference/unusedImports13.symbols | 36 ++++++++++++++++++ .../baselines/reference/unusedImports13.types | 37 +++++++++++++++++++ 4 files changed, 81 insertions(+), 27 deletions(-) delete mode 100644 tests/baselines/reference/unusedImports13.errors.txt create mode 100644 tests/baselines/reference/unusedImports13.symbols create mode 100644 tests/baselines/reference/unusedImports13.types diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d3f15f2367b..a4d53c40d4d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11070,14 +11070,20 @@ namespace ts { function checkJsxOpeningLikeElement(node: JsxOpeningLikeElement) { checkGrammarJsxElement(node); checkJsxPreconditions(node); - // The reactNamespace symbol should be marked as 'used' so we don't incorrectly elide its import. And if there // is no reactNamespace symbol in scope when targeting React emit, we should issue an error. const reactRefErr = compilerOptions.jsx === JsxEmit.React ? Diagnostics.Cannot_find_name_0 : undefined; const reactNamespace = compilerOptions.reactNamespace ? compilerOptions.reactNamespace : "React"; const reactSym = resolveName(node.tagName, reactNamespace, SymbolFlags.Value, reactRefErr, reactNamespace); if (reactSym) { - getSymbolLinks(reactSym).referenced = true; + // Mark local symbol as referenced here because it might not have been marked + // if jsx emit was not react as there wont be error being emitted + reactSym.isReferenced = true; + + // If react symbol is alias, mark it as refereced + if (reactSym.flags & SymbolFlags.Alias && !isConstEnumOrConstEnumOnlyModule(resolveAlias(reactSym))) { + markAliasSymbolAsReferenced(reactSym); + } } const targetAttributesType = getJsxElementAttributesType(node); diff --git a/tests/baselines/reference/unusedImports13.errors.txt b/tests/baselines/reference/unusedImports13.errors.txt deleted file mode 100644 index 0e870795eb6..00000000000 --- a/tests/baselines/reference/unusedImports13.errors.txt +++ /dev/null @@ -1,25 +0,0 @@ -tests/cases/compiler/foo.tsx(2,8): error TS6133: 'React' is declared but never used. - - -==== tests/cases/compiler/foo.tsx (1 errors) ==== - - import React = require("react"); - ~~~~~ -!!! error TS6133: 'React' is declared but never used. - - export const FooComponent =
- -==== tests/cases/compiler/node_modules/@types/react/index.d.ts (0 errors) ==== - export = React; - export as namespace React; - - declare namespace React { - function createClass(spec); - } - declare global { - namespace JSX { - } - } - - - \ No newline at end of file diff --git a/tests/baselines/reference/unusedImports13.symbols b/tests/baselines/reference/unusedImports13.symbols new file mode 100644 index 00000000000..3ac44d17533 --- /dev/null +++ b/tests/baselines/reference/unusedImports13.symbols @@ -0,0 +1,36 @@ +=== tests/cases/compiler/foo.tsx === + +import React = require("react"); +>React : Symbol(React, Decl(foo.tsx, 0, 0)) + +export const FooComponent =
+>FooComponent : Symbol(FooComponent, Decl(foo.tsx, 3, 12)) +>div : Symbol(unknown) +>div : Symbol(unknown) + +=== tests/cases/compiler/node_modules/@types/react/index.d.ts === +export = React; +>React : Symbol(React, Decl(index.d.ts, 1, 26)) + +export as namespace React; +>React : Symbol(React, Decl(index.d.ts, 0, 15)) + +declare namespace React { +>React : Symbol(React, Decl(index.d.ts, 1, 26)) + + function createClass(spec); +>createClass : Symbol(createClass, Decl(index.d.ts, 3, 25)) +>P : Symbol(P, Decl(index.d.ts, 4, 25)) +>S : Symbol(S, Decl(index.d.ts, 4, 27)) +>spec : Symbol(spec, Decl(index.d.ts, 4, 31)) +} +declare global { +>global : Symbol(global, Decl(index.d.ts, 5, 1)) + + namespace JSX { +>JSX : Symbol(JSX, Decl(index.d.ts, 6, 16)) + } +} + + + diff --git a/tests/baselines/reference/unusedImports13.types b/tests/baselines/reference/unusedImports13.types new file mode 100644 index 00000000000..b19531b3e2d --- /dev/null +++ b/tests/baselines/reference/unusedImports13.types @@ -0,0 +1,37 @@ +=== tests/cases/compiler/foo.tsx === + +import React = require("react"); +>React : typeof React + +export const FooComponent =
+>FooComponent : any +>
: any +>div : any +>div : any + +=== tests/cases/compiler/node_modules/@types/react/index.d.ts === +export = React; +>React : typeof React + +export as namespace React; +>React : typeof React + +declare namespace React { +>React : typeof React + + function createClass(spec); +>createClass : (spec: any) => any +>P : P +>S : S +>spec : any +} +declare global { +>global : any + + namespace JSX { +>JSX : any + } +} + + + From 23e9e0ba63095a38f6bfa6ebb412610aad1300b2 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 18 Oct 2016 12:24:41 -0700 Subject: [PATCH 3/3] Adding testcases for reactnamespace --- tests/baselines/reference/unusedImports15.js | 27 ++++++++++++++ .../reference/unusedImports15.symbols | 36 ++++++++++++++++++ .../baselines/reference/unusedImports15.types | 37 +++++++++++++++++++ tests/baselines/reference/unusedImports16.js | 27 ++++++++++++++ .../reference/unusedImports16.symbols | 36 ++++++++++++++++++ .../baselines/reference/unusedImports16.types | 37 +++++++++++++++++++ tests/cases/compiler/unusedImports13.ts | 1 - tests/cases/compiler/unusedImports14.ts | 1 - tests/cases/compiler/unusedImports15.ts | 23 ++++++++++++ tests/cases/compiler/unusedImports16.ts | 23 ++++++++++++ 10 files changed, 246 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/unusedImports15.js create mode 100644 tests/baselines/reference/unusedImports15.symbols create mode 100644 tests/baselines/reference/unusedImports15.types create mode 100644 tests/baselines/reference/unusedImports16.js create mode 100644 tests/baselines/reference/unusedImports16.symbols create mode 100644 tests/baselines/reference/unusedImports16.types create mode 100644 tests/cases/compiler/unusedImports15.ts create mode 100644 tests/cases/compiler/unusedImports16.ts diff --git a/tests/baselines/reference/unusedImports15.js b/tests/baselines/reference/unusedImports15.js new file mode 100644 index 00000000000..01be756e0e5 --- /dev/null +++ b/tests/baselines/reference/unusedImports15.js @@ -0,0 +1,27 @@ +//// [tests/cases/compiler/unusedImports15.ts] //// + +//// [foo.tsx] + +import Element = require("react"); + +export const FooComponent =
+ +//// [index.d.ts] +export = React; +export as namespace React; + +declare namespace React { + function createClass(spec); +} +declare global { + namespace JSX { + } +} + + + + +//// [foo.jsx] +"use strict"; +var Element = require("react"); +exports.FooComponent =
; diff --git a/tests/baselines/reference/unusedImports15.symbols b/tests/baselines/reference/unusedImports15.symbols new file mode 100644 index 00000000000..a325dc7ed76 --- /dev/null +++ b/tests/baselines/reference/unusedImports15.symbols @@ -0,0 +1,36 @@ +=== tests/cases/compiler/foo.tsx === + +import Element = require("react"); +>Element : Symbol(Element, Decl(foo.tsx, 0, 0)) + +export const FooComponent =
+>FooComponent : Symbol(FooComponent, Decl(foo.tsx, 3, 12)) +>div : Symbol(unknown) +>div : Symbol(unknown) + +=== tests/cases/compiler/node_modules/@types/react/index.d.ts === +export = React; +>React : Symbol(React, Decl(index.d.ts, 1, 26)) + +export as namespace React; +>React : Symbol(React, Decl(index.d.ts, 0, 15)) + +declare namespace React { +>React : Symbol(React, Decl(index.d.ts, 1, 26)) + + function createClass(spec); +>createClass : Symbol(createClass, Decl(index.d.ts, 3, 25)) +>P : Symbol(P, Decl(index.d.ts, 4, 25)) +>S : Symbol(S, Decl(index.d.ts, 4, 27)) +>spec : Symbol(spec, Decl(index.d.ts, 4, 31)) +} +declare global { +>global : Symbol(global, Decl(index.d.ts, 5, 1)) + + namespace JSX { +>JSX : Symbol(JSX, Decl(index.d.ts, 6, 16)) + } +} + + + diff --git a/tests/baselines/reference/unusedImports15.types b/tests/baselines/reference/unusedImports15.types new file mode 100644 index 00000000000..c222eaa03a3 --- /dev/null +++ b/tests/baselines/reference/unusedImports15.types @@ -0,0 +1,37 @@ +=== tests/cases/compiler/foo.tsx === + +import Element = require("react"); +>Element : typeof Element + +export const FooComponent =
+>FooComponent : any +>
: any +>div : any +>div : any + +=== tests/cases/compiler/node_modules/@types/react/index.d.ts === +export = React; +>React : typeof React + +export as namespace React; +>React : typeof React + +declare namespace React { +>React : typeof React + + function createClass(spec); +>createClass : (spec: any) => any +>P : P +>S : S +>spec : any +} +declare global { +>global : any + + namespace JSX { +>JSX : any + } +} + + + diff --git a/tests/baselines/reference/unusedImports16.js b/tests/baselines/reference/unusedImports16.js new file mode 100644 index 00000000000..1ce802d869a --- /dev/null +++ b/tests/baselines/reference/unusedImports16.js @@ -0,0 +1,27 @@ +//// [tests/cases/compiler/unusedImports16.ts] //// + +//// [foo.tsx] + +import Element = require("react"); + +export const FooComponent =
+ +//// [index.d.ts] +export = React; +export as namespace React; + +declare namespace React { + function createClass(spec); +} +declare global { + namespace JSX { + } +} + + + + +//// [foo.js] +"use strict"; +var Element = require("react"); +exports.FooComponent = Element.createElement("div", null); diff --git a/tests/baselines/reference/unusedImports16.symbols b/tests/baselines/reference/unusedImports16.symbols new file mode 100644 index 00000000000..a325dc7ed76 --- /dev/null +++ b/tests/baselines/reference/unusedImports16.symbols @@ -0,0 +1,36 @@ +=== tests/cases/compiler/foo.tsx === + +import Element = require("react"); +>Element : Symbol(Element, Decl(foo.tsx, 0, 0)) + +export const FooComponent =
+>FooComponent : Symbol(FooComponent, Decl(foo.tsx, 3, 12)) +>div : Symbol(unknown) +>div : Symbol(unknown) + +=== tests/cases/compiler/node_modules/@types/react/index.d.ts === +export = React; +>React : Symbol(React, Decl(index.d.ts, 1, 26)) + +export as namespace React; +>React : Symbol(React, Decl(index.d.ts, 0, 15)) + +declare namespace React { +>React : Symbol(React, Decl(index.d.ts, 1, 26)) + + function createClass(spec); +>createClass : Symbol(createClass, Decl(index.d.ts, 3, 25)) +>P : Symbol(P, Decl(index.d.ts, 4, 25)) +>S : Symbol(S, Decl(index.d.ts, 4, 27)) +>spec : Symbol(spec, Decl(index.d.ts, 4, 31)) +} +declare global { +>global : Symbol(global, Decl(index.d.ts, 5, 1)) + + namespace JSX { +>JSX : Symbol(JSX, Decl(index.d.ts, 6, 16)) + } +} + + + diff --git a/tests/baselines/reference/unusedImports16.types b/tests/baselines/reference/unusedImports16.types new file mode 100644 index 00000000000..c222eaa03a3 --- /dev/null +++ b/tests/baselines/reference/unusedImports16.types @@ -0,0 +1,37 @@ +=== tests/cases/compiler/foo.tsx === + +import Element = require("react"); +>Element : typeof Element + +export const FooComponent =
+>FooComponent : any +>
: any +>div : any +>div : any + +=== tests/cases/compiler/node_modules/@types/react/index.d.ts === +export = React; +>React : typeof React + +export as namespace React; +>React : typeof React + +declare namespace React { +>React : typeof React + + function createClass(spec); +>createClass : (spec: any) => any +>P : P +>S : S +>spec : any +} +declare global { +>global : any + + namespace JSX { +>JSX : any + } +} + + + diff --git a/tests/cases/compiler/unusedImports13.ts b/tests/cases/compiler/unusedImports13.ts index 875e36abab2..af188e70eae 100644 --- a/tests/cases/compiler/unusedImports13.ts +++ b/tests/cases/compiler/unusedImports13.ts @@ -1,5 +1,4 @@ //@noUnusedLocals:true -//@noUnusedParameters:true //@module: commonjs //@jsx: preserve diff --git a/tests/cases/compiler/unusedImports14.ts b/tests/cases/compiler/unusedImports14.ts index 89e4b31cb13..1823d4bb49b 100644 --- a/tests/cases/compiler/unusedImports14.ts +++ b/tests/cases/compiler/unusedImports14.ts @@ -1,5 +1,4 @@ //@noUnusedLocals:true -//@noUnusedParameters:true //@module: commonjs //@jsx: react diff --git a/tests/cases/compiler/unusedImports15.ts b/tests/cases/compiler/unusedImports15.ts new file mode 100644 index 00000000000..ef6b277d21d --- /dev/null +++ b/tests/cases/compiler/unusedImports15.ts @@ -0,0 +1,23 @@ +//@noUnusedLocals:true +//@module: commonjs +//@reactNamespace: Element +//@jsx: preserve + +// @filename: foo.tsx +import Element = require("react"); + +export const FooComponent =
+ +// @filename: node_modules/@types/react/index.d.ts +export = React; +export as namespace React; + +declare namespace React { + function createClass(spec); +} +declare global { + namespace JSX { + } +} + + diff --git a/tests/cases/compiler/unusedImports16.ts b/tests/cases/compiler/unusedImports16.ts new file mode 100644 index 00000000000..ff0c38bb75e --- /dev/null +++ b/tests/cases/compiler/unusedImports16.ts @@ -0,0 +1,23 @@ +//@noUnusedLocals:true +//@module: commonjs +//@reactNamespace: Element +//@jsx: react + +// @filename: foo.tsx +import Element = require("react"); + +export const FooComponent =
+ +// @filename: node_modules/@types/react/index.d.ts +export = React; +export as namespace React; + +declare namespace React { + function createClass(spec); +} +declare global { + namespace JSX { + } +} + +