-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
jsanalyze.js
360 lines (321 loc) · 10.5 KB
/
jsanalyze.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
* @overview
* Analyzes Titanium JavaScript files for symbols and optionally minifies the code.
*
* @module lib/jsanalyze
*
* @copyright
* Copyright (c) 2009-Present by Appcelerator, Inc. All Rights Reserved.
*
* @license
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
'use strict';
const appc = require('node-appc');
const fs = require('fs-extra');
const path = require('path');
const DOMParser = require('xmldom').DOMParser;
const babel = require('@babel/core');
const babylon = require('@babel/parser');
const types = require('@babel/types');
const traverse = require('@babel/traverse').default;
const minify = require('babel-preset-minify');
const env = require('@babel/preset-env');
const __ = appc.i18n(__dirname).__;
let apiUsage = {};
/**
* Returns an object with the Titanium API usage statistics.
*
* @returns {Object} The API usage stats
*/
exports.getAPIUsage = function getAPIUsage() {
return apiUsage;
};
/**
* Analyzes a Titanium JavaScript file for all Titanium API symbols.
*
* @param {String} file - The full path to the JavaScript file
* @param {Object} [opts] - Analyze options
* @param {String} [opts.filename] - The filename of the original JavaScript source
* @param {Boolean} [opts.minify=false] - If true, minifies the JavaScript and returns it
* @returns {Object} An object containing symbols and minified JavaScript
* @throws {Error} An error if unable to parse the JavaScript
*/
exports.analyzeJsFile = function analyzeJsFile(file, opts) {
opts || (opts = {});
opts.filename = file;
return exports.analyzeJs(fs.readFileSync(file).toString(), opts);
};
// Need to look for MemberExpressions, expand them out to full name
function getMemberValue(node) {
if (types.isIdentifier(node)) {
return node.name;
}
if (types.isStringLiteral(node)) {
return node.value;
}
if (!types.isMemberExpression(node)) {
return null;
}
if (node.computed && !types.isStringLiteral(node.property)) {
return null;
}
const objVal = getMemberValue(node.object);
if (objVal === null) {
return null;
}
const propVal = getMemberValue(node.property);
if (propVal === null) {
return null;
}
return objVal + '.' + propVal;
}
function getTitaniumExpression(member) {
const value = getMemberValue(member);
if (value === null) {
return null;
}
const tiNodeRegExp = /^Ti(tanium)?/;
if (tiNodeRegExp.test(value)) {
// if value.startsWith('Ti.'), replace with 'Titanium.'
if (value.indexOf('Ti.') === 0) {
return 'Titanium.' + value.substring(3);
}
return value;
}
return null;
}
/**
* Given an npm module id, this will copy it and it's dependencies to a
* destination "node_modules" folder.
* Note that all of the packages are copied to the top-level of "node_modules",
* not nested!
* Also, to shortcut the logic, if the original package has been copied to the
* destination we will *not* attempt to read it's dependencies and ensure those
* are copied as well! So if the modules version changes or something goes
* haywire and the copies aren't full finished due to a failure, the only way to
* get right is to clean the destination "node_modules" dir before rebuilding.
*
* @param {String} moduleId The npm package/module to copy (along with it's dependencies)
* @param {String} destNodeModulesDir path to the destination "node_mdoules" folder
* @param {Array} [paths=[]] Array of additional paths to pass to require.resolve() (in addition to those from require.resolve.paths(moduleId))
*/
function copyPackageAndDependencies(moduleId, destNodeModulesDir, paths = []) {
const destPackage = path.join(destNodeModulesDir, moduleId);
if (fs.existsSync(destPackage)) {
return; // if the module seems to exist in the destination, just skip it.
}
// copy the dependency's folder over
let pkgJSONPath;
if (require.resolve.paths) {
const thePaths = require.resolve.paths(moduleId);
pkgJSONPath = require.resolve(path.join(moduleId, 'package.json'), { paths: thePaths.concat(paths) });
} else {
pkgJSONPath = require.resolve(path.join(moduleId, 'package.json'));
}
const srcPackage = path.dirname(pkgJSONPath);
const srcPackageNodeModulesDir = path.join(srcPackage, 'node_modules');
fs.copySync(srcPackage, destPackage, {
preserveTimestamps: true,
filter: src => !src.startsWith(srcPackageNodeModulesDir)
});
// Now read it's dependencies and recurse on them
const packageJSON = fs.readJSONSync(pkgJSONPath);
for (const dependency in packageJSON.dependencies) {
copyPackageAndDependencies(dependency, destNodeModulesDir, [ srcPackageNodeModulesDir ]);
}
}
/**
* Analyzes a string containing JavaScript for all Titanium API symbols.
*
* @param {String} contents - A string of JavaScript
* @param {Object} [opts] - Analyze options
* @param {String} [opts.filename] - The filename of the original JavaScript source
* @param {Boolean} [opts.minify=false] - If true, minifies the JavaScript and returns it
* @param {Boolean} [opts.transpile=false] - If true, transpiles the JS code and retuns it
* @param {Array} [opts.plugins=[]] - An array of resolved Babel plugins
* @param {Function} [opts.logger] - Logger instance to use for logging warnings.
* @returns {Object} An object containing symbols and minified JavaScript
* @throws {Error} An error if unable to parse the JavaScript
*/
exports.analyzeJs = function analyzeJs(contents, opts) {
opts || (opts = {});
opts.plugins || (opts.plugins = []);
// parse the js file
let ast;
try {
ast = babylon.parse(contents, { filename: opts.filename, sourceType: 'module' });
} catch (ex) {
var errmsg = [ __('Failed to parse %s', opts.filename) ];
if (ex.line) {
errmsg.push(__('%s [line %s, column %s]', ex.message, ex.line, ex.col));
} else {
errmsg.push(ex.message);
}
try {
contents = contents.split('\n');
if (ex.line && ex.line <= contents.length) {
errmsg.push('');
errmsg.push(' ' + contents[ex.line - 1].replace(/\t/g, ' '));
if (ex.col) {
var i = 0,
len = ex.col,
buffer = ' ';
for (; i < len; i++) {
buffer += '-';
}
errmsg.push(buffer + '^');
}
errmsg.push('');
}
} catch (ex2) {}
throw new Error(errmsg.join('\n'));
}
// find all of the titanium symbols
const symbols = {};
traverse(ast, {
MemberExpression: {
enter: function (path) {
var memberExpr = getTitaniumExpression(path.node);
if (memberExpr) {
symbols[memberExpr.substring(9)] = 1; // Drop leading 'Titanium.'
if (!opts.skipStats) {
if (apiUsage[memberExpr] === undefined) {
apiUsage[memberExpr] = 1;
} else {
apiUsage[memberExpr]++;
}
}
}
}
}
});
const results = {
original: contents,
contents: contents,
symbols: Object.keys(symbols) // convert the object of symbol names to an array of symbol names
};
const options = {
filename: opts.filename,
retainLines: true,
presets: [],
plugins: [
[ require.resolve('./babel-plugins/global-scope'), {
logger: opts.logger
}]
]
};
// transpile
if (opts.transpile) {
options.plugins.push(require.resolve('./babel-plugins/global-this'));
options.plugins.push(require.resolve('@babel/plugin-transform-async-to-generator'));
options.presets.push([ env, { targets: opts.targets, useBuiltIns: 'usage' } ]);
// install polyfill
if (opts.resourcesDir) {
const modulesDir = path.join(opts.resourcesDir, 'node_modules');
// make sure our 'node_modules' directory exists
if (!fs.existsSync(modulesDir)) {
fs.mkdirSync(modulesDir);
}
// copy over polyfill and its dependencies
copyPackageAndDependencies('@babel/polyfill', modulesDir);
}
}
// minify
if (opts.minify) {
Object.assign(options, {
minified: true,
compact: true,
comments: false
});
options.presets.push([ minify, {
mangle: false,
deadcode: false
} ]);
options.plugins.push(require.resolve('@babel/plugin-transform-property-literals'));
}
if (opts.plugins.length) {
options.plugins.push.apply(options.plugins, opts.plugins);
}
// generate and inline source map
// we inline the source map as the map cannot be retreived from the device
// using the inspector protocol. only parsed .js files can be retreived.
if (opts.sourceMap) {
options.sourceMaps = 'inline';
}
// FIXME we can't re-use the ast here, because we traversed it
results.contents = babel.transform(contents, options).code;
return results;
};
/**
* Analyzes an HTML file for all app:// JavaScript files
*
* @param {String} file - The full path to the HTML file
* @param {String} [relPath] - A relative path to the HTML file with respect to the Resources directory
* @returns {Array} An array of app:// JavaScript files
*/
exports.analyzeHtmlFile = function analyzeHtmlFile(file, relPath) {
return exports.analyzeHtml(fs.readFileSync(file).toString(), relPath);
};
/**
* Analyzes a string containing JavaScript for all Titanium API symbols.
*
* @param {String} contents - A string of JavaScript
* @param {String} [relPath] - A relative path to the HTML file with respect to the Resources directory
* @returns {Array} An array of app:// JavaScript files
*/
exports.analyzeHtml = function analyzeHtml(contents, relPath) {
const files = [];
function addFile(src) {
const m = src && src.match(/^(?:(.*):\/\/)?(.+)/);
let res = m && m[2];
if (res) {
if (!m[1]) {
if (relPath && res.indexOf('/') !== 0) {
res = relPath.replace(/\/$/, '') + '/' + res;
}
// compact the path
const p = res.split(/\/|\\/);
const r = [];
let q;
while (q = p.shift()) {
if (q === '..') {
r.pop();
} else {
r.push(q);
}
}
files.push(r.join('/'));
} else if (m[1] === 'app') {
files.push(res);
}
}
}
try {
const dom = new DOMParser({ errorHandler: function () {} }).parseFromString('<temp>\n' + contents + '\n</temp>', 'text/html'),
doc = dom && dom.documentElement,
scripts = doc && doc.getElementsByTagName('script'),
len = scripts.length;
if (scripts) {
for (let i = 0; i < len; i++) {
const src = scripts[i].getAttribute('src');
src && addFile(src);
}
}
} catch (e) {
// bad html file, try to manually parse out the script tags
contents.split('<script').slice(1).forEach(function (chunk) {
const p = chunk.indexOf('>');
if (p !== -1) {
let m = chunk.substring(0, p).match(/src\s*=\s*['"]([^'"]+)/);
if (!m) {
// try again without the quotes
m = chunk.substring(0, p).match(/src\s*=\s*([^>\s]+)/);
}
m && addFile(m[1]);
}
});
}
return files;
};