Skip to content

Commit

Permalink
Add Pragma for createClass factory method
Browse files Browse the repository at this point in the history
  • Loading branch information
zurawiki authored and yannickcr committed Aug 14, 2016
1 parent 7d3dbfd commit 7982627
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ You can also specify some settings that will be shared across all the plugin rul
{
"settings": {
"react": {
"createClass": "createClass", // Regex for Component Factory to use, default to "createClass"
"pragma": "React", // Pragma to use, default to "React"
"version": "15.0" // React version, default to the latest React stable release
}
Expand Down
3 changes: 2 additions & 1 deletion lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ Components.prototype.length = function() {

function componentRule(rule, context) {

var createClass = pragmaUtil.getCreateClassFromContext(context);
var pragma = pragmaUtil.getFromContext(context);
var sourceCode = context.getSourceCode();
var components = new Components();
Expand All @@ -153,7 +154,7 @@ function componentRule(rule, context) {
if (!node.parent) {
return false;
}
return new RegExp('^(' + pragma + '\\.)?createClass$').test(sourceCode.getText(node.parent.callee));
return new RegExp('^(' + pragma + '\\.)?' + createClass + '$').test(sourceCode.getText(node.parent.callee));
},

/**
Expand Down
21 changes: 20 additions & 1 deletion lib/util/pragma.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,32 @@
'use strict';

var JSX_ANNOTATION_REGEX = /^\*\s*@jsx\s+([^\s]+)/;
// Does not check for reserved keywords or unicode characters
var JS_IDENTIFIER_REGEX = /^[_$a-zA-Z][_$a-zA-Z0-9]*$/;


function getCreateClassFromContext(context) {
var pragma = 'createClass';
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
if (context.settings.react && context.settings.react.createClass) {
pragma = context.settings.react.createClass;
}
if (!JS_IDENTIFIER_REGEX.test(pragma)) {
throw new Error('createClass pragma ' + pragma + 'is not a valid function name');
}
return pragma;
}

function getFromContext(context) {
var pragma = 'React';
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
if (context.settings.react && context.settings.react.pragma) {
pragma = context.settings.react.pragma;
}
return pragma.split('.')[0];
if (!JS_IDENTIFIER_REGEX.test(pragma)) {
throw new Error('React pragma ' + pragma + 'is not a valid identifier');
}
return pragma;
}

function getFromNode(node) {
Expand All @@ -24,6 +42,7 @@ function getFromNode(node) {
}

module.exports = {
getCreateClassFromContext: getCreateClassFromContext,
getFromContext: getFromContext,
getFromNode: getFromNode
};

0 comments on commit 7982627

Please sign in to comment.