1 | "use strict";
|
2 |
|
3 | module.exports = removeUseStrict;
|
4 | const newIssueUrl = "https://github.com/babel/minify/issues/new";
|
5 | const useStrict = "use strict";
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function removeUseStrict(block) {
|
15 | if (!block.isBlockStatement()) {
|
16 | throw new Error(`Received ${block.type}. Expected BlockStatement. ` + `Please report at ${newIssueUrl}`);
|
17 | }
|
18 |
|
19 | const useStricts = getUseStrictDirectives(block);
|
20 |
|
21 | if (useStricts.length < 1) return;
|
22 |
|
23 | if (useStricts.length > 1) {
|
24 | for (let i = 1; i < useStricts.length; i++) {
|
25 | useStricts[i].remove();
|
26 | }
|
27 | }
|
28 |
|
29 |
|
30 | if (hasStrictParent(block)) {
|
31 | useStricts[0].remove();
|
32 | }
|
33 | }
|
34 |
|
35 | function hasStrictParent(path) {
|
36 | return path.findParent(parent => parent.isBlockStatement() && isStrict(parent));
|
37 | }
|
38 |
|
39 | function isStrict(block) {
|
40 | return getUseStrictDirectives(block).length > 0;
|
41 | }
|
42 |
|
43 | function getUseStrictDirectives(block) {
|
44 | var dir = block.get("directives");
|
45 | return Array.isArray(dir) ? dir.filter(function (directive) {
|
46 | return directive.node.value.value === useStrict;
|
47 | }) : [];
|
48 | } |
\ | No newline at end of file |