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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
node_modules/

## IDE
.idea/
.idea/

## Jest
coverage/
53 changes: 53 additions & 0 deletions src/utils/__tests__/getParameterName-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.autoMockOff();

describe('getParameterName', () => {
let getParameterName;
let expression;

beforeEach(() => {
getParameterName = require('../getParameterName');
({expression} = require('../../../tests/utils'));
});

it('returns the name for a normal parameter', () => {
const def = expression('function(a) {}');
const param = def.get('params', 0);
expect(getParameterName(param)).toEqual('a');
});

it('returns the name for a rest parameter', () => {
const def = expression('function(...a) {}');
const param = def.get('params', 0);
expect(getParameterName(param)).toEqual('...a');
});

it('returns the name for a parameter with a default value', () => {
const def = expression('function(a = 0) {}');
const param = def.get('params', 0);
expect(getParameterName(param)).toEqual('a');
});

it('returns the raw object representation for a parameter with destructuring', () => {
const def = expression('function({a}) {}');
const param = def.get('params', 0);
expect(getParameterName(param)).toEqual('{a}');
});

it('throws when passed an invalid path', () => {
const def = expression('function() {}');
const param = def;
expect(() => getParameterName(param)).toThrow();
});
});
3 changes: 2 additions & 1 deletion src/utils/getMethodDocumentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import {getDocblock} from './docblock';
import getFlowType from './getFlowType';
import getParameterName from './getParameterName';
import getPropertyName from './getPropertyName';
import getTypeAnnotation from './getTypeAnnotation';

Expand Down Expand Up @@ -44,7 +45,7 @@ function getMethodParamsDoc(methodPath, jsDoc) {
}

const param = {
name: paramPath.node.name,
name: getParameterName(paramPath),
type,
};

Expand Down
34 changes: 34 additions & 0 deletions src/utils/getParameterName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.
*
* @flow
*/

import recast from 'recast';

import printValue from './printValue';

const {types: {namedTypes: types}} = recast;

export default function getParameterName(parameterPath: NodePath): string {
switch (parameterPath.node.type) {
case types.Identifier.name:
return parameterPath.node.name;
case types.AssignmentPattern.name:
return getParameterName(parameterPath.get('left'));
case types.ObjectPattern.name:
return printValue(parameterPath);
case types.RestElement.name:
return '...' + getParameterName(parameterPath.get('argument'));
default:
throw new TypeError(
'Parameter name must be an Identifier, an AssignmentPattern an ' +
`ObjectPattern or a RestElement, got ${parameterPath.node.type}`
);
}
}
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export {default as getMembers} from './getMembers';
export {default as getMemberValuePath} from './getMemberValuePath';
export {default as getMethodDocumentation} from './getMethodDocumentation';
export {default as getNameOrValue} from './getNameOrValue';
export {default as getParameterName} from './getParameterName';
export {default as getPropertyName} from './getPropertyName';
export {default as getPropertyValuePath} from './getPropertyValuePath';
export {default as getPropType} from './getPropType';
Expand Down
17 changes: 9 additions & 8 deletions src/utils/isReactComponentMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ import getPropertyName from './getPropertyName';
const {types: {namedTypes: types}} = recast;

const componentMethods = [
'render',
'getInitialState',
'getDefaultProps',
'getChildContext',
'componentWillMount',
'componentDidMount',
'componentWillReceiveProps',
'shouldComponentUpdate',
'componentWillUpdate',
'componentDidReceiveProps',
'componentDidUpdate',
'componentWillMount',
'componentWillReceiveProps',
'componentWillUnmount',
'componentWillUpdate',
'getChildContext',
'getDefaultProps',
'getInitialState',
'render',
'shouldComponentUpdate',
];

/**
Expand Down