forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
218e9fc
commit 9d07f1d
Showing
5 changed files
with
159 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,37 @@ | ||
# Enforce PasalCase for user-defined JSX components (jsx-pascal-case) | ||
|
||
Enforces coding style that user-defined JSX components are defined and referenced in PascalCase. | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
<testComponent /> | ||
``` | ||
|
||
```js | ||
<testComponent> | ||
<div /> | ||
</testComponent> | ||
``` | ||
|
||
```js | ||
<test_component /> | ||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
<TestComponent /> | ||
``` | ||
|
||
```js | ||
<TestComponent> | ||
<div /> | ||
</TestComponent> | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
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,49 @@ | ||
/** | ||
* @fileoverview Enforce PasalCase for user-defined JSX components | ||
* @author Jake Marsh | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var variableUtil = require('../util/variable'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Constants | ||
// ------------------------------------------------------------------------------ | ||
|
||
var PASCAL_CASE_REGEX = /^[A-Z][a-z]+(?:[A-Z][a-z]+)*$/; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
return { | ||
JSXOpeningElement: function(node) { | ||
var variables = variableUtil.variablesInScope(context); | ||
|
||
switch (node.name.type) { | ||
case 'JSXIdentifier': | ||
node = node.name; | ||
break; | ||
case 'JSXMemberExpression': | ||
node = node.name.object; | ||
break; | ||
case 'JSXNamespacedName': | ||
node = node.name.namespace; | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
var isImportedVariable = variableUtil.findVariable(variables, node.name); | ||
var isPascalCase = PASCAL_CASE_REGEX.test(node.name); | ||
|
||
if (isImportedVariable && !isPascalCase) { | ||
context.report(node, 'Imported JSX component ' + node.name + ' must be in PascalCase'); | ||
} | ||
} | ||
}; | ||
|
||
}; |
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,70 @@ | ||
/** | ||
* @fileoverview Tests for jsx-pascal-case | ||
* @author Jake Marsh | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/jsx-pascal-case'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
ruleTester.run('jsx-pascal-case', rule, { | ||
valid: [{ | ||
code: [ | ||
'var TestComponent;', | ||
'<TestComponent />' | ||
].join('\n'), | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}, { | ||
code: [ | ||
'var TestComponent;', | ||
'<TestComponent>', | ||
' <div />', | ||
'</TestComponent>' | ||
].join('\n'), | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}], | ||
|
||
invalid: [{ | ||
code: [ | ||
'var testComponent;', | ||
'<testComponent />' | ||
].join('\n'), | ||
ecmaFeatures: { | ||
jsx: true | ||
}, | ||
errors: [{message: 'Imported JSX component testComponent must be in PascalCase'}] | ||
}, { | ||
code: [ | ||
'var test_component;', | ||
'<test_component />' | ||
].join('\n'), | ||
ecmaFeatures: { | ||
jsx: true | ||
}, | ||
errors: [{message: 'Imported JSX component test_component must be in PascalCase'}] | ||
}, { | ||
code: [ | ||
'var testComponent;', | ||
'<testComponent>', | ||
' <div />', | ||
'</testComponent>' | ||
].join('\n'), | ||
ecmaFeatures: { | ||
jsx: true | ||
}, | ||
errors: [{message: 'Imported JSX component testComponent must be in PascalCase'}] | ||
}] | ||
}); |