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

WIP: Update: align argument for indent VariableDeclarator(fixes #8976) #9627

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion lib/rules/max-len.js
Expand Up @@ -264,7 +264,7 @@ module.exports = {
// list of comments to ignore
comments = ignoreComments || maxCommentLength || ignoreTrailingComments ? sourceCode.getAllComments() : [];

// we iterate over comments in parallel with the lines
// we iterate over comments in parallel with the lines
let commentsIndex = 0;

const strings = getAllStrings();
Expand Down