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

Commit 0f2e90a

Browse files
rafecasadick254
authored andcommitted
Re-apply prettier JS formatter
1 parent e4b7ae6 commit 0f2e90a

4 files changed

Lines changed: 163 additions & 132 deletions

File tree

package-lock.json

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/tree-indenter-spec.js

Lines changed: 65 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
const {beforeEach} = require('./async-spec-helpers')
1+
const { beforeEach } = require('./async-spec-helpers');
22

3-
const fs = require('fs')
4-
const path = require('path')
5-
const TreeSitterGrammar = require('../src/tree-sitter-grammar')
6-
const TreeSitterLanguageMode = require('../src/tree-sitter-language-mode')
7-
const TreeIndenter = require('../src/tree-indenter')
3+
const fs = require('fs');
4+
const path = require('path');
5+
const TreeSitterGrammar = require('../src/tree-sitter-grammar');
6+
const TreeSitterLanguageMode = require('../src/tree-sitter-language-mode');
7+
const TreeIndenter = require('../src/tree-indenter');
88

9-
const jsGrammarPath = require.resolve('language-javascript/grammars/tree-sitter-javascript.cson')
9+
const jsGrammarPath = require.resolve(
10+
'language-javascript/grammars/tree-sitter-javascript.cson'
11+
);
1012

1113
const TAB_LENGTH = 2;
1214

@@ -22,7 +24,7 @@ const jsScopes = {
2224
jsx_opening_element: true,
2325
jsx_expression: true,
2426
switch_body: true,
25-
comment: true,
27+
comment: true
2628
},
2729
indentExceptFirst: {
2830
member_expression: true,
@@ -35,91 +37,108 @@ const jsScopes = {
3537
},
3638
indentExceptFirstOrBlock: {
3739
if_statement: true,
38-
while_statement: true,
40+
while_statement: true
3941
},
4042
types: {
4143
indent: {},
4244
outdent: {
4345
else: true
4446
}
4547
}
46-
}
47-
48+
};
4849

4950
describe('TreeIndenter', () => {
50-
let editor, buffer, grammar
51+
let editor, buffer, grammar;
52+
let languageMode, treeIndenter;
5153

5254
beforeEach(async () => {
53-
editor = await atom.workspace.open('')
54-
buffer = editor.getBuffer()
55-
editor.displayLayer.reset({foldCharacter: '…'})
55+
editor = await atom.workspace.open('');
56+
buffer = editor.getBuffer();
57+
editor.displayLayer.reset({ foldCharacter: '…' });
5658

5759
grammar = new TreeSitterGrammar(atom.grammars, jsGrammarPath, {
58-
parser: 'tree-sitter-javascript',
59-
})
60-
})
60+
parser: 'tree-sitter-javascript'
61+
});
62+
});
6163

