Skip to content

Commit

Permalink
WIP: [TIMOB-26403] Add plugin to convert all top level declarations t…
Browse files Browse the repository at this point in the history
…o global scope (#47)

* [TIMOB-26403] Add plugin to convert all top level declarations to global scope

* [TIMOB-26403] Use cli logger instance only if provided

* 0.6.4

* Fix assertion in adb tests
  • Loading branch information
ewanharris authored and sgtcoolguy committed Oct 2, 2018
1 parent 2d746a8 commit 00b5334
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 20 deletions.
38 changes: 38 additions & 0 deletions lib/babel-plugins/global-scope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';
const _path = require('path');
/**
* Convert all top level variable declarations in "app.js" into explictly
* defined variables on the global scope.
*/
module.exports = function (babel) {
const t = babel.types;
let didEdits = false;
return {
name: 'app.js top level variables global transform',
visitor: {
Program: function (path, state) {
const logger = state.opts.logger;
if (_path.basename(this.file.opts.filename) !== 'app.js') {
return;
}
for (const bodyPath of path.get('body')) {
for (const name in t.getBindingIdentifiers(bodyPath.node, false, true)) {
didEdits = true;
bodyPath.insertAfter(
t.expressionStatement(
t.assignmentExpression(
'=',
t.identifier(`global.${name} `),
t.identifier(`${name}`)
)
)
);
}
}
if (didEdits) {
logger && logger.warn('The implicit global scope for variable declarations in app.js is deprecated in 7.5.0, and will be removed in 9.0.0');
}
}
}
};
};
File renamed without changes.
26 changes: 15 additions & 11 deletions lib/jsanalyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ function getTitaniumExpression(member) {
* @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
*/
Expand Down Expand Up @@ -177,12 +178,16 @@ exports.analyzeJs = function analyzeJs(contents, opts) {
filename: opts.filename,
retainLines: true,
presets: [],
plugins: []
plugins: [
[ require.resolve('./babel-plugins/global-scope'), {
logger: opts.logger
}]
]
};

// transpile
if (opts.transpile) {
options.plugins.push(require.resolve('./global-this'));
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: true } ]);

Expand Down Expand Up @@ -229,17 +234,16 @@ exports.analyzeJs = function analyzeJs(contents, opts) {
options.plugins.push.apply(options.plugins, opts.plugins);
}

if (options.presets.length || options.plugins.length) {
// 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;
// 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;
};

Expand Down
47 changes: 43 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"titanium",
"mobile"
],
"version": "0.6.3",
"version": "0.6.4",
"author": {
"name": "Appcelerator, Inc.",
"email": "info@appcelerator.com"
Expand Down Expand Up @@ -45,6 +45,7 @@
"xmldom": "0.1.27"
},
"devDependencies": {
"babel-plugin-tester": "^5.5.1",
"eslint-plugin-mocha": "^5.0.0",
"grunt": "^1.0.3",
"grunt-appc-js": "^2.1.0",
Expand All @@ -53,6 +54,7 @@
"istanbul": "^0.4.5",
"mocha": "^5.2.0",
"mocha-jenkins-reporter": "0.3.7",
"semver": "^5.5.1",
"should": "^13.2.1"
},
"repository": {
Expand Down
7 changes: 3 additions & 4 deletions tests/adb_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ function MockConfig() {

const fs = require('fs');
const path = require('path');

const semver = require('semver');
const should = require('should'); // eslint-disable-line no-unused-vars

const android = require('../lib/android');
android.androidPackageJson({
vendorDependencies: {
Expand All @@ -34,12 +33,12 @@ const emulator = new Emulator(config);

describe('adb', function () {

it('#version() returns 1.0.39', function (finished) {
it('#version() returns a valid semver string', function (finished) {
adb.version(function (err, ver) {
if (err) {
return finished(err);
}
ver.should.eql('1.0.39'); // FIXME: Occasionally this will return '1.0.NaN'!
should(semver.valid(ver)).not.be.null();
finished();
});
});
Expand Down
23 changes: 23 additions & 0 deletions tests/global-plugin_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const path = require('path');
const pluginTester = require('babel-plugin-tester');
const plugin = require('../lib/babel-plugins/global-scope');

pluginTester({
plugin,
tests: {
'exposes declerations as global variables': {
babelOptions: {
filename: 'app.js'
},
fixture: path.join(__dirname, 'resources', 'global-plugin', 'app.js'),
outputFixture: path.join(__dirname, 'resources', 'global-plugin', 'output.js'),
},
'should only operate on app.js': {
babelOptions: {
filename: 'another-file.js'
},
fixture: path.join(__dirname, 'resources', 'global-plugin', 'app.js'),
outputFixture: path.join(__dirname, 'resources', 'global-plugin', 'app.js'),
}
}
});
16 changes: 16 additions & 0 deletions tests/resources/global-plugin/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function foobar() {
var x = 'bar';
}
global.aGlobalVar = 'foobar';
const foo = () => {};
let win = Ti.UI.createWindow({
backgroundColor: 'white'
});
var textField = Ti.UI.createTextField({
borderStyle: Ti.UI.INPUT_BORDERSTYLE_BEZEL,
color: '#336699',
top: 10, left: 10,
width: 250, height: 60
});
win.add(textField);
win.open();
20 changes: 20 additions & 0 deletions tests/resources/global-plugin/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function foobar() {
var x = 'bar';
}
global.foobar = foobar;
global.aGlobalVar = 'foobar';
const foo = () => {};
global.foo = foo;
let win = Ti.UI.createWindow({
backgroundColor: 'white'
});
global.win = win;
var textField = Ti.UI.createTextField({
borderStyle: Ti.UI.INPUT_BORDERSTYLE_BEZEL,
color: '#336699',
top: 10, left: 10,
width: 250, height: 60
});
global.textField = textField;
win.add(textField);
win.open();

0 comments on commit 00b5334

Please sign in to comment.