Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/__tests__/__snapshots__/main-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,11 @@ Object {
},
}
`;

exports[`main fixtures processes component "component_7.js" without errors 1`] = `
Object {
"description": "The is a component to test the document generation",
"displayName": "Component",
"methods": Array [],
}
`;
22 changes: 22 additions & 0 deletions src/__tests__/fixtures/component_7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

var Component, createReactClass;

createReactClass = require('create-react-class');

/**
* The is a component to test the document generation
*/
Component = createReactClass({
displayName: 'Component',
});

module.exports = Component;
101 changes: 101 additions & 0 deletions src/utils/__tests__/isReactCreateClassCall-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

/*global jest, describe, beforeEach, it, expect*/


jest.disableAutomock();

describe('isReactCreateClassCall', () => {
var isReactCreateClassCall;
var utils;

function parse(src) {
var root = utils.parse(src);
return root.get('body', root.node.body.length - 1, 'expression');
}

beforeEach(() => {
isReactCreateClassCall = require('../isReactCreateClassCall').default;
utils = require('../../../tests/utils');
});

describe('built in React.createClass', () => {
it('accepts createClass called on React', () => {
var def = parse(`
var React = require("React");
React.createClass({
render() {}
});
`);
expect(isReactCreateClassCall(def)).toBe(true);
});

it('accepts createClass called on aliased React', () => {
var def = parse(`
var other = require("React");
other.createClass({
render() {}
});
`);
expect(isReactCreateClassCall(def)).toBe(true);
});

it('ignores other React calls', () => {
var def = parse(`
var React = require("React");
React.isValidElement({});
`);
expect(isReactCreateClassCall(def)).toBe(false);
});

it('ignores non React calls to createClass', () => {
var def = parse(`
var React = require("bob");
React.createClass({
render() {}
});
`);
expect(isReactCreateClassCall(def)).toBe(false);
});
});

describe('modular in create-react-class', () => {
it('accepts create-react-class', () => {
var def = parse(`
var createReactClass = require("create-react-class");
createReactClass({
render() {}
});
`);
expect(isReactCreateClassCall(def)).toBe(true);
});

it('accepts create-react-class calls on another name', () => {
var def = parse(`
var makeClass = require("create-react-class");
makeClass({
render() {}
});
`);
expect(isReactCreateClassCall(def)).toBe(true);
});

it('ignores non create-react-class calls to createReactClass', () => {
var def = parse(`
var createReactClass = require("bob");
createReactClass({
render() {}
});
`);
expect(isReactCreateClassCall(def)).toBe(false);
});
});
});
34 changes: 33 additions & 1 deletion src/utils/isReactCreateClassCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var {types: {namedTypes: types}} = recast;
* Returns true if the expression is a function call of the form
* `React.createClass(...)`.
*/
export default function isReactCreateClassCall(path: NodePath): boolean {
function isReactCreateClassCallBuiltIn(path: NodePath): boolean {
if (types.ExpressionStatement.check(path.node)) {
path = path.get('expression');
}
Expand All @@ -32,3 +32,35 @@ export default function isReactCreateClassCall(path: NodePath): boolean {
var module = resolveToModule(path.get('callee', 'object'));
return Boolean(module && isReactModuleName(module));
}


/**
* Returns true if the expression is a function call of the form
* ```
* import createReactClass from 'create-react-class';
* createReactClass(...);
* ```
*/
function isReactCreateClassCallModular(path: NodePath): boolean {
if (types.ExpressionStatement.check(path.node)) {
path = path.get('expression');
}

if (!match(path.node, {type: 'CallExpression'})) {
return false;
}
var module = resolveToModule(path);
return Boolean(module && module === 'create-react-class');
}

/**
* Returns true if the expression is a function call of the form
* `React.createClass(...)` or
* ```
* import createReactClass from 'create-react-class';
* createReactClass(...);
* ```
*/
export default function isReactCreateClassCall(path: NodePath): boolean {
return isReactCreateClassCallBuiltIn(path) || isReactCreateClassCallModular(path)
}