• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// code sample of hacking old script
2;({
3  "type": "Program",
4  "body": [
5    {
6      "type": "ExpressionStatement",
7      "expression": {
8        "type": "AssignmentExpression",
9        "operator": "=",
10        "left": {
11          "type": "MemberExpression",
12          "computed": false,
13          "object": {
14            "type": "Identifier",
15            "name": "module"
16          },
17          "property": {
18            "type": "Identifier",
19            "name": "exports"
20          }
21        },
22        "right": {
23          "type": "ObjectExpression",
24          "properties": [
25            {
26              "type": "Property",
27              "key": {
28                "type": "Identifier",
29                "name": "data"
30              },
31              "computed": false,
32              "value": {
33                // "type": "FunctionExpression",
34                // "id": null,
35                // "params": [],
36                // "defaults": [],
37                // "body": {
38                  // "type": "BlockStatement",
39                  // "body": [
40                    // {
41                      // "type": "ReturnStatement",
42                      // "argument": {
43                        "type": "ObjectExpression",
44                        "properties": []
45                      // }
46                    // }
47                  // ]
48                // },
49                // "generator": false,
50                // "expression": false
51              },
52              "kind": "init",
53              "method": false,
54              "shorthand": false
55            }
56          ]
57        }
58      }
59    }
60  ],
61  "sourceType": "script"
62})
63
64var LEFT_MODULE_EXPORTS_AST = {
65  "type": "MemberExpression",
66  "computed": false,
67  "object": {
68    "type": "Identifier",
69    "name": "module"
70  },
71  "property": {
72    "type": "Identifier",
73    "name": "exports"
74  }
75}
76
77function removeAllLoc(ast) {
78  ast = JSON.parse(JSON.stringify(ast))
79  function remove(o) {
80    if (Array.isArray(o)) {
81      o.forEach(remove)
82    }
83    else if (typeof o === 'object') {
84      for (var i in o) {
85        if (i === 'loc') {
86          delete o[i]
87        }
88        if (i === 'range') {
89          delete o[i]
90        }
91        else {
92          if (typeof o[i] === 'object') {
93            remove(o[i])
94          }
95        }
96      }
97    }
98  }
99  remove(ast)
100  return ast
101}
102
103function findDataValue(ast) {
104  var exp, left, right, dataValue
105  if (ast && ast.body && ast.body.length) {
106    ast.body.forEach(function (bodyItem) {
107      if (bodyItem.type === 'ExpressionStatement') {
108        exp = bodyItem.expression
109        if (exp.type === 'AssignmentExpression' && exp.operator === '=') {
110          left = removeAllLoc(exp.left || {})
111          if (JSON.stringify(left) === JSON.stringify(LEFT_MODULE_EXPORTS_AST)) {
112            right = exp.right
113            if (right.type === 'ObjectExpression') {
114              right.properties.some(function (prop) {
115                if (prop.type === 'Property' && prop.key && prop.key.name === 'data') {
116                  if (prop.value && prop.value.type === 'ObjectExpression') {
117                    dataValue = prop
118                    return true
119                  }
120                }
121              })
122            }
123          }
124        }
125      }
126    })
127  }
128  return dataValue
129}
130
131function convertValueAst(value) {
132  var data = {
133    type: "FunctionExpression",
134    id: null,
135    params: [],
136    defaults: [],
137    body: {
138      type: "BlockStatement",
139      body: [
140        {
141          type: "ReturnStatement",
142          argument: value
143        }
144      ]
145    },
146    generator: false,
147    expression: false
148  }
149  return data
150}
151function format(code, needCodegen) {
152  return code
153}
154
155function formatBetter(code) {
156  return code
157}
158
159exports.fix = formatBetter
160exports.formatWhenFix = format
161