Skip to content

Commit 477e390

Browse files
authoredSep 11, 2022
Fix detecting the indent of files with many repeats after a single indent (#34)
1 parent 6062d95 commit 477e390

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed
 

Diff for: ‎fixture/long-repeat.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
/* Examine the case of an indent that happens only once
3+
but then has a long block at that level, as compared to
4+
an indent that occurs many times.
5+
The odd spacing on this line is artificial, but critical to the example. */
6+
observation: 'This is literally the only line with a 1-space change',
7+
key1: 'dummy',
8+
key2: 'dummy',
9+
key3: 'dummy',
10+
key4: 'dummy',
11+
key5: 'dummy',
12+
key6: 'dummy',
13+
key7: 'dummy',
14+
key8: 'dummy',
15+
key9: 'dummy',
16+
key10: 'dummy',
17+
key11: 'dummy',
18+
key12: 'dummy',
19+
key13: 'dummy',
20+
key14: 'dummy',
21+
key15: 'dummy',
22+
key16: 'dummy',
23+
key17: 'dummy',
24+
key18: 'dummy',
25+
key19: 'dummy',
26+
key20: 'dummy',
27+
contrast: {
28+
idea: 'Now we will have about ten instances with an indent change of 4',
29+
},
30+
contrast2: {
31+
subkey: 'dummy'
32+
},
33+
contrast3: {
34+
subkey: 'dummy'
35+
},
36+
contrast4: {
37+
subkey: 'dummy'
38+
},
39+
contrast5: {
40+
subkey: 'dummy'
41+
},
42+
}
43+

Diff for: ‎index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function makeIndentsMap(string, ignoreSingleSpaces) {
3636

3737
let indent;
3838
let indentType;
39+
let use;
3940
let weight;
4041
let entry;
4142
const matches = line.match(INDENT_REGEX);
@@ -58,14 +59,18 @@ function makeIndentsMap(string, ignoreSingleSpaces) {
5859

5960
previousIndentType = indentType;
6061

62+
use = 1;
6163
weight = 0;
6264

6365
const indentDifference = indent - previousSize;
6466
previousSize = indent;
6567

6668
// Previous line have same indent?
6769
if (indentDifference === 0) {
68-
weight++;
70+
// Not a new "use" of the current indent:
71+
use = 0;
72+
// But do add a bit to it for breaking ties:
73+
weight = 1;
6974
// We use the key from previous loop
7075
} else {
7176
const absoluteIndentDifference = indentDifference > 0 ? indentDifference : -indentDifference;
@@ -74,7 +79,7 @@ function makeIndentsMap(string, ignoreSingleSpaces) {
7479

7580
// Update the stats
7681
entry = indents.get(key);
77-
entry = entry === undefined ? [1, 0] : [++entry[0], entry[1] + weight];
82+
entry = entry === undefined ? [1, 0] : [entry[0] + use, entry[1] + weight];
7883

7984
indents.set(key, entry);
8085
}

Diff for: ‎test.js

+5
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,8 @@ test('return indentations status for indented files with single spaces only', t
140140
type: 'space',
141141
});
142142
});
143+
144+
test('detect the indent of a file with many repeats after a single indent', t => {
145+
const stats = detectIndent(getFile('fixture/long-repeat.js'));
146+
t.is(stats.amount, 4);
147+
});

0 commit comments

Comments
 (0)
Please sign in to comment.