Skip to content

Commit

Permalink
Convert global 'this' references to 'global' before transpiling (#25)
Browse files Browse the repository at this point in the history
* When transpiling, run a custom babel plugin which turns global 'this' references into 'global' references.

* Add unit test to verify global this becomes 'global' and not 'undefined'
  • Loading branch information
sgtcoolguy committed Feb 16, 2018
1 parent dc5dde9 commit ac4167a
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 21 deletions.
24 changes: 24 additions & 0 deletions lib/global-this.js
@@ -0,0 +1,24 @@
'use strict';

const THIS_BREAK_KEYS = [ 'FunctionExpression', 'FunctionDeclaration', 'ClassProperty',
'ClassMethod', 'ObjectMethod' ];

// Walk the AST looking for 'this' references intended to be references to global
// Replace them with an explicit 'global' reference
module.exports = function (_ref) {
const t = _ref.types;
return {
visitor: {
ThisExpression(path, state) {
if (
state.opts.allowTopLevelThis !== true
&& !path.findParent((path) => !path.is('shadow')
&& THIS_BREAK_KEYS.indexOf(path.type) >= 0)
) {
// TODO: Spit out a warning/deprecation notice?
path.replaceWith(t.identifier('global'));
}
}
}
};
};
9 changes: 5 additions & 4 deletions lib/jsanalyze.js
Expand Up @@ -76,7 +76,7 @@ function getMemberValue(node) {
function getTitaniumExpression(member) {
var value = getMemberValue(member),
tiNodeRegExp = /^Ti(tanium)?/;
if (value == null) return null;
if (value == null) { return null; }
if (tiNodeRegExp.test(value)) {
// if value.startsWith('Ti.'), replace with 'Titanium.'
if (value.indexOf('Ti.') === 0) {
Expand Down Expand Up @@ -123,7 +123,7 @@ exports.analyzeJs = function analyzeJs(contents, opts) {
contents = contents.split('\n');
if (ex.line && ex.line <= contents.length) {
errmsg.push('');
errmsg.push(' ' + contents[ex.line-1].replace(/\t/g, ' '));
errmsg.push(' ' + contents[ex.line - 1].replace(/\t/g, ' '));
if (ex.col) {
var i = 0,
len = ex.col,
Expand All @@ -142,7 +142,7 @@ exports.analyzeJs = function analyzeJs(contents, opts) {
// find all of the titanium symbols
traverse(ast, {
MemberExpression: {
enter: function(path) {
enter: function (path) {
var memberExpr = getTitaniumExpression(path.node);
if (memberExpr) {
symbols[memberExpr.substring(9)] = 1; // Drop leading 'Titanium.'
Expand Down Expand Up @@ -170,6 +170,7 @@ exports.analyzeJs = function analyzeJs(contents, opts) {

// transpile
if (opts.transpile) {
options.plugins.push(require.resolve('./global-this'));
options.presets.push([ env, { targets: opts.targets } ]);
}

Expand Down Expand Up @@ -246,7 +247,7 @@ exports.analyzeHtml = function analyzeHtml(contents, relPath) {
}

try {
var dom = new DOMParser({ errorHandler: function(){} }).parseFromString('<temp>\n' + contents + '\n</temp>', 'text/html'),
var dom = new DOMParser({ errorHandler: function () {} }).parseFromString('<temp>\n' + contents + '\n</temp>', 'text/html'),
doc = dom && dom.documentElement,
scripts = doc && doc.getElementsByTagName('script'),
i, len, src, m, p, q, r;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -6,7 +6,7 @@
"titanium",
"mobile"
],
"version": "0.4.8",
"version": "0.4.9",
"author": {
"name": "Appcelerator, Inc.",
"email": "info@appcelerator.com"
Expand Down Expand Up @@ -53,6 +53,6 @@
"node": ">=0.6.6"
},
"scripts": {
"test": "JUNIT_REPORT_PATH=junit_report.xml mocha --require tests/init --reporter mocha-jenkins-reporter --check-leaks tests/test-tiappxml.js"
"test": "JUNIT_REPORT_PATH=junit_report.xml mocha --require tests/init --reporter mocha-jenkins-reporter --check-leaks tests/*_test.js"
}
}
19 changes: 10 additions & 9 deletions tests/init.js
@@ -1,9 +1,10 @@
global.should = null;
global.should = require('should');

var util = require('util');
global.dump = function () {
for (var i = 0; i < arguments.length; i++) {
console.error(util.inspect(arguments[i], false, null, true));
}
};
'use strict';
global.should = null;
global.should = require('should');

var util = require('util');
global.dump = function () {
for (var i = 0; i < arguments.length; i++) {
console.error(util.inspect(arguments[i], false, null, true));
}
};
10 changes: 10 additions & 0 deletions tests/jsanalyze_test.js
@@ -0,0 +1,10 @@
'use strict';

const jsanalyze = require('../lib/jsanalyze');

describe('jsanalyze', function () {
it('#analyzeJs() converts global "this" references into "global" references when transpiling', function () {
const results = jsanalyze.analyzeJs('this.myGlobalMethod = function() {};', { transpile: true });
results.contents.should.eql('"use strict";global.myGlobalMethod = function () {};');
});
});
14 changes: 9 additions & 5 deletions tests/run.js
@@ -1,17 +1,21 @@
var spawn = require('child_process').spawn,
exitCode = 0,
'use strict';

const spawn = require('child_process').spawn, // eslint-disable-line security/detect-child-process
tests = [
'test-tiappxml.js'
'jsanalyze_test.js',
'tiappxml_test.js'
];

let exitCode = 0;

(function next() {
if (tests.length === 0) {
process.exit(exitCode);
}

var file = tests.shift();
console.log(file);

var proc = spawn('node', [ 'tests/' + file ]);
proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
Expand Down
File renamed without changes.

0 comments on commit ac4167a

Please sign in to comment.