Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Pragma for createClass factory method #725

Merged
merged 1 commit into from
Aug 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
};