Skip to content

Commit

Permalink
Update: align argument for indent VariableDeclarator(fixes eslint#8976)
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Nov 14, 2017
1 parent 332c214 commit 5f2935f
Showing 1 changed file with 20 additions and 26 deletions.
46 changes: 20 additions & 26 deletions lib/rules/indent.js
Expand Up @@ -518,25 +518,13 @@ module.exports = {
},
VariableDeclarator: {
oneOf: [
{
type: "integer",
minimum: 0
},
ELEMENT_LIST_SCHEMA,
{
type: "object",
properties: {
var: {
type: "integer",
minimum: 0
},
let: {
type: "integer",
minimum: 0
},
const: {
type: "integer",
minimum: 0
}
var: ELEMENT_LIST_SCHEMA,
let: ELEMENT_LIST_SCHEMA,
const: ELEMENT_LIST_SCHEMA
},
additionalProperties: false
}
Expand Down Expand Up @@ -653,7 +641,7 @@ module.exports = {
if (context.options[1]) {
lodash.merge(options, context.options[1]);

if (typeof options.VariableDeclarator === "number") {
if (typeof options.VariableDeclarator !== "undefined" && typeof options.VariableDeclarator !== "object") {
options.VariableDeclarator = {
var: options.VariableDeclarator,
let: options.VariableDeclarator,
Expand Down Expand Up @@ -779,9 +767,10 @@ module.exports = {
* @param {Token} startToken The start token of the list that element should be aligned against, e.g. '['
* @param {Token} endToken The end token of the list, e.g. ']'
* @param {number|string} offset The amount that the elements should be offset
* @param {boolean} force `true` if collapsing should be disabled
* @returns {void}
*/
function addElementListIndent(elements, startToken, endToken, offset) {
function addElementListIndent(elements, startToken, endToken, offset, force) {

/**
* Gets the first token of a given element, including surrounding parentheses.
Expand All @@ -801,7 +790,8 @@ module.exports = {
offsets.setDesiredOffsets(
[startToken.range[1], endToken.range[0]],
startToken,
typeof offset === "number" ? offset : 1
typeof offset === "number" ? offset : 1,
force
);
offsets.setDesiredOffset(endToken, startToken, 0);

Expand Down Expand Up @@ -1302,6 +1292,13 @@ module.exports = {
},

VariableDeclaration(node) {
const variableKeyword = sourceCode.getFirstToken(node);

/*
* TODO: this is a bit of a hack because addElementsListIndent expects there to be a "last token"
* (like a closing array bracket) which is always aligned with the first token.
*/
const semicolon = sourceCode.getLastToken(node);
const variableIndent = options.VariableDeclarator.hasOwnProperty(node.kind) ? options.VariableDeclarator[node.kind] : DEFAULT_VARIABLE_INDENT;

if (node.declarations[node.declarations.length - 1].loc.start.line > node.loc.start.line) {
Expand All @@ -1325,16 +1322,13 @@ module.exports = {
* on the same line as the start of the declaration, provided that there are declarators that
* follow this one.
*/
const firstToken = sourceCode.getFirstToken(node);

offsets.setDesiredOffsets(node.range, firstToken, variableIndent, true);
addElementListIndent(node.declarations, variableKeyword, semicolon, variableIndent, true);
} else {
offsets.setDesiredOffsets(node.range, sourceCode.getFirstToken(node), variableIndent);
addElementListIndent(node.declarations, variableKeyword, semicolon, variableIndent);
}
const lastToken = sourceCode.getLastToken(node);

if (astUtils.isSemicolonToken(lastToken)) {
offsets.ignoreToken(lastToken);
if (astUtils.isSemicolonToken(semicolon)) {
offsets.ignoreToken(semicolon);
}
},

Expand Down

0 comments on commit 5f2935f

Please sign in to comment.