UNPKG

671 BJavaScriptView Raw
1'use strict';
2
3const THIS_BREAK_KEYS = [ 'FunctionExpression', 'FunctionDeclaration', 'ClassProperty',
4 'ClassMethod', 'ObjectMethod' ];
5
6// Walk the AST looking for 'this' references intended to be references to global
7// Replace them with an explicit 'global' reference
8module.exports = function (_ref) {
9 const t = _ref.types;
10 return {
11 visitor: {
12 ThisExpression(path, state) {
13 if (
14 state.opts.allowTopLevelThis !== true
15 && !path.findParent((path) => !path.is('shadow')
16 && THIS_BREAK_KEYS.indexOf(path.type) >= 0)
17 ) {
18 // TODO: Spit out a warning/deprecation notice?
19 path.replaceWith(t.identifier('global'));
20 }
21 }
22 }
23 };
24};