-
Notifications
You must be signed in to change notification settings - Fork 119
/
BlockPatcher.js
167 lines (154 loc) · 5.34 KB
/
BlockPatcher.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import FunctionPatcher from './FunctionPatcher.js';
import NodePatcher from './../../../patchers/NodePatcher.js';
import ReturnPatcher from './ReturnPatcher.js';
import type { SourceToken, Node, ParseContext, Editor } from './../../../patchers/types.js';
import { NEWLINE, SEMICOLON } from 'coffee-lex';
export default class BlockPatcher extends NodePatcher {
statements: Array<NodePatcher>;
constructor(node: Node, context: ParseContext, editor: Editor, statements: Array<NodePatcher>) {
super(node, context, editor);
this.statements = statements;
}
canPatchAsExpression(): boolean {
return this.statements.every(
statement => statement.prefersToPatchAsExpression()
);
}
setExpression(force=false): boolean {
let willPatchAsExpression = super.setExpression(force);
if (willPatchAsExpression && this.prefersToPatchAsExpression()) {
this.statements.forEach(statement => statement.setExpression());
}
}
setImplicitlyReturns() {
this.statements[this.statements.length - 1].setImplicitlyReturns();
}
patchAsStatement({ leftBrace=true, rightBrace=true }={}) {
if (leftBrace) {
this.insert(this.innerStart, '{');
}
this.statements.forEach(
(statement, i, statements) => {
if (i === statements.length - 1 && this.parent instanceof FunctionPatcher) {
if (statement instanceof ReturnPatcher && !statement.expression) {
let removeStart;
if (statements.length > 1) {
let startOfLineIndex = this.context.sourceTokens.lastIndexOfTokenMatchingPredicate(
token => token.type === NEWLINE || token.type === SEMICOLON,
statement.outerStartTokenIndex
);
removeStart = this.sourceTokenAtIndex(startOfLineIndex).start;
} else {
removeStart = statement.outerStart;
}
this.remove(
removeStart,
statement.outerEnd
);
return;
}
}
if (statement.isSurroundedByParentheses()) {
statement.setRequiresExpression();
}
let hasImplicitReturn = (
statement.implicitlyReturns() &&
!statement.explicitlyReturns()
);
let implicitReturnPatcher = hasImplicitReturn ?
this.implicitReturnPatcher() : null;
if (implicitReturnPatcher) {
implicitReturnPatcher.patchImplicitReturnStart(statement);
}
statement.patch();
if (implicitReturnPatcher) {
implicitReturnPatcher.patchImplicitReturnEnd(statement);
}
if (statement.statementNeedsSemicolon()) {
this.insert(statement.outerEnd, ';');
}
}
);
if (rightBrace) {
if (this.inline()) {
this.insert(this.innerEnd, ' }');
} else {
this.appendLineAfter('}', -1);
}
}
}
patchAsExpression({
leftBrace=this.statements.length > 1,
rightBrace=this.statements.length > 1
}={}) {
if (leftBrace) {
this.insert(this.innerStart, '(');
}
this.statements.forEach(
(statement, i, statements) => {
statement.patch();
if (i !== statements.length - 1) {
let semicolonTokenIndex = this.getSemicolonSourceTokenBetween(
statement,
statements[i + 1]
);
if (semicolonTokenIndex) {
let semicolonToken = this.sourceTokenAtIndex(semicolonTokenIndex);
this.overwrite(semicolonToken.start, semicolonToken.end, ',');
} else {
this.insert(statement.outerEnd, ',');
}
}
}
);
if (rightBrace) {
this.insert(this.innerEnd, ')');
}
}
/**
* Insert statements somewhere in this block.
*/
insertStatementsAtIndex(statements: Array<string>, index: number) {
let separator = this.inline() ? '; ' : ';\n';
if (index === this.statements.length) {
let lastStatement = this.statements[this.statements.length - 1];
let terminatorTokenIndex = this.context.sourceTokens.indexOfTokenMatchingPredicate(
token => token.type === NEWLINE || token.type === SEMICOLON,
lastStatement.outerEndTokenIndex
);
let insertionPoint = terminatorTokenIndex ?
this.sourceTokenAtIndex(terminatorTokenIndex).start :
lastStatement.outerEnd;
let indent = lastStatement.getIndent();
statements.forEach(line => this.insert(insertionPoint, `${separator}${indent}${line}`));
} else {
let statementToInsertBefore = this.statements[index];
let insertionPoint = statementToInsertBefore.outerStart;
let indent = statementToInsertBefore.getIndent();
statements.forEach(line => this.insert(insertionPoint, `${line}${separator}${indent}`));
}
}
/**
* @private
*/
getSemicolonSourceTokenBetween(left: NodePatcher, right: NodePatcher): ?SourceToken {
return this.indexOfSourceTokenBetweenPatchersMatching(
left,
right,
token => token.type === SEMICOLON
);
}
/**
* Gets whether this patcher's block is inline (on the same line as the node
* that contains it) or not.
*/
inline(): boolean {
return this.node.inline;
}
/**
* Blocks only exit via the last statement, so we check its code paths.
*/
allCodePathsPresent(): boolean {
return this.statements[this.statements.length - 1].allCodePathsPresent();
}
}