Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 7363537

Browse files
committed
[Perf] Remove getLoc() in TokenAssert.indentation and requireAlignedMultilineParams
1 parent d480c30 commit 7363537

File tree

3 files changed

+36
-124
lines changed

3 files changed

+36
-124
lines changed

lib/rules/require-aligned-multiline-params.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@
5858

5959
var assert = require('assert');
6060

61+
function getNodeColumn(node) {
62+
var currentToken = node.getFirstToken().getPreviousToken();
63+
var indentationLevel = 0;
64+
while (currentToken) {
65+
if (currentToken.getNewlineCount() > 0) {
66+
var sourceCodeLines = currentToken.getSourceCodeLines();
67+
indentationLevel += sourceCodeLines[sourceCodeLines.length - 1].length;
68+
break;
69+
}
70+
71+
indentationLevel += currentToken.getSourceCodeLength();
72+
73+
currentToken = currentToken.getPreviousToken();
74+
}
75+
return indentationLevel;
76+
}
77+
6178
module.exports = function() {
6279
};
6380

@@ -97,12 +114,12 @@ module.exports.prototype = {
97114
return;
98115
}
99116

100-
var currentLine = params[0].getLoc().start.line;
117+
var firstParam = params[0];
101118
var referenceColumn;
102119
var body;
103120

