Skip to content

Commit

Permalink
Allow member expression identifiers when determining React displayName (
Browse files Browse the repository at this point in the history
#274)

Fixes #253

Also fix stale comment.
  • Loading branch information
alangpierce committed Jun 25, 2018
1 parent f63fc3d commit 40af8e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
13 changes: 3 additions & 10 deletions src/transformers/ReactDisplayNameTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import Transformer from "./Transformer";
/**
* Implementation of babel-plugin-transform-react-display-name, which adds a
* display name to usages of React.createClass and createReactClass.
*
* This implementation has the following limitations compared with the
* - It does not handle `export default React.createClass`, using the filename,
* since Sucrase currently does not know the name of the current file.
*/
export default class ReactDisplayNameTransformer extends Transformer {
constructor(
Expand Down Expand Up @@ -83,12 +79,9 @@ export default class ReactDisplayNameTransformer extends Transformer {
}

private findDisplayName(startIndex: number): string | null {
if (
this.tokens.matchesAtIndex(startIndex - 2, [tt.name, tt.eq]) &&
!this.tokens.matchesAtIndex(startIndex - 3, [tt.dot])
) {
// This is an assignment (or declaration) with an identifier LHS, so use
// that identifier name.
if (this.tokens.matchesAtIndex(startIndex - 2, [tt.name, tt.eq])) {
// This is an assignment (or declaration) and the LHS is either an identifier or a member
// expression ending in an identifier, so use that identifier name.
return this.tokens.identifierNameAtIndex(startIndex - 2);
}
if (
Expand Down
22 changes: 22 additions & 0 deletions test/react-display-name-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,26 @@ describe("transform react-display-name", () => {
{transforms: ["jsx", "imports"], filePath: "MyComponent.js"},
);
});

it("properly reads a MemberExpression assignee", () => {
assertResult(
`
import React from 'react';
a.b.c = React.createClass({
render() {
return <a/>
}
});
`,
`"use strict";const _jsxFileName = "MyComponent.js";${IMPORT_DEFAULT_PREFIX}
var _react = require('react'); var _react2 = _interopRequireDefault(_react);
a.b.c = _react2.default.createClass({displayName: 'c',
render() {
return _react2.default.createElement('a', {${devProps(5)}})
}
});
`,
{transforms: ["jsx", "imports"], filePath: "MyComponent.js"},
);
});
});

0 comments on commit 40af8e5

Please sign in to comment.