-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
80 lines (76 loc) · 2.48 KB
/
index.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
/**
* babel-plugin-empower-assert
* Babel plugin to convert assert to power-assert at compile time
*
* https://github.com/power-assert-js/babel-plugin-empower-assert
*
* Copyright (c) 2016-2018 Takuto Wada
* Licensed under the MIT license.
* https://github.com/power-assert-js/babel-plugin-empower-assert/blob/master/LICENSE
*/
'use strict';
var declare = require('@babel/helper-plugin-utils').declare;
module.exports = declare(function (api, options, dirname) {
api.assertVersion(7);
return {
visitor: {
AssignmentExpression: {
enter: function (nodePath, pluginPass) {
if (!nodePath.equals('operator', '=')) {
return;
}
var left = nodePath.get('left');
if (!left.isIdentifier()) {
return;
}
if (!left.equals('name', 'assert')) {
return;
}
replaceAssertIfMatch(nodePath.get('right'));
}
},
VariableDeclarator: {
enter: function (nodePath, pluginPass) {
var id = nodePath.get('id');
if (!id.isIdentifier()) {
return;
}
if (!id.equals('name', 'assert')) {
return;
}
replaceAssertIfMatch(nodePath.get('init'));
}
},
ImportDeclaration: {
enter: function (nodePath, pluginPass) {
var source = nodePath.get('source');
if (!(source.equals('value', 'assert'))) {
return;
}
source.set('value', 'power-assert');
}
}
}
};
});
function replaceAssertIfMatch (node) {
var target;
if (node.isCallExpression()) {
target = node;
} else if (node.isMemberExpression()) {
target = node.get('object');
} else {
return;
}
var callee = target.get('callee');
var arg = target.get('arguments')[0];
if (isRequireAssert(callee, arg)) {
arg.set('value', 'power-assert');
}
}
function isRequireAssert (callee, arg) {
if (!callee.isIdentifier() || !callee.equals('name', 'require')) {
return false;
}
return (arg.isLiteral() && arg.equals('value', 'assert'));
}