-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.js
45 lines (33 loc) · 1.09 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
'use strict';
const {
isExpressionStatement,
StringLiteral,
ExpressionStatement,
} = require('putout').types;
const store = require('fullstore');
module.exports.report = () => '"use strict" directive should be on top of commonjs file';
module.exports.fix = ({node}) => {
node.body.unshift(ExpressionStatement(StringLiteral('use strict')));
};
module.exports.traverse = ({push}) => {
const isModule = store();
let added = false;
return {
'ImportDeclaration|ExportNamedDeclaration|ExportDefaultDeclaration|TypeAlias'() {
isModule(true);
},
Program: {
exit(path) {
const [first] = path.node.body;
if (added)
return;
added = true;
if (isExpressionStatement(first) && first.expression.value === 'use strict')
return;
if (isModule())
return;
push(path);
},
},
};
};