Skip to content

Commit 76496f5

Browse files
committed
Update utils to ES6; fix lint errors
1 parent 9612a15 commit 76496f5

16 files changed

+202
-222
lines changed

src/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import * as handlers from './handlers';
1414
import parse from './parse';
1515
import * as resolver from './resolver';
16-
import utils from './utils';
16+
import * as utils from './utils';
1717

1818
const defaultResolver = resolver.findExportedComponentDefinition;
1919
const defaultHandlers = [

src/utils/docblock.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
/*
2-
* Copyright (c) 2015, Facebook, Inc.
3-
* All rights reserved.
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
44
*
5-
* This source code is licensed under the BSD-style license found in the
6-
* LICENSE file in the root directory of this source tree. An additional grant
7-
* of patent rights can be found in the PATENTS file in the same directory.
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
88
*
9+
* @flow
910
*/
1011

1112
/**
1213
* Helper functions to work with docblock comments.
13-
* @flow
1414
*/
15-
"use strict";
1615

17-
var types = require('recast').types.namedTypes;
18-
var docletPattern = /^@(\w+)(?:$|\s((?:[^](?!^@\w))*))/gmi;
16+
import recast from 'recast';
17+
18+
const DOCLET_PATTERN = /^@(\w+)(?:$|\s((?:[^](?!^@\w))*))/gmi;
1919

2020
function parseDocblock(str) {
2121
var lines = str.split('\n');
@@ -29,7 +29,7 @@ function parseDocblock(str) {
2929
* Given a path, this function returns the closest preceding docblock if it
3030
* exists.
3131
*/
32-
function getDocblock(path: NodePath): ?string {
32+
export function getDocblock(path: NodePath): ?string {
3333
if (path.node.comments) {
3434
var comments = path.node.comments.filter(function(comment) {
3535
return comment.leading &&
@@ -47,16 +47,13 @@ function getDocblock(path: NodePath): ?string {
4747
* Given a string, this functions returns an object with doclet names as keys
4848
* and their "content" as values.
4949
*/
50-
function getDoclets(str: string): Object {
50+
export function getDoclets(str: string): Object {
5151
var doclets = Object.create(null);
52-
var match = docletPattern.exec(str);
52+
var match = DOCLET_PATTERN.exec(str);
5353

54-
for (; match; match = docletPattern.exec(str)) {
54+
for (; match; match = DOCLET_PATTERN.exec(str)) {
5555
doclets[match[1]] = match[2] || true;
5656
}
5757

5858
return doclets;
5959
}
60-
61-
exports.getDocblock = getDocblock;
62-
exports.getDoclets = getDoclets;

src/utils/expressionTo.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
/*
2-
* Copyright (c) 2015, Facebook, Inc.
3-
* All rights reserved.
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
44
*
5-
* This source code is licensed under the BSD-style license found in the
6-
* LICENSE file in the root directory of this source tree. An additional grant
7-
* of patent rights can be found in the PATENTS file in the same directory.
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
88
*
9-
*/
10-
11-
/**
129
* @flow
10+
*
1311
*/
14-
"use strict";
1512

16-
var resolveToValue = require('./resolveToValue');
17-
var types = require('recast').types.namedTypes;
13+
/*eslint no-loop-func: 0, no-use-before-define: 0*/
14+
15+
import resolveToValue from './resolveToValue';
16+
import recast from 'recast';
17+
18+
var {types: {namedTypes: types}} = recast;
1819

1920
/**
2021
* Splits a MemberExpression or CallExpression into parts.
@@ -76,5 +77,7 @@ function toString(path: NodePath): string {
7677
return toArray(path).join('.');
7778
}
7879

79-
exports.String = toString;
80-
exports.Array = toArray;
80+
export {
81+
toString as String,
82+
toArray as Array,
83+
}

src/utils/getMembers.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
/*
2-
* Copyright (c) 2015, Facebook, Inc.
3-
* All rights reserved.
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
44
*
5-
* This source code is licensed under the BSD-style license found in the
6-
* LICENSE file in the root directory of this source tree. An additional grant
7-
* of patent rights can be found in the PATENTS file in the same directory.
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @flow
810
*
911
*/
1012

13+
/*eslint no-labels: 0*/
14+
1115
/**
1216
* Helper methods for dealing with MemberExpressions (and CallExpressions).
13-
* @flow
1417
*/
15-
"use strict";
1618

17-
type MemberDescriptor = {
19+
type MemberDescriptor = { // eslint-disable-line no-undef
1820
path: NodePath;
1921
computed: boolean;
2022
argumentsPath?: ?NodePath;
2123
}
2224

23-
var types = require('recast').types.namedTypes;
25+
import recast from 'recast';
26+
27+
var {types: {namedTypes: types}} = recast;
2428

2529
/**
2630
* Given a "nested" Member/CallExpression, e.g.
@@ -34,10 +38,10 @@ var types = require('recast').types.namedTypes;
3438
* {path: NodePath<42>, arguments: null, computed: false}
3539
* ]
3640
*/
37-
function getMembers(path: NodePath): Array<MemberExpression> {
41+
export default function getMembers(path: NodePath): Array<MemberExpression> {
3842
var result = [];
3943
var argumentsPath = null;
40-
loop: while(true) {
44+
loop: while(true) { // eslint-disable-line no-constant-condition
4145
switch (true) {
4246
case types.MemberExpression.check(path.node):
4347
result.push({

src/utils/getNameOrValue.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
/*
2-
* Copyright (c) 2015, Facebook, Inc.
3-
* All rights reserved.
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
44
*
5-
* This source code is licensed under the BSD-style license found in the
6-
* LICENSE file in the root directory of this source tree. An additional grant
7-
* of patent rights can be found in the PATENTS file in the same directory.
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
88
*
9-
*/
10-
11-
/**
129
* @flow
10+
*
1311
*/
14-
"use strict";
1512

16-
var types = require('recast').types.namedTypes;
13+
import recast from 'recast';
14+
15+
var {types: {namedTypes: types}} = recast;
1716

1817
/**
1918
* If node is an Identifier, it returns its name. If it is a literal, it returns
2019
* its value.
2120
*/
22-
function getNameOrValue(path: NodePath, raw?: boolean): string {
21+
export default function getNameOrValue(path: NodePath, raw?: boolean): string {
2322
var node = path.node;
2423
switch (node.type) {
2524
case types.Identifier.name:
@@ -30,5 +29,3 @@ function getNameOrValue(path: NodePath, raw?: boolean): string {
3029
throw new TypeError('Argument must be an Identifier or a Literal');
3130
}
3231
}
33-
34-
module.exports = getNameOrValue;

src/utils/getPropType.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
/*
2-
* Copyright (c) 2015, Facebook, Inc.
3-
* All rights reserved.
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
44
*
5-
* This source code is licensed under the BSD-style license found in the
6-
* LICENSE file in the root directory of this source tree. An additional grant
7-
* of patent rights can be found in the PATENTS file in the same directory.
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
88
*
9-
*/
10-
11-
/**
129
* @flow
10+
*
1311
*/
14-
"use strict";
1512

16-
var getMembers = require('./getMembers');
17-
var getPropertyName = require('./getPropertyName');
18-
var printValue = require('./printValue');
19-
var recast = require('recast');
20-
var resolveToValue = require('./resolveToValue');
13+
/*eslint no-use-before-define: 0*/
2114

22-
var types = recast.types.namedTypes;
15+
16+
import getMembers from './getMembers';
17+
import getPropertyName from './getPropertyName';
18+
import printValue from './printValue';
19+
import recast from 'recast';
20+
import resolveToValue from './resolveToValue';
21+
22+
var {types: {namedTypes: types}} = recast;
2323

2424
function getEnumValues(path) {
2525
return path.get('elements').map(function(elementPath) {
2626
return {
2727
value: printValue(elementPath),
28-
computed: !types.Literal.check(elementPath.node)
28+
computed: !types.Literal.check(elementPath.node),
2929
};
3030
});
3131
}
@@ -85,7 +85,7 @@ function getPropTypeShape(argumentPath) {
8585
function getPropTypeInstanceOf(argumentPath) {
8686
return {
8787
name: 'instanceOf',
88-
value: printValue(argumentPath)
88+
value: printValue(argumentPath),
8989
};
9090
}
9191

@@ -98,15 +98,15 @@ var simplePropTypes = {
9898
string: 1,
9999
any: 1,
100100
element: 1,
101-
node: 1
101+
node: 1,
102102
};
103103

104104
var propTypes = {
105105
oneOf: getPropTypeOneOf,
106106
oneOfType: getPropTypeOneOfType,
107107
instanceOf: getPropTypeInstanceOf,
108108
arrayOf: getPropTypeArrayOf,
109-
shape: getPropTypeShape
109+
shape: getPropTypeShape,
110110
};
111111

112112
/**
@@ -117,7 +117,7 @@ var propTypes = {
117117
*
118118
* If there is no match, "custom" is returned.
119119
*/
120-
function getPropType(path: NodePath): PropTypeDescriptor {
120+
export default function getPropType(path: NodePath): PropTypeDescriptor {
121121
var node = path.node;
122122
var descriptor;
123123
getMembers(path).some(member => {
@@ -150,5 +150,3 @@ function getPropType(path: NodePath): PropTypeDescriptor {
150150
}
151151
return descriptor;
152152
}
153-
154-
module.exports = getPropType;

src/utils/getPropertyName.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
11
/*
2-
* Copyright (c) 2015, Facebook, Inc.
3-
* All rights reserved.
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
44
*
5-
* This source code is licensed under the BSD-style license found in the
6-
* LICENSE file in the root directory of this source tree. An additional grant
7-
* of patent rights can be found in the PATENTS file in the same directory.
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
88
*
9-
*/
10-
11-
/**
129
* @flow
10+
*
1311
*/
14-
"use strict";
1512

16-
var getNameOrValue = require('./getNameOrValue');
17-
var types = require('recast').types.namedTypes;
13+
import getNameOrValue from './getNameOrValue';
1814

1915
/**
2016
* In an ObjectExpression, the name of a property can either be an identifier
2117
* or a literal (or dynamic, but we don't support those). This function simply
2218
* returns the value of the literal or name of the identifier.
2319
*/
24-
function getPropertyName(propertyPath: NodePath): string {
20+
export default function getPropertyName(propertyPath: NodePath): string {
2521
if (propertyPath.node.computed) {
2622
throw new TypeError('Propery name must be an Identifier or a Literal');
2723
}
2824

2925
return getNameOrValue(propertyPath.get('key'), false);
3026
}
31-
32-
module.exports = getPropertyName;

src/utils/getPropertyValuePath.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
/*
2-
* Copyright (c) 2015, Facebook, Inc.
3-
* All rights reserved.
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
44
*
5-
* This source code is licensed under the BSD-style license found in the
6-
* LICENSE file in the root directory of this source tree. An additional grant
7-
* of patent rights can be found in the PATENTS file in the same directory.
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
88
*
9-
*/
10-
11-
/**
129
* @flow
10+
*
1311
*/
14-
"use strict";
1512

16-
var types = require('recast').types.namedTypes;
17-
var getPropertyName = require('./getPropertyName');
13+
import getPropertyName from './getPropertyName';
14+
import recast from 'recast';
15+
16+
var {types: {namedTypes: types}} = recast;
1817

1918
/**
2019
* Given an ObjectExpression, this function returns the path of the value of
2120
* the property with name `propertyName`.
2221
*/
23-
function getPropertyValuePath(path: NodePath, propertyName: string): ?NodePath {
22+
export default function getPropertyValuePath(
23+
path: NodePath,
24+
propertyName: string
25+
): ?NodePath {
2426
types.ObjectExpression.assert(path.node);
2527

2628
return path.get('properties')
2729
.filter(propertyPath => getPropertyName(propertyPath) === propertyName)
2830
.map(propertyPath => propertyPath.get('value'))[0];
2931
}
30-
31-
module.exports = getPropertyValuePath;

0 commit comments

Comments
 (0)