104121
if (_this._alignWithFirstParam) {
105-
referenceColumn = params[0].getLoc().start.column;
122+
referenceColumn = getNodeColumn(firstParam);
106123
} else {
107124

108125
body = node.body.body[0];
@@ -115,19 +132,21 @@ module.exports.prototype = {
115132
referenceColumn = body.getLoc().start.column + _this._indentationLevel;
116133
}
117134

118-
params.forEach(function(param) {
119-
if (param.getLoc().start.line !== currentLine) {
120-
if (param.getLoc().start.column !== referenceColumn) {
135+
var previousParam = firstParam;
136+
params.slice(1).forEach(function(param) {
137+
if (!file.isOnTheSameLine(previousParam, param)) {
138+
var paramColumn = getNodeColumn(param);
139+
if (paramColumn !== referenceColumn) {
121140
errors.assert.indentation({
122-
lineNumber: param.getLoc().start.line,
123-
actual: param.getLoc().start.column,
141+
token: param.getFirstToken(),
142+
actual: paramColumn,
124143
expected: referenceColumn,
125144
indentChar: ' ',
126145
silent: false
127146
});
128147
}
129148

130-
currentLine = param.getLoc().start.line;
149+
previousParam = param;
131150
}
132151
});
133152

lib/token-assert.js

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,14 @@ TokenAssert.prototype.spacesBetween = function(options) {
121121
* Requires the specified line to have the expected indentation.
122122
*
123123
* @param {Object} options
124-
* @param {Number} options.lineNumber
125124
* @param {Number} options.actual
126125
* @param {Number} options.expected
127126
* @param {String} options.indentChar
127+
* @param {String} options.token
128128
* @param {Boolean} [options.silent] if true, will suppress error emission but still fix whitespace
129129
*/
130130
TokenAssert.prototype.indentation = function(options) {
131+
var token = options.token;
131132
var lineNumber = options.lineNumber;
132133
var actual = options.actual;
133134
var expected = options.expected;
@@ -146,19 +147,10 @@ TokenAssert.prototype.indentation = function(options) {
146147
});
147148
}
148149

149-
var token = this._file.getFirstTokenOnLine(lineNumber, {
150-
includeComments: true
151-
});
152150
var newWhitespace = (new Array(expected + 1)).join(indentChar);
153151

154-
if (!token) {
155-
this._setEmptyLineIndentation(lineNumber, newWhitespace);
156-
return;
157-
}
158-
159152
this._updateWhitespaceByLine(token, function(lines) {
160153
lines[lines.length - 1] = newWhitespace;
161-
162154
return lines;
163155
});
164156

@@ -210,33 +202,6 @@ TokenAssert.prototype._updateCommentWhitespace = function(token, indentChar, act
210202
token.parentElement.replaceChild(newComment, token);
211203
};
212204

213-
/**
214-
* Fixes the indentation of a line that has no tokens on it
215-
*
216-
* @param {Number} lineNumber
217-
* @param {String} newWhitespace
218-
*/
219-
TokenAssert.prototype._setEmptyLineIndentation = function(lineNumber, newWhitespace) {
220-
var token;
221-
do {
222-
token = this._file.getFirstTokenOnLine(++lineNumber, {
223-
includeComments: true
224-
});
225-
} while (!token);
226-
227-
this._updateWhitespaceByLine(token, function(lines) {
228-
if (lines[0] !== '') {
229-
lines[0] = newWhitespace;
230-
}
231-
232-
for (var i = 1; i < lines.length; i++) {
233-
lines[i] = newWhitespace;
234-
}
235-
236-
return lines;
237-
});
238-
};
239-
240205
/**
241206
* Requires tokens to be on the same line.
242207
*

test/specs/token-assert.js

Lines changed: 7 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,7 @@ describe('token-assert', function() {
12161216
});
12171217
});
12181218

1219-
describe('indentation', function() {
1219+
describe.only('indentation', function() {
12201220
it('should not trigger on correct indentation', function() {
12211221
var file = createJsFile('x=y;');
12221222

@@ -1225,7 +1225,7 @@ describe('token-assert', function() {
12251225
tokenAssert.on('error', onError);
12261226

12271227
tokenAssert.indentation({
1228-
lineNumber: 1,
1228+
token: file.getProgram().getFirstToken(),
12291229
actual: 0,
12301230
expected: 0,
12311231
indentChar: ' '
@@ -1242,7 +1242,7 @@ describe('token-assert', function() {
12421242
tokenAssert.on('error', onError);
12431243

12441244
tokenAssert.indentation({
1245-
lineNumber: 1,
1245+
token: file.getProgram().getFirstToken().getNextCodeToken(),
12461246
actual: 2,
12471247
expected: 0,
12481248
indentChar: ' '
@@ -1259,7 +1259,7 @@ describe('token-assert', function() {
12591259
tokenAssert.on('error', onError);
12601260

12611261
tokenAssert.indentation({
1262-
lineNumber: 1,
1262+
token: file.getProgram().getFirstToken().getNextCodeToken(),
12631263
actual: 2,
12641264
expected: 0,
12651265
indentChar: ' ',
@@ -1277,7 +1277,7 @@ describe('token-assert', function() {
12771277
tokenAssert.on('error', onError);
12781278

12791279
tokenAssert.indentation({
1280-
lineNumber: 1,
1280+
token: file.getProgram().getFirstToken().getNextCodeToken(),
12811281
actual: 2,
12821282
expected: 0,
12831283
indentChar: ' '
@@ -1286,42 +1286,6 @@ describe('token-assert', function() {
12861286
expect(file.getWhitespaceBefore(file.getFirstToken())).to.equal('');
12871287
});
12881288

1289-
it('should fix whitespace on incorrect indentation for the second token', function() {
1290-
var file = createJsFile('x=y;');
1291-
1292-
var tokenAssert = new TokenAssert(file);
1293-
var onError = sinon.spy();
1294-
tokenAssert.on('error', onError);
1295-
1296-
tokenAssert.indentation({
1297-
lineNumber: 1,
1298-
actual: 0,
1299-
expected: 2,
1300-
indentChar: ' '
1301-
});
1302-
1303-
var token = file.getFirstToken().getNextToken();
1304-
expect(file.getWhitespaceBefore(token)).to.equal(' ');
1305-
});
1306-
1307-
it('should fix empty line whitespace on incorrect indentation', function() {
1308-
var file = createJsFile(' \n \nx=y;');
1309-
1310-
var tokenAssert = new TokenAssert(file);
1311-
var onError = sinon.spy();
1312-
tokenAssert.on('error', onError);
1313-
1314-
tokenAssert.indentation({
1315-
lineNumber: 1,
1316-
actual: 2,
1317-
expected: 0,
1318-
indentChar: ' '
1319-
});
1320-
1321-
var token = file.findNextToken(file.getFirstToken(), 'Identifier', 'x');
1322-
expect(file.getWhitespaceBefore(token)).to.equal('\n\n');
1323-
});
1324-
13251289
it('should fix docblock on incorrect overindentation', function() {
13261290
var file = createJsFile(' /*\n *\n */\nx=y;');
13271291

@@ -1330,7 +1294,7 @@ describe('token-assert', function() {
13301294
tokenAssert.on('error', onError);
13311295

13321296
tokenAssert.indentation({
1333-
lineNumber: 1,
1297+
token: file.getProgram().getFirstToken().getNextNonWhitespaceToken(),
13341298
actual: 2,
13351299
expected: 0,
13361300
indentChar: ' '
@@ -1349,7 +1313,7 @@ describe('token-assert', function() {
13491313
tokenAssert.on('error', onError);
13501314

13511315
tokenAssert.indentation({
1352-
lineNumber: 1,
1316+
token: file.getProgram().getFirstToken().getNextNonWhitespaceToken(),
13531317
actual: 2,
13541318
expected: 4,
13551319
indentChar: ' '
@@ -1359,41 +1323,5 @@ describe('token-assert', function() {
13591323
expect(file.getWhitespaceBefore(newToken)).to.equal(' ');
13601324
expect(newToken.value).to.equal('\n *\n ');
13611325
});
1362-
1363-
it('should fix whitespace after docblock on incorrect indentation', function() {
1364-
var file = createJsFile('/**/\n \nx=y;');
1365-
1366-
var tokenAssert = new TokenAssert(file);
1367-
var onError = sinon.spy();
1368-
var token = file.getFirstTokenOnLine(3);
1369-
tokenAssert.on('error', onError);
1370-
1371-
tokenAssert.indentation({
1372-
lineNumber: 2,
1373-
actual: 2,
1374-
expected: 0,
1375-
indentChar: ' '
1376-
});
1377-
1378-
expect(file.getWhitespaceBefore(token)).to.equal('\n\n');
1379-
});
1380-
1381-
it('should not lose lines with mixed line endings', function() {
1382-
var file = createJsFile(' \r\n \r\n \nx=y;');
1383-
1384-
var tokenAssert = new TokenAssert(file);
1385-
var onError = sinon.spy();
1386-
var token = file.getFirstTokenOnLine(4);
1387-
tokenAssert.on('error', onError);
1388-
1389-
tokenAssert.indentation({
1390-
lineNumber: 1,
1391-
actual: 2,
1392-
expected: 0,
1393-
indentChar: ' '
1394-
});
1395-
1396-
expect(file.getWhitespaceBefore(token)).to.equal('\r\n\r\n\r\n');
1397-
});
13981326
});
13991327
});

0 commit comments

Comments
 (0)