6264
/** load a file from disk and verify that our proposed indentation
6365
is the same as it is in the file */
6466
function compareFile(filename) {
65-
const text = fs.readFileSync(filename)
66-
buffer.setText(text)
67-
languageMode = new TreeSitterLanguageMode({buffer, grammar})
68-
treeIndenter = new TreeIndenter(languageMode, jsScopes)
67+
const text = fs.readFileSync(filename);
68+
buffer.setText(text);
69+
languageMode = new TreeSitterLanguageMode({ buffer, grammar });
70+
treeIndenter = new TreeIndenter(languageMode, jsScopes);
6971

7072
for (let row = 0; row < buffer.getLineCount(); row++) {
7173
// get current (correct) indentation
72-
const line = buffer.lineForRow(row)
73-
const currentIndentation = languageMode.indentLevelForLine(line, TAB_LENGTH)
74+
const line = buffer.lineForRow(row);
75+
const currentIndentation = languageMode.indentLevelForLine(
76+
line,
77+
TAB_LENGTH
78+
);
7479

7580
// get suggested indentation
76-
const indent = treeIndenter.suggestedIndentForBufferRow(row, TAB_LENGTH, {})
81+
const indent = treeIndenter.suggestedIndentForBufferRow(
82+
row,
83+
TAB_LENGTH,
84+
{}
85+
);
7786

7887
// verify
79-
if (indent != currentIndentation) {
80-
throw Error(`failure in file row ${row+1}: suggested ${indent} but ${currentIndentation} is correct (${line})`);
88+
if (indent !== currentIndentation) {
89+
throw Error(
90+
`failure in file row ${row +
91+
1}: suggested ${indent} but ${currentIndentation} is correct (${line})`
92+
);
8193
} else {
8294
expect(indent).toEqual(currentIndentation);
8395
}
8496
}
8597
}
8698

87-
8899
describe('indentation', () => {
89-
90100
it('indents wrongly indented lines', () => {
91101
buffer.setText(`if (true) {
92102
a = {a: [
93103
1,
94104
'something'
95105
],
96106
b: 2}
97-
}`)
98-
const correct = [0,1,3,3,2,2,0]
99-
languageMode = new TreeSitterLanguageMode({buffer, grammar})
100-
treeIndenter = new TreeIndenter(languageMode, jsScopes)
107+
}`);
108+
const correct = [0, 1, 3, 3, 2, 2, 0];
109+
languageMode = new TreeSitterLanguageMode({ buffer, grammar });
110+
treeIndenter = new TreeIndenter(languageMode, jsScopes);
101111

102112
for (let row = 0; row < buffer.getLineCount(); row++) {
103113
// get suggested indentation
104-
const indent = treeIndenter.suggestedIndentForBufferRow(row, TAB_LENGTH, {})
114+
const indent = treeIndenter.suggestedIndentForBufferRow(
115+
row,
116+
TAB_LENGTH,
117+
{}
118+
);
105119

106120
// verify
107-
if (indent != correct[row]) {
108-
const line = buffer.lineForRow(row).trim()
109-
throw Error(`failure in row ${row}: suggested ${indent} but ${correct[row]} is correct (${line})`);
121+
if (indent !== correct[row]) {
122+
const line = buffer.lineForRow(row).trim();
123+
throw Error(
124+
`failure in row ${row}: suggested ${indent} but ${
125+
correct[row]
126+
} is correct (${line})`
127+
);
110128
} else {
111129
expect(indent).toEqual(correct[row]);
112130
}
113131
}
114-
})
132+
});
115133

116-
const fixtures = fs.readdirSync(path.join(__dirname, 'fixtures', 'indentation'))
134+
const fixtures = fs.readdirSync(
135+
path.join(__dirname, 'fixtures', 'indentation')
136+
);
117137

118-
fixtures.forEach((filename) => {
138+
fixtures.forEach(filename => {
119139
it(`suggests correct indentations for ${filename}`, () => {
120-
compareFile(path.join(__dirname, 'fixtures', 'indentation', filename))
121-
})
122-
})
123-
124-
})
125-
})
140+
compareFile(path.join(__dirname, 'fixtures', 'indentation', filename));
141+
});
142+
});
143+
});
144+
});

src/tree-indenter.js

Lines changed: 72 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
1-
21
// const log = console.debug // in dev
3-
const log = () => {} // in production
2+
const log = () => {}; // in production
43

54
module.exports = class TreeIndenter {
6-
constructor (languageMode, scopes = undefined) {
7-
this.languageMode = languageMode
8-
this.scopes = scopes || languageMode.config.get('editor.scopes',
9-
{scope: this.languageMode.rootScopeDescriptor})
10-
log('[TreeIndenter] constructor', this.scopes)
5+
constructor(languageMode, scopes = undefined) {
6+
this.languageMode = languageMode;
7+
this.scopes =
8+
scopes ||
9+
languageMode.config.get('editor.scopes', {
10+
scope: this.languageMode.rootScopeDescriptor
11+
});
12+
log('[TreeIndenter] constructor', this.scopes);
1113
}
1214

1315
/** tree indenter is configured for this language */
14-
get isConfigured () {
15-
return (!!this.scopes)
16+
get isConfigured() {
17+
return !!this.scopes;
1618
}
1719

1820
// Given a position, walk up the syntax tree, to find the highest level
1921
// node that still starts here. This is to identify the column where this
2022
// node (e.g., an HTML closing tag) ends.
21-
_getHighestSyntaxNodeAtPosition (row, column = null) {
23+
_getHighestSyntaxNodeAtPosition(row, column = null) {
2224
if (column == null) {
2325
// Find the first character on the row that is not whitespace + 1
24-
column = this.languageMode.buffer.lineForRow(row).search(/\S/) + 1
26+
column = this.languageMode.buffer.lineForRow(row).search(/\S/) + 1;
2527
}
2628

27-
let syntaxNode
29+
let syntaxNode;
2830
if (column >= 0) {
29-
syntaxNode = this.languageMode.getSyntaxNodeAtPosition({row, column})
30-
while (syntaxNode && syntaxNode.parent &&
31-
syntaxNode.parent.startPosition.row === syntaxNode.startPosition.row &&
32-
syntaxNode.parent.endPosition.row === syntaxNode.startPosition.row &&
33-
syntaxNode.parent.startPosition.column === syntaxNode.startPosition.column
31+
syntaxNode = this.languageMode.getSyntaxNodeAtPosition({ row, column });
32+
while (
33+
syntaxNode &&
34+
syntaxNode.parent &&
35+
syntaxNode.parent.startPosition.row === syntaxNode.startPosition.row &&
36+
syntaxNode.parent.endPosition.row === syntaxNode.startPosition.row &&
37+
syntaxNode.parent.startPosition.column ===
38+
syntaxNode.startPosition.column
3439
) {
35-
syntaxNode = syntaxNode.parent
40+
syntaxNode = syntaxNode.parent;
3641
}
37-
return syntaxNode
42+
return syntaxNode;
3843
}
3944
}
4045

@@ -47,74 +52,87 @@ module.exports = class TreeIndenter {
4752
It might make more sense to reverse the direction of this walk, i.e.,
4853
go from root to leaf instead.
4954
*/
50-
_treeWalk (node, lastScope = null) {
55+
_treeWalk(node, lastScope = null) {
5156
if (node == null || node.parent == null) {
52-
return 0
57+
return 0;
5358
} else {
54-
let increment = 0
59+
let increment = 0;
5560

5661
const notFirstOrLastSibling =
57-
(node.previousSibling != null && node.nextSibling != null)
62+
node.previousSibling != null && node.nextSibling != null;
5863

59-
const isScope = this.scopes.indent[node.parent.type]
60-
notFirstOrLastSibling && isScope && increment++
64+
const isScope = this.scopes.indent[node.parent.type];
65+
notFirstOrLastSibling && isScope && increment++;
6166

62-
const isScope2 = this.scopes.indentExceptFirst[node.parent.type]
63-
!increment && isScope2 && node.previousSibling != null && increment++
67+
const isScope2 = this.scopes.indentExceptFirst[node.parent.type];
68+
!increment && isScope2 && node.previousSibling != null && increment++;
6469

65-
const isScope3 = this.scopes.indentExceptFirstOrBlock[node.parent.type]
66-
!increment && isScope3 && node.previousSibling != null && increment++
70+
const isScope3 = this.scopes.indentExceptFirstOrBlock[node.parent.type];
71+
!increment && isScope3 && node.previousSibling != null && increment++;
6772

6873
// apply current row, single line, type-based rules, e.g., 'else' or 'private:'
69-
let typeDent = 0
70-
this.scopes.types.indent[node.type] && typeDent++
71-
this.scopes.types.outdent[node.type] && increment && typeDent--
72-
increment += typeDent
74+
let typeDent = 0;
75+
this.scopes.types.indent[node.type] && typeDent++;
76+
this.scopes.types.outdent[node.type] && increment && typeDent--;
77+
increment += typeDent;
7378

7479
// check whether the last (lower) indentation happend due to a scope that
7580
// started on the same row and ends directly before this.
76-
if (lastScope && increment > 0 &&
81+
if (
82+
lastScope &&
83+
increment > 0 &&
7784
// previous (lower) scope was a two-sided scope, reduce if starts on
7885
// same row and ends right before
7986
// TODO: this currently only works for scopes that have a single-character
8087
// closing delimiter (like statement_blocks, but not HTML, for instance).
8188
((node.parent.startPosition.row === lastScope.node.startPosition.row &&
82-
(node.parent.endIndex <= lastScope.node.endIndex + 1)) ||
89+
node.parent.endIndex <= lastScope.node.endIndex + 1) ||
8390
// or this is a special scope (like if, while) and it's ends coincide
84-
(isScope3 && lastScope.node.endIndex === node.endIndex))) {
85-
log('ignoring repeat', node.parent.type, lastScope)
86-
increment = 0
91+
(isScope3 && lastScope.node.endIndex === node.endIndex))
92+
) {
93+
log('ignoring repeat', node.parent.type, lastScope);
94+
increment = 0;
8795
}
8896

89-
log('treewalk', {node, notFirstOrLastSibling, type: node.parent.type, increment})
90-
const newLastScope = (isScope || isScope2 ? {node: node.parent} : lastScope)
91-
return this._treeWalk(node.parent, newLastScope) + increment
97+
log('treewalk', {
98+
node,
99+
notFirstOrLastSibling,
100+
type: node.parent.type,
101+
increment
102+
});
103+
const newLastScope =
104+
isScope || isScope2 ? { node: node.parent } : lastScope;
105+
return this._treeWalk(node.parent, newLastScope) + increment;
92106
}
93107
}
94108

95-
suggestedIndentForBufferRow (row, tabLength, options) {
109+
suggestedIndentForBufferRow(row, tabLength, options) {
96110
// get current indentation for row
97-
const line = this.languageMode.buffer.lineForRow(row)
98-
const currentIndentation = this.languageMode.indentLevelForLine(line, tabLength)
111+
const line = this.languageMode.buffer.lineForRow(row);
112+
const currentIndentation = this.languageMode.indentLevelForLine(
113+
line,
114+
tabLength
115+
);
99116

100-
const syntaxNode = this._getHighestSyntaxNodeAtPosition(row)
117+
const syntaxNode = this._getHighestSyntaxNodeAtPosition(row);
101118
if (!syntaxNode) {
102-
return 0
119+
return 0;
103120
}
104-
let indentation = this._treeWalk(syntaxNode)
121+
let indentation = this._treeWalk(syntaxNode);
105122

106123
// Special case for comments
107-
if (syntaxNode.type === 'comment' &&
124+
if (
125+
syntaxNode.type === 'comment' &&
108126
syntaxNode.startPosition.row < row &&
109-
syntaxNode.endPosition.row > row) {
110-
indentation += 1
127+
syntaxNode.endPosition.row > row
128+
) {
129+
indentation += 1;
111130
}
112131

113132
if (options && options.preserveLeadingWhitespace) {
114-
indentation -= currentIndentation
133+
indentation -= currentIndentation;
115134
}
116135

117-
return indentation
136+
return indentation;
118137
}
119-
120-
}
138+
};

0 commit comments

Comments
 (0)