-
-
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.
Initial implementation of jsx-no-namespace
- Loading branch information
1 parent
da7a045
commit fbd325a
Showing
5 changed files
with
152 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Enforce that namespaces are not used in JSX (react/jsx-no-namespace) | ||
|
||
Enforces the absence of a namespace in JSX components, 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 JSX. |
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,40 @@ | ||
/** | ||
* @fileoverview Enforce that namespaces are not used in JSX | ||
* @author Yacine Hmito | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const elementType = require('jsx-ast-utils/elementType'); | ||
const docsUrl = require('../util/docsUrl'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Enforce that namespaces are not used in JSX', | ||
category: 'Possible Errors', | ||
recommended: true, | ||
url: docsUrl('jsx-no-namespace') | ||
}, | ||
|
||
schema: [{ | ||
type: 'object', | ||
additionalProperties: false | ||
}] | ||
}, | ||
|
||
create(context) { | ||
return { | ||
JSXOpeningElement(node) { | ||
const name = elementType(node); | ||
if (name.indexOf(':') === -1) return undefined; | ||
const message = `JSX 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,80 @@ | ||
/** | ||
* @fileoverview Tests for jsx-no-namespace | ||
* @author Yacine Hmito | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../../../lib/rules/jsx-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: '<testComponent />' | ||
}, { | ||
code: '<test_component />' | ||
}, { | ||
code: '<TestComponent />' | ||
}, { | ||
code: '<object.testcomponent />' | ||
}, { | ||
code: '<object.testComponent />' | ||
}, { | ||
code: '<object.test_component />' | ||
}, { | ||
code: '<object.TestComponent />' | ||
}, { | ||
code: '<Object.testcomponent />' | ||
}, { | ||
code: '<Object.testComponent />' | ||
}, { | ||
code: '<Object.test_component />' | ||
}, { | ||
code: '<Object.TestComponent />' | ||
}], | ||
|
||
invalid: [{ | ||
code: '<ns:testcomponent />', | ||
errors: [{message: 'JSX component ns:testcomponent must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: '<ns:testComponent />', | ||
errors: [{message: 'JSX component ns:testComponent must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: '<ns:test_component />', | ||
errors: [{message: 'JSX component ns:test_component must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: '<ns:TestComponent />', | ||
errors: [{message: 'JSX component ns:TestComponent must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: '<Ns:testcomponent />', | ||
errors: [{message: 'JSX component Ns:testcomponent must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: '<Ns:testComponent />', | ||
errors: [{message: 'JSX component Ns:testComponent must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: '<Ns:test_component />', | ||
errors: [{message: 'JSX component Ns:test_component must not be in a namespace as React does not support them'}] | ||
}, { | ||
code: '<Ns:TestComponent />', | ||
errors: [{message: 'JSX component Ns:TestComponent must not be in a namespace as React does not support them'}] | ||
}] | ||
}); |