Skip to content

Commit 3df3dd2

Browse files
committed
add removeAll option
1 parent 2fc684b commit 3df3dd2

1 file changed

Lines changed: 29 additions & 12 deletions

File tree

src/index.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
'use strict';
22

3+
// remove for file that only have 'not strict'
4+
function removeSelective(path, t) {
5+
if (path.node.value.value === 'use strict') {
6+
const siblings = path.container // get all the sibilings (code in the same level) of 'use strict'
7+
8+
for (const sibling of siblings) {
9+
if (t.isDirective(sibling)) { // if the sibiling is a directive
10+
if (sibling.value.value === 'not strict') { // check if its 'not strict'
11+
path.remove(); // remove 'use strict'
12+
break;
13+
}
14+
}
15+
}
16+
}
17+
}
18+
19+
// remove for all files
20+
function removeAll(path) {
21+
if (path.node.value.value === 'use strict') {
22+
path.node.value.value = 'not strict' // replace 'use strict' with 'not strict'
23+
}
24+
}
25+
26+
327
module.exports = function ({ types: t }) {
428
return {
529
name: 'transform-not-strict',
630
visitor: {
7-
Directive(path) {
8-
if (path.node.value.value === 'use strict') {
9-
const siblings = path.container // get all the sibilings (code in the same level) of 'use strict'
10-
11-
for (const sibling of siblings) {
12-
if (t.isDirective(sibling)) { // if the sibiling is a directive
13-
if (sibling.value.value === 'not strict') { // check if its 'not strict'
14-
path.remove(); // remove 'use strict'
15-
break;
16-
}
17-
}
18-
}
31+
Directive(path, state) {
32+
if (state.opts.removeAll) {
33+
removeAll(path) // remove for all files
34+
} else {
35+
removeSelective(path, t) // remove for file that only have 'not strict'
1936
}
2037
},
2138
},

0 commit comments

Comments
 (0)