This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 656
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #387 from macovedj/no-children-prop
WIP: No children prop
- Loading branch information
Showing
7 changed files
with
184 additions
and
2 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
95 changes: 95 additions & 0 deletions
95
packages/@romejs/js-compiler/lint/rules/react/noChildrenProp.test.md
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,95 @@ | ||
# `noChildrenProp.test.ts` | ||
|
||
**DO NOT MODIFY**. This file has been autogenerated. Run `rome test packages/@romejs/js-compiler/lint/rules/react/noChildrenProp.test.ts --update-snapshots` to update. | ||
|
||
## `no children props` | ||
|
||
### `0` | ||
|
||
``` | ||
unknown:1 lint/noChildrenProp ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ children should not be passed as a prop | ||
<MyComponent children={'foo'}></MyComponent> | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Found 1 problem | ||
``` | ||
|
||
### `0: formatted` | ||
|
||
``` | ||
<MyComponent children={'foo'}></MyComponent>; | ||
``` | ||
|
||
### `1` | ||
|
||
``` | ||
unknown:1 lint/noChildrenProp ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ children should not be passed as a prop | ||
React.createElement('div', {children: 'foo'}) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Found 1 problem | ||
``` | ||
|
||
### `1: formatted` | ||
|
||
``` | ||
React.createElement('div', {children: 'foo'}); | ||
``` | ||
|
||
### `2` | ||
|
||
``` | ||
✔ No known problems! | ||
``` | ||
|
||
### `2: formatted` | ||
|
||
``` | ||
<MyComponent><AnotherComponent /></MyComponent>; | ||
``` | ||
|
||
### `3` | ||
|
||
``` | ||
✔ No known problems! | ||
``` | ||
|
||
### `3: formatted` | ||
|
||
``` | ||
React.createElement('div', {}, 'children'); | ||
``` | ||
|
||
### `4` | ||
|
||
``` | ||
✔ No known problems! | ||
``` | ||
|
||
### `4: formatted` | ||
|
||
``` | ||
React.createElement('div', child1, 'child2'); | ||
``` |
28 changes: 28 additions & 0 deletions
28
packages/@romejs/js-compiler/lint/rules/react/noChildrenProp.test.ts
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,28 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {test} from 'rome'; | ||
import {testLintMultiple} from '../testHelpers'; | ||
|
||
test( | ||
'no children props', | ||
async (t) => { | ||
await testLintMultiple( | ||
t, | ||
[ | ||
// INVALID | ||
`<MyComponent children={'foo'}></MyComponent>`, | ||
`React.createElement('div', {children: 'foo'})`, | ||
// VALID | ||
`<MyComponent><AnotherComponent /></MyComponent >`, | ||
`React.createElement('div', {}, 'children')`, | ||
`React.createElement('div', child1, 'child2')`, | ||
], | ||
{category: 'lint/noChildrenProp'}, | ||
); | ||
}, | ||
); |
47 changes: 47 additions & 0 deletions
47
packages/@romejs/js-compiler/lint/rules/react/noChildrenProp.ts
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,47 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {Path} from '@romejs/js-compiler'; | ||
import {AnyNode, JSXAttribute, JSXSpreadAttribute, ObjectProperty, ObjectMethod, SpreadProperty} from '@romejs/js-ast'; | ||
import {doesNodeMatchPattern} from '@romejs/js-ast-utils'; | ||
import {descriptions} from '@romejs/diagnostics'; | ||
|
||
const isAttributePassingChildrenProp = (attribute: JSXAttribute|JSXSpreadAttribute): boolean => ( | ||
attribute.type === 'JSXAttribute' && | ||
attribute.name.name === 'children' | ||
) | ||
|
||
const isCreateElementPassingChildrenProp = (property: ObjectProperty|ObjectMethod|SpreadProperty): boolean => ( | ||
property.type === 'ObjectProperty' && | ||
property.key.value.type === 'Identifier' && | ||
property.key.value.name === 'children' | ||
) | ||
|
||
export default { | ||
name: 'noChildrenProp', | ||
enter(path: Path): AnyNode { | ||
const {node} = path; | ||
if ( | ||
( | ||
node.type === 'JSXElement' && | ||
node.attributes.find(isAttributePassingChildrenProp) | ||
) || ( | ||
node.type === 'CallExpression' && | ||
doesNodeMatchPattern(node.callee, 'React.createElement') && | ||
node.arguments[1].type === 'ObjectExpression' && | ||
node.arguments[1].properties.find(isCreateElementPassingChildrenProp) | ||
) | ||
) { | ||
path.context.addNodeDiagnostic( | ||
node, | ||
descriptions.LINT.NO_CHILDREN_PROP, | ||
); | ||
} | ||
|
||
return node; | ||
}, | ||
}; |
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