-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert global 'this' references to 'global' before transpiling (#25)
* 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
1 parent
dc5dde9
commit ac4167a
Showing
8 changed files
with
61 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); | ||
} | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
"titanium", | ||
"mobile" | ||
], | ||
"version": "0.4.8", | ||
"version": "0.4.9", | ||
"author": { | ||
"name": "Appcelerator, Inc.", | ||
"email": "[email protected]" | ||
|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 () {};'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.