Skip to content

Commit acd1d53

Browse files
committed
Don't include line comments in prop type values
Fixes #1
1 parent 780b685 commit acd1d53

File tree

6 files changed

+86
-8
lines changed

6 files changed

+86
-8
lines changed

src/handlers/defaultPropsHandler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var Documentation = require('../Documentation');
1717

1818
var getPropertyName = require('../utils/getPropertyName');
1919
var getPropertyValuePath = require('../utils/getPropertyValuePath');
20+
var printValue = require('../utils/printValue');
2021
var recast = require('recast');
2122
var resolveToValue = require('../utils/resolveToValue');
2223
var types = recast.types.namedTypes;
@@ -30,7 +31,7 @@ function getDefaultValue(path) {
3031
} else {
3132
path = resolveToValue(path);
3233
node = path.node;
33-
defaultValue = recast.print(path).code;
34+
defaultValue = printValue(path);
3435
}
3536
if (typeof defaultValue !== 'undefined') {
3637
return {

src/handlers/propTypeHandler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var getPropType = require('../utils/getPropType');
2121
var getPropertyName = require('../utils/getPropertyName');
2222
var getPropertyValuePath = require('../utils/getPropertyValuePath');
2323
var isReactModuleName = require('../utils/isReactModuleName');
24+
var printValue = require('../utils/printValue');
2425
var recast = require('recast');
2526
var resolveToModule = require('../utils/resolveToModule');
2627
var resolveToValue = require('../utils/resolveToValue');
@@ -76,7 +77,7 @@ function amendPropTypes(documentation, path) {
7677
var valuePath = propertyPath.get('value');
7778
var type = isPropTypesExpression(valuePath) ?
7879
getPropType(valuePath) :
79-
{name: 'custom', raw: recast.print(valuePath).code};
80+
{name: 'custom', raw: printValue(valuePath)};
8081

8182
if (type) {
8283
propDescriptor.type = type;

src/utils/__tests__/getPropType-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ describe('getPropType', function() {
6565
]
6666
});
6767

68+
// line comments are ignored
69+
expect(getPropType(parse('oneOf(["foo", // baz\n"bar"])'))).toEqual({
70+
name: 'enum',
71+
value: [
72+
{value: '"foo"', computed: false},
73+
{value: '"bar"', computed: false}
74+
]
75+
});
76+
6877
expect(getPropType(parse('oneOfType([number, bool])'))).toEqual({
6978
name: 'union',
7079
value: [
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
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+
*/
10+
11+
"use strict";
12+
13+
jest.autoMockOff();
14+
15+
describe('printValue', function() {
16+
var printValue;
17+
var utils;
18+
19+
beforeEach(function() {
20+
printValue = require('../printValue');
21+
utils = require('../../../tests/utils');
22+
});
23+
24+
function pathFromSource(source) {
25+
return utils.parse(source).get('body', 0, 'expression');
26+
}
27+
28+
it('does not print leading comments', function() {
29+
expect(printValue(pathFromSource('//foo\nbar')))
30+
.toEqual('bar');
31+
});
32+
33+
it('does not print trailing comments', function() {
34+
expect(printValue(pathFromSource('bar//foo')))
35+
.toEqual('bar');
36+
});
37+
38+
});

src/utils/getPropType.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
var getMembers = require('./getMembers');
1717
var getPropertyName = require('./getPropertyName');
18+
var printValue = require('./printValue');
1819
var recast = require('recast');
1920
var resolveToValue = require('./resolveToValue');
2021

@@ -23,7 +24,7 @@ var types = recast.types.namedTypes;
2324
function getEnumValues(path) {
2425
return path.get('elements').map(function(elementPath) {
2526
return {
26-
value: recast.print(elementPath).code,
27+
value: printValue(elementPath),
2728
computed: !types.Literal.check(elementPath.node)
2829
};
2930
});
@@ -33,7 +34,7 @@ function getPropTypeOneOf(argumentPath) {
3334
var type = {name: 'enum'};
3435
if (!types.ArrayExpression.check(argumentPath.node)) {
3536
type.computed = true;
36-
type.value = recast.print(argumentPath).code;
37+
type.value = printValue(argumentPath);
3738
} else {
3839
type.value = getEnumValues(argumentPath);
3940
}
@@ -44,7 +45,7 @@ function getPropTypeOneOfType(argumentPath) {
4445
var type = {name: 'union'};
4546
if (!types.ArrayExpression.check(argumentPath.node)) {
4647
type.computed = true;
47-
type.value = recast.print(argumentPath).code;
48+
type.value = printValue(argumentPath);
4849
} else {
4950
type.value = argumentPath.get('elements').map(getPropType);
5051
}
@@ -56,7 +57,7 @@ function getPropTypeArrayOf(argumentPath) {
5657
var subType = getPropType(argumentPath);
5758

5859
if (subType.name === 'unknown') {
59-
type.value = recast.print(argumentPath).code;
60+
type.value = printValue(argumentPath);
6061
type.computed = true;
6162
} else {
6263
type.value = subType;
@@ -84,7 +85,7 @@ function getPropTypeShape(argumentPath) {
8485
function getPropTypeInstanceOf(argumentPath) {
8586
return {
8687
name: 'instanceOf',
87-
value: recast.print(argumentPath).code
88+
value: printValue(argumentPath)
8889
};
8990
}
9091

@@ -144,7 +145,7 @@ function getPropType(path: NodePath): PropTypeDescriptor {
144145
propTypes.hasOwnProperty(node.callee.name)) {
145146
descriptor = propTypes[node.callee.name](path.get('arguments', 0));
146147
} else {
147-
descriptor = {name: 'custom', raw: recast.print(path).code};
148+
descriptor = {name: 'custom', raw: printValue(path)};
148149
}
149150
}
150151
return descriptor;

src/utils/printValue.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
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+
*/
10+
11+
/**
12+
* @flow
13+
*/
14+
"use strict";
15+
16+
var recast = require('recast');
17+
18+
/**
19+
* Prints the given path without leading or trailing comments.
20+
*/
21+
function printValue(path: NodePath): string {
22+
if (path.node.comments) {
23+
path.node.comments.length = 0;
24+
}
25+
return recast.print(path).code;
26+
}
27+
28+
module.exports = printValue;

0 commit comments

Comments
 (0)