Skip to content

Commit 7982627

Browse files
zurawikiyannickcr
authored andcommitted
Add Pragma for createClass factory method
1 parent 7d3dbfd commit 7982627

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ You can also specify some settings that will be shared across all the plugin rul
3737
{
3838
"settings": {
3939
"react": {
40+
"createClass": "createClass", // Regex for Component Factory to use, default to "createClass"
4041
"pragma": "React", // Pragma to use, default to "React"
4142
"version": "15.0" // React version, default to the latest React stable release
4243
}

lib/util/Components.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ Components.prototype.length = function() {
136136

137137
function componentRule(rule, context) {
138138

139+
var createClass = pragmaUtil.getCreateClassFromContext(context);
139140
var pragma = pragmaUtil.getFromContext(context);
140141
var sourceCode = context.getSourceCode();
141142
var components = new Components();
@@ -153,7 +154,7 @@ function componentRule(rule, context) {
153154
if (!node.parent) {
154155
return false;
155156
}
156-
return new RegExp('^(' + pragma + '\\.)?createClass$').test(sourceCode.getText(node.parent.callee));
157+
return new RegExp('^(' + pragma + '\\.)?' + createClass + '$').test(sourceCode.getText(node.parent.callee));
157158
},
158159

159160
/**

lib/util/pragma.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,32 @@
55
'use strict';
66

77
var JSX_ANNOTATION_REGEX = /^\*\s*@jsx\s+([^\s]+)/;
8+
// Does not check for reserved keywords or unicode characters
9+
var JS_IDENTIFIER_REGEX = /^[_$a-zA-Z][_$a-zA-Z0-9]*$/;
10+
11+
12+
function getCreateClassFromContext(context) {
13+
var pragma = 'createClass';
14+
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
15+
if (context.settings.react && context.settings.react.createClass) {
16+
pragma = context.settings.react.createClass;
17+
}
18+
if (!JS_IDENTIFIER_REGEX.test(pragma)) {
19+
throw new Error('createClass pragma ' + pragma + 'is not a valid function name');
20+
}
21+
return pragma;
22+
}
823

924
function getFromContext(context) {
1025
var pragma = 'React';
1126
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
1227
if (context.settings.react && context.settings.react.pragma) {
1328
pragma = context.settings.react.pragma;
1429
}
15-
return pragma.split('.')[0];
30+
if (!JS_IDENTIFIER_REGEX.test(pragma)) {
31+
throw new Error('React pragma ' + pragma + 'is not a valid identifier');
32+
}
33+
return pragma;
1634
}
1735

1836
function getFromNode(node) {
@@ -24,6 +42,7 @@ function getFromNode(node) {
2442
}
2543

2644
module.exports = {
45+
getCreateClassFromContext: getCreateClassFromContext,
2746
getFromContext: getFromContext,
2847
getFromNode: getFromNode
2948
};

0 commit comments

Comments
 (0)