diff --git a/lib/rules/jsx-curly-spacing.js b/lib/rules/jsx-curly-spacing.js index 47c45ee694..1ecfe5a19c 100644 --- a/lib/rules/jsx-curly-spacing.js +++ b/lib/rules/jsx-curly-spacing.js @@ -127,8 +127,9 @@ module.exports = { message: `There should be no space after '${token.value}'`, fix: function(fixer) { var nextToken = sourceCode.getTokenAfter(token); - var leadingComments = sourceCode.getNodeByRangeIndex(nextToken.range[0]).leadingComments; - var rangeEndRef = leadingComments ? leadingComments[0] : nextToken; + var nextNode = sourceCode.getNodeByRangeIndex(nextToken.range[0]); + var leadingComments = sourceCode.getComments(nextNode).leading; + var rangeEndRef = leadingComments.length ? leadingComments[0] : nextToken; return fixer.removeRange([token.range[1], rangeEndRef.range[0]]); } }); @@ -147,8 +148,9 @@ module.exports = { message: `There should be no space before '${token.value}'`, fix: function(fixer) { var previousToken = sourceCode.getTokenBefore(token); - var trailingComments = sourceCode.getNodeByRangeIndex(previousToken.range[0]).trailingComments; - var rangeStartRef = trailingComments ? trailingComments[trailingComments.length - 1] : previousToken; + var previousNode = sourceCode.getNodeByRangeIndex(previousToken.range[0]); + var trailingComments = sourceCode.getComments(previousNode).trailing; + var rangeStartRef = trailingComments.length ? trailingComments[trailingComments.length - 1] : previousToken; return fixer.removeRange([rangeStartRef.range[1], token.range[0]]); } }); @@ -200,14 +202,19 @@ module.exports = { } var first = context.getFirstToken(node); var last = sourceCode.getLastToken(node); - var second = context.getTokenAfter(first); - var penultimate = sourceCode.getTokenBefore(last); + var second = context.getTokenAfter(first, {includeComments: true}); + var penultimate = sourceCode.getTokenBefore(last, {includeComments: true}); - var leadingComments = sourceCode.getNodeByRangeIndex(second.range[0]).leadingComments; - second = leadingComments ? leadingComments[0] : second; - - var trailingComments = sourceCode.getNodeByRangeIndex(penultimate.range[0]).trailingComments; - penultimate = trailingComments ? trailingComments[trailingComments.length - 1] : penultimate; + if (!second) { + second = context.getTokenAfter(first); + var leadingComments = sourceCode.getNodeByRangeIndex(second.range[0]).leadingComments; + second = leadingComments ? leadingComments[0] : second; + } + if (!penultimate) { + penultimate = sourceCode.getTokenBefore(last); + var trailingComments = sourceCode.getNodeByRangeIndex(penultimate.range[0]).trailingComments; + penultimate = trailingComments ? trailingComments[trailingComments.length - 1] : penultimate; + } var isObjectLiteral = first.value === second.value; if (isObjectLiteral) { diff --git a/lib/rules/jsx-uses-react.js b/lib/rules/jsx-uses-react.js index 7b22aed561..5038ed778b 100644 --- a/lib/rules/jsx-uses-react.js +++ b/lib/rules/jsx-uses-react.js @@ -32,10 +32,6 @@ module.exports = { JSXOpeningElement: function() { context.markVariableAsUsed(pragma); - }, - - BlockComment: function(node) { - pragma = pragmaUtil.getFromNode(node) || pragma; } }; diff --git a/lib/rules/no-deprecated.js b/lib/rules/no-deprecated.js index 947ab930d1..d87d115ebb 100644 --- a/lib/rules/no-deprecated.js +++ b/lib/rules/no-deprecated.js @@ -159,10 +159,6 @@ module.exports = { node.id.properties.forEach(function(property) { checkDeprecation(node, `${reactModuleName || pragma}.${property.key.name}`); }); - }, - - BlockComment: function(node) { - pragma = pragmaUtil.getFromNode(node) || pragma; } }; diff --git a/lib/rules/react-in-jsx-scope.js b/lib/rules/react-in-jsx-scope.js index 13fe554f19..98e2363db4 100644 --- a/lib/rules/react-in-jsx-scope.js +++ b/lib/rules/react-in-jsx-scope.js @@ -40,10 +40,6 @@ module.exports = { name: pragma } }); - }, - - BlockComment: function(node) { - pragma = pragmaUtil.getFromNode(node) || pragma; } }; diff --git a/lib/util/Components.js b/lib/util/Components.js index c7b7ac3ebc..d6d844be51 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -598,10 +598,6 @@ function componentRule(rule, context) { components.add(node, 0); }, - BlockComment: function(node) { - pragma = pragmaUtil.getFromNode(node) || pragma; - }, - ReturnStatement: function(node) { if (!utils.isReturningJSX(node)) { return; diff --git a/lib/util/pragma.js b/lib/util/pragma.js index dc1798078a..e982d6159f 100644 --- a/lib/util/pragma.js +++ b/lib/util/pragma.js @@ -23,26 +23,27 @@ function getCreateClassFromContext(context) { function getFromContext(context) { var pragma = 'React'; + + var sourceCode = context.getSourceCode(); + var pragmaNode = sourceCode.getAllComments().find(function(node) { + return JSX_ANNOTATION_REGEX.test(node.value); + }); + + if (pragmaNode) { + var matches = JSX_ANNOTATION_REGEX.exec(pragmaNode.value); + pragma = matches[1].split('.')[0]; // .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings) - if (context.settings.react && context.settings.react.pragma) { + } else if (context.settings.react && context.settings.react.pragma) { pragma = context.settings.react.pragma; } + if (!JS_IDENTIFIER_REGEX.test(pragma)) { throw new Error(`React pragma ${pragma} is not a valid identifier`); } return pragma; } -function getFromNode(node) { - var matches = JSX_ANNOTATION_REGEX.exec(node.value); - if (!matches) { - return false; - } - return matches[1].split('.')[0]; -} - module.exports = { getCreateClassFromContext: getCreateClassFromContext, - getFromContext: getFromContext, - getFromNode: getFromNode + getFromContext: getFromContext };