/
ProgramPatcher.js
146 lines (128 loc) · 3.61 KB
/
ProgramPatcher.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
import SharedProgramPatcher from '../../../patchers/SharedProgramPatcher';
import getIndent from '../../../utils/getIndent';
import { SourceType } from 'coffee-lex';
import type { SourceToken } from './../../../patchers/types';
const BLOCK_COMMENT_DELIMITER = '###';
export default class ProgramPatcher extends SharedProgramPatcher {
canPatchAsExpression(): boolean {
return false;
}
patchAsStatement() {
if (this.body) {
this.body.patch({ leftBrace: false, rightBrace: false });
}
this.patchContinuations();
this.patchComments();
this.patchHelpers();
}
/**
* Removes continuation tokens (i.e. '\' at the end of a line).
*
* @private
*/
patchContinuations() {
this.getProgramSourceTokens().forEach(token => {
if (token.type === SourceType.CONTINUATION) {
this.remove(token.start, token.end);
}
});
}
/**
* Replaces CoffeeScript style comments with JavaScript style comments.
*
* @private
*/
patchComments() {
let { source } = this.context;
this.getProgramSourceTokens().forEach(token => {
if (token.type === SourceType.COMMENT) {
if (token.start === 0 && source[1] === '!') {
this.patchShebangComment(token);
} else {
this.patchLineComment(token);
}
} else if (token.type === SourceType.HERECOMMENT) {
this.patchBlockComment(token);
}
});
}
/**
* Patches a block comment.
*
* @private
*/
patchBlockComment(comment: SourceToken) {
let { start, end } = comment;
this.overwrite(start, start + BLOCK_COMMENT_DELIMITER.length, '/*');
let atStartOfLine = false;
let lastStartOfLine = null;
let lineUpAsterisks = true;
let isMultiline = false;
let { source } = this.context;
let expectedIndent = getIndent(source, start);
let leadingHashIndexes = [];
for (let index = start + BLOCK_COMMENT_DELIMITER.length; index < end - BLOCK_COMMENT_DELIMITER.length; index++) {
switch (source[index]) {
case '\n':
isMultiline = true;
atStartOfLine = true;
lastStartOfLine = index + '\n'.length;
break;
case ' ':
case '\t':
break;
case '#':
if (atStartOfLine) {
leadingHashIndexes.push(index);
atStartOfLine = false;
if (source.slice(lastStartOfLine, index) !== expectedIndent) {
lineUpAsterisks = false;
}
}
break;
default:
if (atStartOfLine) {
atStartOfLine = false;
lineUpAsterisks = false;
}
break;
}
}
leadingHashIndexes.forEach(index => {
this.overwrite(index, index + '#'.length, lineUpAsterisks ? ' *' : '*');
});
this.overwrite(end - BLOCK_COMMENT_DELIMITER.length, end, isMultiline && lineUpAsterisks ? ' */' : '*/');
}
/**
* Patches a single-line comment.
*
* @private
*/
patchLineComment(comment: SourceToken) {
let { start } = comment;
this.overwrite(start, start + '#'.length, '//');
}
/**
* Patches a shebang comment.
*
* @private
*/
patchShebangComment(comment: SourceToken) {
let { start, end } = comment;
let commentBody = this.slice(start, end);
let coffeeIndex = commentBody.indexOf('coffee');
if (coffeeIndex >= 0) {
this.overwrite(
start + coffeeIndex,
start + coffeeIndex + 'coffee'.length,
'node'
);
}
}
/**
* Serve as the implicit return patcher for anyone not included in a function.
*/
canHandleImplicitReturn(): boolean {
return true;
}
}