-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_file.js
53 lines (44 loc) · 1.41 KB
/
validate_file.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
const validateFunctions = require('./validate/futs'),
fs = require('fs'),
log = require('./log'),
esprima = require('esprima'),
escodegen = require('escodegen'),
getFunctions = require('./instrument/get_functions'),
instrument = require('./instrument/instrument');
function validateFile (file, callback) {
fs.readFile(
file,
'utf-8',
(error, data) => {
// javascript -> AST
let json_file;
try {
data = removeShebang(data);
json_file = esprima.parse(data);
}
catch (e) {
log.error("[Parse error]",e);
return callback();
}
let json_functions = getFunctions(json_file);
// AST -> javascript (instrumented)
let js_functions = json_functions
.map( ({name, code}) => ({
name,
code: escodegen.generate(instrument(code))
}) );
validateFunctions(js_functions);
callback(); // Resume processing changes when test complete
});
}
function removeShebang(data) {
// If the first line is a shebang (#!), remove it
if (data && data.substr(0, 2) === '#!') {
let endOfLineIndex = data.indexOf( '\n' );
if (endOfLineIndex === -1)
return "";
data = data.substr(endOfLineIndex + 1);
}
return data;
}
module.exports = validateFile;