-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (36 loc) · 1.04 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
const espower = require('espower')
const acorn = require('acorn')
const escodegen = require('escodegen')
module.exports = [{
ext: '.js',
/**
* Enhance current file content with espower assertions
* @param {String} content - parsed file content
* @param {String} filename - relative file path
* @returns {String} content - enhanced Javascript content
*/
transform: (content, filename) => {
if (filename.indexOf('node_modules') !== -1 || content.indexOf('power-assert') === -1) return content
let ast
const comments = []
const tokens = []
try {
ast = acorn.parse(content, {
ecmaVersion: 8,
ranges: true,
locations: true,
sourceFile: true,
onComment: comments,
onToken: tokens
})
} catch (err) {
// remove stack for simpler errors
delete err.stack
throw err
}
// attach comments using collected information
escodegen.attachComments(ast, comments, tokens)
// generate code
return escodegen.generate(espower(ast))
}
}]