forked from decaffeinate/decaffeinate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (42 loc) · 1.28 KB
/
index.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
import MagicString from 'magic-string';
import asi from 'automatic-semicolon-insertion';
import buildConfig from 'ast-processor-babylon-config';
import { basename } from 'path';
import { logger } from '../../utils/debug.js';
import { parse } from 'babylon';
const BABYLON_PLUGINS = [
'flow',
'jsx',
'asyncFunctions',
'asyncGenerators',
'classConstructorCall',
'classProperties',
'decorators',
'doExpressions',
'exponentiationOperator',
'exportExtensions',
'functionBind',
'functionSent',
'objectRestSpread',
'trailingFunctionCommas'
];
export default class SemicolonsStage {
static run(content: string, filename: string): { code: string, map: Object } {
let log = logger(this.name);
log(content);
let editor = new MagicString(content);
let ast = parse(content, { sourceType: 'module', plugins: BABYLON_PLUGINS });
let config = buildConfig(content, ast);
asi(config);
config.insertions.forEach(({ index, content }) => editor.appendLeft(index, content));
config.removals.forEach(({ start, end }) => editor.remove(start, end));
return {
code: editor.toString(),
map: editor.generateMap({
source: filename,
file: `${basename(filename, '.js')}-${this.name}.js`,
includeContent: true
})
};
}
}