-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Yacine Hmito <yacine.hmito@gmail.com> Co-authored-by: Jordan Harband <ljharb@gmail.com>
- Loading branch information
1 parent
f25a8ec
commit 53a0d84
Showing
6 changed files
with
214 additions
and
0 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,29 @@ | ||
# Enforce that namespaces are not used in React elements (react/no-namespace) | ||
|
||
Enforces the absence of a namespace in React elements, such as with `svg:circle`, as they are not supported in React. | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```jsx | ||
<ns:TestComponent /> | ||
``` | ||
|
||
```jsx | ||
<Ns:TestComponent /> | ||
``` | ||
|
||
The following patterns are **not** considered warnings: | ||
|
||
```jsx | ||
<TestComponent /> | ||
``` | ||
|
||
```jsx | ||
<testComponent /> | ||
``` | ||
|
||
## When not to use | ||
|
||
If you are not using React. |
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,49 @@ | ||
/** | ||
* @fileoverview Enforce that namespaces are not used in React elements | ||
* @author Yacine Hmito | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const elementType = require('jsx-ast-utils/elementType'); | ||
const docsUrl = require('../util/docsUrl'); | ||
const isCreateElement = require('../util/isCreateElement'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Enforce that namespaces are not used in React elements', | ||
category: 'Possible Errors', | ||
recommended: false, | ||
url: docsUrl('no-namespace') | ||
}, | ||
|
||
schema: [{ | ||
type: 'object', | ||
additionalProperties: false | ||
}] | ||
}, | ||
|
||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
if (isCreateElement(node, context) && node.arguments.length > 0 && node.arguments[0].type === 'Literal') { | ||
const name = node.arguments[0].value; | ||
if (name.indexOf(':') === -1) return undefined; | ||
const message = `React component ${name} must not be in a namespace as React does not support them`; | ||
context.report({node, message}); | ||
} | ||
}, | ||
JSXOpeningElement(node) { | ||
const name = elementType(node); | ||
if (name.indexOf(':') === -1) return undefined; | ||
const message = `React component ${name} must not be in a namespace as React does not support them`; | ||
context.report({node, message}); | ||
} | ||
}; | ||
} | ||
}; |
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,128 @@ | ||
/** | ||
* @fileoverview Tests for jsx-no-namespace | ||
* @author Yacine Hmito | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../../../lib/rules/no-namespace'); | ||
|
||
const parserOptions = { | ||
ecmaVersion: 2018, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({parserOptions}); | ||
ruleTester.run('jsx-no-namespace', rule, { | ||
valid: [{ | ||
code: '<testcomponent />' | ||
}, { | ||
code: 'React.createElement("testcomponent")' | ||
}, { | ||
code: '<testComponent />' | ||
}, { | ||
code: 'React.createElement("testComponent")' | ||
}, { | ||
code: '<test_component />' | ||
}, { | ||
code: 'React.createElement("test_component")' | ||
}, { | ||
code: '<TestComponent />' | ||
}, { | ||
code: 'React.createElement("TestComponent")' | ||
}, { | ||
code: '<object.testcomponent />' | ||
}, { | ||
code: 'React.createElement("object.testcomponent")' | ||
}, { | ||
code: '<object.testComponent />' | ||
}, { | ||
code: 'React.createElement("object.testComponent")' | ||
}, { | ||
code: '<object.test_component />' | ||
}, { | ||
code: 'React.createElement("object.test_component")' | ||
}, { | ||
code: '<object.TestComponent />' | ||
}, { | ||
code: 'React.createElement("object.TestComponent")' | ||
}, { | ||
code: '<Object.testcomponent />' | ||
}, { | ||
code: 'React.createElement("Object.testcomponent")' | ||
}, { | ||
code: '<Object.testComponent />' | ||
}, { | ||
code: 'React.createElement("Object.testComponent")' | ||
}, { | ||
code: '<Object.test_component />' | ||
}, { | ||
code: 'React.createElement("Object.test_component")' | ||
}, { | ||
code: '<Object.TestComponent />' | ||
}, { | ||
code: 'React.createElement("Object.TestComponent")' | ||
}], | ||
|
||
invalid: [{ | ||
code: '<ns:testcomponent />', | ||
errors: [{message: 'React component ns:testcomponent must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: 'React.createElement("ns:testcomponent")', | ||
errors: [{message: 'React component ns:testcomponent must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: '<ns:testComponent />', | ||
errors: [{message: 'React component ns:testComponent must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: 'React.createElement("ns:testComponent")', | ||
errors: [{message: 'React component ns:testComponent must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: '<ns:test_component />', | ||
errors: [{message: 'React component ns:test_component must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: 'React.createElement("ns:test_component")', | ||
errors: [{message: 'React component ns:test_component must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: '<ns:TestComponent />', | ||
errors: [{message: 'React component ns:TestComponent must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: 'React.createElement("ns:TestComponent")', | ||
errors: [{message: 'React component ns:TestComponent must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: '<Ns:testcomponent />', | ||
errors: [{message: 'React component Ns:testcomponent must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: 'React.createElement("Ns:testcomponent")', | ||
errors: [{message: 'React component Ns:testcomponent must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: '<Ns:testComponent />', | ||
errors: [{message: 'React component Ns:testComponent must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: 'React.createElement("Ns:testComponent")', | ||
errors: [{message: 'React component Ns:testComponent must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: '<Ns:test_component />', | ||
errors: [{message: 'React component Ns:test_component must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: 'React.createElement("Ns:test_component")', | ||
errors: [{message: 'React component Ns:test_component must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: '<Ns:TestComponent />', | ||
errors: [{message: 'React component Ns:TestComponent must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: 'React.createElement("Ns:TestComponent")', | ||
errors: [{message: 'React component Ns:TestComponent must not be in a namespace as React does not support them'}] | ||
}] | ||
}); |