-
Notifications
You must be signed in to change notification settings - Fork 107
/
use-strict.js
36 lines (30 loc) · 854 Bytes
/
use-strict.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
module.exports = (file, api, options) => {
const j = api.jscodeshift;
const hasStrictMode = body =>
body.some(
statement => j.match(statement, {
type: 'ExpressionStatement',
expression: {
type: 'Literal',
value: 'use strict',
},
})
);
const withComments = (to, from) => {
to.comments = from.comments;
return to;
};
const createUseStrictExpression = () =>
j.expressionStatement(
j.literal('use strict')
);
const root = j(file.source);
const body = root.get().value.program.body;
if (!body.length || hasStrictMode(body)) {
return null;
}
body.unshift(withComments(createUseStrictExpression(), body[0]));
body[0].comments = body[1].comments;
delete body[1].comments;
return root.toSource(options.printOptions || {quote: 'single'});
};