/
helpers.js
55 lines (49 loc) · 1.52 KB
/
helpers.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
"use strict";
const VOID_0 = t => t.unaryExpression("void", t.numericLiteral(0), true);
const evaluate = require("babel-helper-evaluate-path");
// Types as Symbols - for comparing types
const types = {};
// This is a test key which is used to avoid Object.keys check
// Object.keys() check is really expensive
// https://gist.github.com/vigneshshanmugam/c766550ecd02292dcdfbf0bf013b9d3d
const testKey = "Expression";
const typeSymbols = t => {
// don't recompute
if (types[testKey] !== undefined) {
return types;
}
t.TYPES.forEach(type => {
types[type] = Symbol.for(type);
});
return types;
};
const isNodeOfType = (t, node, typeSymbol) =>
typeof typeSymbol !== "symbol"
? false
: t["is" + Symbol.keyFor(typeSymbol)](node);
const isPatternMatchesPath = t =>
function _isPatternMatchesPath(patternValue, inputPath) {
if (Array.isArray(patternValue)) {
for (let i = 0; i < patternValue.length; i++) {
if (_isPatternMatchesPath(patternValue[i], inputPath)) {
return true;
}
}
return false;
}
if (typeof patternValue === "function") {
return patternValue(inputPath);
}
if (isNodeOfType(t, inputPath.node, patternValue)) return true;
const evalResult = evaluate(inputPath);
if (!evalResult.confident || !inputPath.isPure()) return false;
return evalResult.value === patternValue;
};
module.exports = {
VOID_0,
// Types as Symbols
typeSymbols,
// This is required for resolving type aliases
isNodeOfType,
isPatternMatchesPath
};