Skip to content

Commit

Permalink
Chore: Replace deprecated calls to context - batch 2 (refs #6029) (#6049
Browse files Browse the repository at this point in the history
)
  • Loading branch information
alberto authored and ilyavolodin committed May 3, 2016
1 parent a3a6e06 commit cd1b057
Show file tree
Hide file tree
Showing 20 changed files with 83 additions and 54 deletions.
2 changes: 1 addition & 1 deletion lib/rules/newline-after-var.js
Expand Up @@ -35,7 +35,7 @@ module.exports = {
var mode = context.options[0] === "never" ? "never" : "always";

// Cache starting and ending line numbers of comments for faster lookup
var commentEndLine = context.getAllComments().reduce(function(result, token) {
var commentEndLine = sourceCode.getAllComments().reduce(function(result, token) {
result[token.loc.start.line] = token.loc.end.line;
return result;
}, {});
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/newline-per-chained-call.js
Expand Up @@ -36,6 +36,8 @@ module.exports = {
var options = context.options[0] || {},
ignoreChainWithDepth = options.ignoreChainWithDepth || 2;

var sourceCode = context.getSourceCode();

return {
"CallExpression:exit": function(node) {
if (!node.callee || node.callee.type !== "MemberExpression") {
Expand All @@ -55,7 +57,7 @@ module.exports = {
context.report(
callee.property,
callee.property.loc.start,
"Expected line break after `" + context.getSource(callee.object).replace(/\r\n|\r|\n/g, "\\n") + "`."
"Expected line break after `" + sourceCode.getText(callee.object).replace(/\r\n|\r|\n/g, "\\n") + "`."
);
}
}
Expand Down
10 changes: 6 additions & 4 deletions lib/rules/no-cond-assign.js
Expand Up @@ -34,6 +34,8 @@ module.exports = {

var prohibitAssign = (context.options[0] || "except-parens");

var sourceCode = context.getSourceCode();

/**
* Check whether an AST node is the test expression for a conditional statement.
* @param {!Object} node The node to test.
Expand Down Expand Up @@ -68,8 +70,8 @@ module.exports = {
* @returns {boolean} `true` if the code is enclosed in parentheses; otherwise, `false`.
*/
function isParenthesised(node) {
var previousToken = context.getTokenBefore(node),
nextToken = context.getTokenAfter(node);
var previousToken = sourceCode.getTokenBefore(node),
nextToken = sourceCode.getTokenAfter(node);

return previousToken.value === "(" && previousToken.range[1] <= node.range[0] &&
nextToken.value === ")" && nextToken.range[0] >= node.range[1];
Expand All @@ -81,8 +83,8 @@ module.exports = {
* @returns {boolean} `true` if the code is enclosed in two sets of parentheses; otherwise, `false`.
*/
function isParenthesisedTwice(node) {
var previousToken = context.getTokenBefore(node, 1),
nextToken = context.getTokenAfter(node, 1);
var previousToken = sourceCode.getTokenBefore(node, 1),
nextToken = sourceCode.getTokenAfter(node, 1);

return isParenthesised(node) &&
previousToken.value === "(" && previousToken.range[1] <= node.range[0] &&
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-div-regex.js
Expand Up @@ -21,11 +21,12 @@ module.exports = {
},

create: function(context) {
var sourceCode = context.getSourceCode();

return {

Literal: function(node) {
var token = context.getFirstToken(node);
var token = sourceCode.getFirstToken(node);

if (token.type === "RegularExpression" && token.value[1] === "=") {
context.report(node, "A regular expression literal can be confused with '/='.");
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-duplicate-case.js
Expand Up @@ -22,13 +22,14 @@ module.exports = {
},

create: function(context) {
var sourceCode = context.getSourceCode();

return {
SwitchStatement: function(node) {
var mapping = {};

node.cases.forEach(function(switchCase) {
var key = context.getSource(switchCase.test);
var key = sourceCode.getText(switchCase.test);

if (mapping[key]) {
context.report(switchCase, "Duplicate case label.");
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-empty-character-class.js
Expand Up @@ -39,11 +39,12 @@ module.exports = {
},

create: function(context) {
var sourceCode = context.getSourceCode();

return {

Literal: function(node) {
var token = context.getFirstToken(node);
var token = sourceCode.getFirstToken(node);

if (token.type === "RegularExpression" && !regex.test(token.value)) {
context.report(node, "Empty class.");
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/no-empty-function.js
Expand Up @@ -121,6 +121,8 @@ module.exports = {
var options = context.options[0] || {};
var allowed = options.allow || [];

var sourceCode = context.getSourceCode();

/**
* Reports a given function node if the node matches the following patterns.
*
Expand All @@ -139,7 +141,7 @@ module.exports = {
if (allowed.indexOf(kind) === -1 &&
node.body.type === "BlockStatement" &&
node.body.body.length === 0 &&
context.getComments(node.body).trailing.length === 0
sourceCode.getComments(node.body).trailing.length === 0
) {
context.report({
node: node,
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/no-empty.js
Expand Up @@ -35,6 +35,8 @@ module.exports = {
var options = context.options[0] || {},
allowEmptyCatch = options.allowEmptyCatch || false;

var sourceCode = context.getSourceCode();

return {
BlockStatement: function(node) {

Expand All @@ -53,7 +55,7 @@ module.exports = {
}

// any other block is only allowed to be empty, if it contains a comment
if (context.getComments(node).trailing.length > 0) {
if (sourceCode.getComments(node).trailing.length > 0) {
return;
}

Expand Down
7 changes: 4 additions & 3 deletions lib/rules/no-extra-semi.js
Expand Up @@ -22,6 +22,7 @@ module.exports = {
},

create: function(context) {
var sourceCode = context.getSourceCode();

/**
* Reports an unnecessary semicolon error.
Expand All @@ -48,7 +49,7 @@ module.exports = {
function checkForPartOfClassBody(firstToken) {
for (var token = firstToken;
token.type === "Punctuator" && token.value !== "}";
token = context.getTokenAfter(token)
token = sourceCode.getTokenAfter(token)
) {
if (token.value === ";") {
report(token);
Expand Down Expand Up @@ -78,7 +79,7 @@ module.exports = {
* @returns {void}
*/
ClassBody: function(node) {
checkForPartOfClassBody(context.getFirstToken(node, 1)); // 0 is `{`.
checkForPartOfClassBody(sourceCode.getFirstToken(node, 1)); // 0 is `{`.
},

/**
Expand All @@ -87,7 +88,7 @@ module.exports = {
* @returns {void}
*/
MethodDefinition: function(node) {
checkForPartOfClassBody(context.getTokenAfter(node));
checkForPartOfClassBody(sourceCode.getTokenAfter(node));
}
};

Expand Down
14 changes: 8 additions & 6 deletions lib/rules/no-implicit-coercion.js
Expand Up @@ -179,6 +179,8 @@ module.exports = {
var options = parseOptions(context.options[0]),
operatorAllowed = false;

var sourceCode = context.getSourceCode();

return {
UnaryExpression: function(node) {

Expand All @@ -188,7 +190,7 @@ module.exports = {
context.report(
node,
"use `Boolean({{code}})` instead.", {
code: context.getSource(node.argument.argument)
code: sourceCode.getText(node.argument.argument)
});
}

Expand All @@ -198,7 +200,7 @@ module.exports = {
context.report(
node,
"use `{{code}} !== -1` instead.", {
code: context.getSource(node.argument)
code: sourceCode.getText(node.argument)
});
}

Expand All @@ -208,7 +210,7 @@ module.exports = {
context.report(
node,
"use `Number({{code}})` instead.", {
code: context.getSource(node.argument)
code: sourceCode.getText(node.argument)
});
}
},
Expand All @@ -224,7 +226,7 @@ module.exports = {
context.report(
node,
"use `Number({{code}})` instead.", {
code: context.getSource(nonNumericOperand)
code: sourceCode.getText(nonNumericOperand)
});
}

Expand All @@ -234,7 +236,7 @@ module.exports = {
context.report(
node,
"use `String({{code}})` instead.", {
code: context.getSource(getOtherOperand(node, ""))
code: sourceCode.getText(getOtherOperand(node, ""))
});
}
},
Expand All @@ -247,7 +249,7 @@ module.exports = {
context.report(
node,
"use `{{code}} = String({{code}})` instead.", {
code: context.getSource(getOtherOperand(node, ""))
code: sourceCode.getText(getOtherOperand(node, ""))
});
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/no-inline-comments.js
Expand Up @@ -22,6 +22,7 @@ module.exports = {
},

create: function(context) {
var sourceCode = context.getSourceCode();

/**
* Will check that comments are not on lines starting with or ending with code
Expand All @@ -32,8 +33,8 @@ module.exports = {
function testCodeAroundComment(node) {

// Get the whole line and cut it off at the start of the comment
var startLine = String(context.getSourceLines()[node.loc.start.line - 1]);
var endLine = String(context.getSourceLines()[node.loc.end.line - 1]);
var startLine = String(sourceCode.lines[node.loc.start.line - 1]);
var endLine = String(sourceCode.lines[node.loc.end.line - 1]);

var preamble = startLine.slice(0, node.loc.start.column).trim();

Expand Down
8 changes: 5 additions & 3 deletions lib/rules/no-irregular-whitespace.js
Expand Up @@ -58,6 +58,8 @@ module.exports = {
var skipRegExps = !!options.skipRegExps;
var skipTemplates = !!options.skipTemplates;

var sourceCode = context.getSourceCode();

/**
* Removes errors that occur inside a string node
* @param {ASTNode} node to check for matching errors.
Expand Down Expand Up @@ -132,7 +134,7 @@ module.exports = {
* @private
*/
function checkForIrregularWhitespace(node) {
var sourceLines = context.getSourceLines();
var sourceLines = sourceCode.lines;

sourceLines.forEach(function(sourceLine, lineIndex) {
var lineNumber = lineIndex + 1,
Expand All @@ -157,8 +159,8 @@ module.exports = {
* @private
*/
function checkForIrregularLineTerminators(node) {
var source = context.getSource(),
sourceLines = context.getSourceLines(),
var source = sourceCode.getText(),
sourceLines = sourceCode.lines,
linebreaks = source.match(/\r\n|\r|\n|\u2028|\u2029/g),
lastLineIndex = -1,
lineIndex,
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/no-mixed-spaces-and-tabs.js
Expand Up @@ -24,6 +24,7 @@ module.exports = {
},

create: function(context) {
var sourceCode = context.getSourceCode();

var smartTabs,
ignoredLocs = [];
Expand Down Expand Up @@ -86,8 +87,8 @@ module.exports = {
*/
var regex = /^(?=[\t ]*(\t | \t))/,
match,
lines = context.getSourceLines(),
comments = context.getAllComments();
lines = sourceCode.lines,
comments = sourceCode.getAllComments();

comments.forEach(function(comment) {
ignoredLocs.push(comment.loc);
Expand Down
11 changes: 6 additions & 5 deletions lib/rules/no-multi-spaces.js
Expand Up @@ -95,8 +95,9 @@ module.exports = {
return {
Program: function() {

var source = context.getSource(),
allComments = context.getAllComments(),
var sourceCode = context.getSourceCode(),
source = sourceCode.getText(),
allComments = sourceCode.getAllComments(),
pattern = /[^\n\r\u2028\u2029\t ].? {2,}/g, // note: repeating space
token,
previousToken,
Expand All @@ -121,12 +122,12 @@ module.exports = {
// do not flag anything inside of comments
if (!isIndexInComment(pattern.lastIndex, allComments)) {

token = context.getTokenByRangeStart(pattern.lastIndex);
token = sourceCode.getTokenByRangeStart(pattern.lastIndex);
if (token) {
previousToken = context.getTokenBefore(token);
previousToken = sourceCode.getTokenBefore(token);

if (hasExceptions) {
parent = context.getNodeByRangeIndex(pattern.lastIndex - 1);
parent = sourceCode.getNodeByRangeIndex(pattern.lastIndex - 1);
}

if (!parent || !exceptions[parent.type]) {
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/no-multiple-empty-lines.js
Expand Up @@ -56,6 +56,8 @@ module.exports = {
maxBOF = context.options[0].maxBOF;
}

var sourceCode = context.getSourceCode();

//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
Expand All @@ -73,7 +75,7 @@ module.exports = {
},

"Program:exit": function checkBlankLines(node) {
var lines = context.getSourceLines(),
var lines = sourceCode.lines,
currentLocation = -1,
lastLocation,
blankCounter = 0,
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-regex-spaces.js
Expand Up @@ -21,11 +21,12 @@ module.exports = {
},

create: function(context) {
var sourceCode = context.getSourceCode();

return {

Literal: function(node) {
var token = context.getFirstToken(node),
var token = sourceCode.getFirstToken(node),
nodeType = token.type,
nodeValue = token.value,
multipleSpacesRegex = /( {2,})+?/,
Expand Down
12 changes: 7 additions & 5 deletions lib/rules/no-return-assign.js
Expand Up @@ -20,12 +20,12 @@ function isAssignment(node) {
/**
* Checks whether or not a node is enclosed in parentheses.
* @param {Node|null} node - A node to check.
* @param {RuleContext} context - The current context.
* @param {sourceCode} sourceCode - The ESLint SourceCode object.
* @returns {boolean} Whether or not the node is enclosed in parentheses.
*/
function isEnclosedInParens(node, context) {
var prevToken = context.getTokenBefore(node);
var nextToken = context.getTokenAfter(node);
function isEnclosedInParens(node, sourceCode) {
var prevToken = sourceCode.getTokenBefore(node);
var nextToken = sourceCode.getTokenAfter(node);

return prevToken.value === "(" && nextToken.value === ")";
}
Expand All @@ -52,6 +52,8 @@ module.exports = {
create: function(context) {
var always = (context.options[0] || "except-parens") !== "except-parens";

var sourceCode = context.getSourceCode();

/**
* Check whether return statement contains assignment
* @param {ASTNode} nodeToCheck node to check
Expand All @@ -61,7 +63,7 @@ module.exports = {
* @private
*/
function checkForAssignInReturn(nodeToCheck, nodeToReport, message) {
if (isAssignment(nodeToCheck) && (always || !isEnclosedInParens(nodeToCheck, context))) {
if (isAssignment(nodeToCheck) && (always || !isEnclosedInParens(nodeToCheck, sourceCode))) {
context.report(nodeToReport, message);
}
}
Expand Down

0 comments on commit cd1b057

Please sign in to comment.