-
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect "isRequired" in shapes (closes #21)
- Loading branch information
Showing
6 changed files
with
100 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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('isRequiredPropType', () => { | ||
var expression; | ||
var isRequiredPropType; | ||
|
||
beforeEach(() => { | ||
isRequiredPropType = require('../isRequiredPropType'); | ||
({expression} = require('../../../tests/utils')); | ||
}); | ||
|
||
|
||
it('considers isRequired', () => { | ||
expect(isRequiredPropType(expression('foo.bar.isRequired'))).toEqual(true); | ||
expect(isRequiredPropType(expression('foo.isRequired.bar'))).toEqual(true); | ||
}); | ||
|
||
it('considers ["isRequired"]', () => { | ||
expect(isRequiredPropType(expression('foo.bar["isRequired"]'))) | ||
.toEqual(true); | ||
expect(isRequiredPropType(expression('foo["isRequired"].bar'))) | ||
.toEqual(true); | ||
}); | ||
|
||
it('ignores variables', () => { | ||
expect(isRequiredPropType(expression('foo.bar[isRequired]'))) | ||
.toEqual(false); | ||
}); | ||
|
||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import getMembers from '../utils/getMembers'; | ||
|
||
/** | ||
* Returns true of the prop is required, according to its type defintion | ||
*/ | ||
export default function isRequiredPropType(path) { | ||
return getMembers(path).some( | ||
member => !member.computed && member.path.node.name === 'isRequired' || | ||
member.computed && member.path.node.value === 'isRequired' | ||
); | ||
} |