Skip to content

Commit

Permalink
Update rules to match changes in comments attachement in ESLint 4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed May 8, 2017
1 parent 1b5743f commit 159d0e5
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 38 deletions.
29 changes: 18 additions & 11 deletions lib/rules/jsx-curly-spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]]);
}
});
Expand All @@ -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]]);
}
});
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 0 additions & 4 deletions lib/rules/jsx-uses-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ module.exports = {

JSXOpeningElement: function() {
context.markVariableAsUsed(pragma);
},

BlockComment: function(node) {
pragma = pragmaUtil.getFromNode(node) || pragma;
}

};
Expand Down
4 changes: 0 additions & 4 deletions lib/rules/no-deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

};
Expand Down
4 changes: 0 additions & 4 deletions lib/rules/react-in-jsx-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ module.exports = {
name: pragma
}
});
},

BlockComment: function(node) {
pragma = pragmaUtil.getFromNode(node) || pragma;
}

};
Expand Down
4 changes: 0 additions & 4 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 12 additions & 11 deletions lib/util/pragma.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

0 comments on commit 159d0e5

Please sign in to comment.