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

Children of JSX Fragments not recognized as "read." #19939

Closed
phyllisstein opened this issue Nov 11, 2017 · 1 comment
Closed

Children of JSX Fragments not recognized as "read." #19939

phyllisstein opened this issue Nov 11, 2017 · 1 comment
Assignees
Labels
Bug A bug in TypeScript Domain: JSX/TSX Relates to the JSX parser and emitter Fixed A PR has been merged for this issue

Comments

@phyllisstein
Copy link

phyllisstein commented Nov 11, 2017

Hey folks! I was taking the new JSX fragments syntax for a spin and noticed that there seems to be a glitch when it's used in combination with the --noUnusedLocals flag. Looks like TypeScript doesn't recognize declared variables as "read" when they're used within a JSX fragment.

One particularly insidious consequence of this bug is that the compiler will strip out imports that it believes are unused---in the sample below, the import for MyComponent is removed from the emitted code when it appears within a fragment, but not when it appears between (e.g.) <div />s.


TypeScript Version: 2.7.0-dev.20171110

Code

~ ❯❯❯ tsc --jsx react --noUnusedLocals test.tsx
// test.tsx

import * as React from 'react'
import MyComponent from './my-component'

const MY_STRING: string = 'Ceci n\'est pas une string.'
const MY_CLASSNAME: string = 'jeclass'

class RenderString extends React.PureComponent {
  render() {
    return (
      <>
        <MyComponent />
        <span>{ MY_STRING }</span>
        <span className={ MY_CLASSNAME } />
      </>
    )
  }
}

export default RenderString

Expected behavior:
TypeScript should recognize that MyComponent, MY_STRING, and MY_CLASSNAME are used within the JSX fragment, and not return errors as a consequence of the --noUnusedLocals compiler flag. Additionally, TypeScript should emit a require call for ./my-component.

Actual behavior:
The compiler flags MyComponent, MY_STRING, and MY_CLASSNAME as unused...

~ ❯❯❯ tsc --jsx react --noUnusedLocals test.tsx
test.tsx(2,8): error TS6133: 'MyComponent' is declared but its value is never read.
test.tsx(4,7): error TS6133: 'MY_STRING' is declared but its value is never read.
test.tsx(5,7): error TS6133: 'MY_CLASSNAME' is declared but its value is never read.

...and strips the require('./my-component') line from the emitted JS:

// with the TSX source file above...

var React = require("react");
// 🕵️‍♀️ Whither my require?
var MY_STRING = 'Ceci n\'est pas une string.';
var MY_CLASSNAME = 'jeclass';
var RenderString = /** @class */ (function (_super) {
    __extends(RenderString, _super);
    function RenderString() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    RenderString.prototype.render = function () {
        return (React.createElement(React.Fragment, null,
            React.createElement(my_component_1["default"], null), // 🚫 I don't exist.
            React.createElement("span", null, MY_STRING),
            React.createElement("span", { className: MY_CLASSNAME })));
    };
    return RenderString;
}(React.PureComponent));
// ...compared to using <div />s in place of the fragments.

var React = require("react");
var my_component_1 = require("./my-component"); // 🎉Whew.
var MY_STRING = 'Ceci n\'est pas une string.';
var MY_CLASSNAME = 'jeclass';
var RenderString = /** @class */ (function (_super) {
    __extends(RenderString, _super);
    function RenderString() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    RenderString.prototype.render = function () {
        return (React.createElement("div", null,
            React.createElement(my_component_1["default"], null), // 💃 I'm defined!
            React.createElement("span", null, MY_STRING),
            React.createElement("span", { className: MY_CLASSNAME })));
    };
    return RenderString;
}(React.PureComponent));
@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript Domain: JSX/TSX Relates to the JSX parser and emitter labels Nov 11, 2017
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 2.7 milestone Nov 11, 2017
@uniqueiniquity
Copy link
Contributor

Thanks for reporting this! I'll look into this shortly.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jan 11, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Domain: JSX/TSX Relates to the JSX parser and emitter Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants