-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
validators.js
106 lines (92 loc) 路 2.86 KB
/
validators.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import {
DEPRECATED_ALIASES,
DEPRECATED_KEYS,
FLIPPED_ALIAS_KEYS,
NODE_FIELDS,
PLACEHOLDERS,
PLACEHOLDERS_FLIPPED_ALIAS,
VISITOR_KEYS,
} from "../../lib/index.js";
const has = Function.call.bind(Object.prototype.hasOwnProperty);
function joinComparisons(leftArr, right) {
return (
leftArr.map(JSON.stringify).join(` === ${right} || `) + ` === ${right}`
);
}
function addIsHelper(type, aliasKeys, deprecated) {
const targetType = JSON.stringify(type);
let aliasSource = "";
if (aliasKeys) {
aliasSource = joinComparisons(aliasKeys, "nodeType");
}
let placeholderSource = "";
const placeholderTypes = [];
if (PLACEHOLDERS.includes(type) && has(FLIPPED_ALIAS_KEYS, type)) {
placeholderTypes.push(type);
}
if (has(PLACEHOLDERS_FLIPPED_ALIAS, type)) {
placeholderTypes.push(...PLACEHOLDERS_FLIPPED_ALIAS[type]);
}
if (placeholderTypes.length > 0) {
placeholderSource =
' || nodeType === "Placeholder" && (' +
joinComparisons(
placeholderTypes,
"(node as t.Placeholder).expectedNode"
) +
")";
}
const result =
NODE_FIELDS[type] || FLIPPED_ALIAS_KEYS[type]
? `node is t.${type}`
: "boolean";
return `export function is${type}(node: object | null | undefined, opts?: object | null): ${result} {
${deprecated || ""}
if (!node) return false;
const nodeType = (node as t.Node).type;
if (${
aliasSource ? aliasSource : `nodeType === ${targetType}`
}${placeholderSource}) {
if (typeof opts === "undefined") {
return true;
} else {
return shallowEqual(node, opts);
}
}
return false;
}
`;
}
export default function generateValidators() {
let output = `/*
* This file is auto-generated! Do not modify it directly.
* To re-generate run 'make build'
*/
import shallowEqual from "../../utils/shallowEqual";
import type * as t from "../..";\n\n`;
Object.keys(VISITOR_KEYS).forEach(type => {
output += addIsHelper(type);
});
Object.keys(FLIPPED_ALIAS_KEYS)
.filter(
type => !Object.prototype.hasOwnProperty.call(DEPRECATED_ALIASES, type)
)
.forEach(type => {
output += addIsHelper(type, FLIPPED_ALIAS_KEYS[type]);
});
Object.keys(DEPRECATED_KEYS).forEach(type => {
const newType = DEPRECATED_KEYS[type];
const deprecated = `console.trace("\`is${type}\` has been deprecated, please migrate to \`is${newType}\`.");`;
output += addIsHelper(type, null, deprecated);
});
Object.keys(DEPRECATED_ALIASES).forEach(type => {
const newType = DEPRECATED_ALIASES[type];
const deprecated = `console.trace("\`is${type}\` has been deprecated, please migrate to \`is${newType}\`.");`;
output += `export function is${type}(node: object | null | undefined, opts?: object | null): node is t.${newType} {
${deprecated}
return is${newType}(node, opts);
}
`;
});
return output;
}