forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree-indenter-spec.js
142 lines (127 loc) · 3.71 KB
/
tree-indenter-spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const fs = require('fs');
const path = require('path');
const TreeSitterGrammar = require('../src/tree-sitter-grammar');
const TreeSitterLanguageMode = require('../src/tree-sitter-language-mode');
const TreeIndenter = require('../src/tree-indenter');
const jsGrammarPath = require.resolve(
'language-javascript/grammars/tree-sitter-javascript.cson'
);
const TAB_LENGTH = 2;
const jsScopes = {
indent: {
array: true,
object: true,
arguments: true,
statement_block: true,
class_body: true,
parenthesized_expression: true,
jsx_element: true,
jsx_opening_element: true,
jsx_expression: true,
switch_body: true,
comment: true
},
indentExceptFirst: {
member_expression: true,
assignment_expression: true,
expression_statement: true,
variable_declarator: true,
lexical_declaration: true,
binary_expression: true,
jsx_self_closing_element: true
},
indentExceptFirstOrBlock: {
if_statement: true,
while_statement: true
},
types: {
indent: {},
outdent: {
else: true
}
}
};
describe('TreeIndenter', () => {
let editor, buffer, grammar;
let languageMode, treeIndenter;
beforeEach(async () => {
editor = await atom.workspace.open('');
buffer = editor.getBuffer();
editor.displayLayer.reset({ foldCharacter: '…' });
grammar = new TreeSitterGrammar(atom.grammars, jsGrammarPath, {
parser: 'tree-sitter-javascript'
});
});
/** load a file from disk and verify that our proposed indentation
is the same as it is in the file */
function compareFile(filename) {
const text = fs.readFileSync(filename);
buffer.setText(text);
languageMode = new TreeSitterLanguageMode({ buffer, grammar });
treeIndenter = new TreeIndenter(languageMode, jsScopes);
for (let row = 0; row < buffer.getLineCount(); row++) {
// get current (correct) indentation
const line = buffer.lineForRow(row);
const currentIndentation = languageMode.indentLevelForLine(
line,
TAB_LENGTH
);
// get suggested indentation
const indent = treeIndenter.suggestedIndentForBufferRow(
row,
TAB_LENGTH,
{}
);
// verify
if (indent !== currentIndentation) {
throw Error(
`failure in file row ${row +
1}: suggested ${indent} but ${currentIndentation} is correct (${line})`
);
} else {
expect(indent).toEqual(currentIndentation);
}
}
}
describe('indentation', () => {
it('indents wrongly indented lines', () => {
buffer.setText(`if (true) {
a = {a: [
1,
'something'
],
b: 2}
}`);
const correct = [0, 1, 3, 3, 2, 2, 0];
languageMode = new TreeSitterLanguageMode({ buffer, grammar });
treeIndenter = new TreeIndenter(languageMode, jsScopes);
for (let row = 0; row < buffer.getLineCount(); row++) {
// get suggested indentation
const indent = treeIndenter.suggestedIndentForBufferRow(
row,
TAB_LENGTH,
{}
);
// verify
if (indent !== correct[row]) {
const line = buffer.lineForRow(row).trim();
throw Error(
`failure in row ${row}: suggested ${indent} but ${
correct[row]
} is correct (${line})`
);
} else {
expect(indent).toEqual(correct[row]);
}
}
});
const fixtures = fs.readdirSync(
path.join(__dirname, 'fixtures', 'indentation')
);
fixtures.forEach(filename => {
it(`suggests correct indentations for ${filename}`, () => {
compareFile(path.join(__dirname, 'fixtures', 'indentation', filename));
});
});
});
});