TSNonNullExpression.js
3.02 KB
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
const extractValueFromThisExpression = require('./ThisExpression').default;
function navigate(obj, prop, value) {
if (value.computed) {
return value.optional ? `${obj}?.[${prop}]` : `${obj}[${prop}]`;
}
return value.optional ? `${obj}?.${prop}` : `${obj}.${prop}`;
}
/**
* Extractor function for a TSNonNullExpression type value node.
* A TSNonNullExpression is accessing a TypeScript Non-Null Assertion
* Operator !
*
* @param - value - AST Value object with type `TSNonNullExpression`
* @returns - The extracted value converted to correct type
* and maintaing `obj.property` convention.
*/
export default function extractValueFromTSNonNullExpression(value) {
// eslint-disable-next-line global-require
// const getValue = require('.').default;
const errorMessage = 'The prop value with an expression type of TSNonNullExpression could not be resolved. Please file an issue ( https://github.com/jsx-eslint/jsx-ast-utils/issues/new ) to get this fixed immediately.';
// it's just the name
if (value.type === 'Identifier') {
const { name } = value;
return name;
}
if (value.type === 'Literal') {
return value.value;
}
if (value.type === 'TSAsExpression') {
return extractValueFromTSNonNullExpression(value.expression);
}
if (value.type === 'ThisExpression') {
return extractValueFromThisExpression();
}
// does not contains properties & is not parenthesized
if (value.type === 'TSNonNullExpression' && (!value.extra || value.extra.parenthesized === false)) {
const { expression } = value;
return `${extractValueFromTSNonNullExpression(expression)}${'!'}`;
}
// does not contains properties & is parenthesized
if (value.type === 'TSNonNullExpression' && value.extra && value.extra.parenthesized === true) {
const { expression } = value;
return `${'('}${extractValueFromTSNonNullExpression(expression)}${'!'}${')'}`;
}
if (value.type === 'MemberExpression') {
// contains a property & is not parenthesized
if ((!value.extra || value.extra.parenthesized === false)) {
return navigate(
extractValueFromTSNonNullExpression(value.object),
extractValueFromTSNonNullExpression(value.property),
value,
);
}
// contains a property & is parenthesized
if (value.extra && value.extra.parenthesized === true) {
const result = navigate(
extractValueFromTSNonNullExpression(value.object),
extractValueFromTSNonNullExpression(value.property),
value,
);
return `(${result})`;
}
}
// try to fail silently, if specs for TSNonNullExpression change
// not throw, only log error. Similar to how it was done previously
if (value.expression) {
let { expression } = value;
while (expression) {
if (expression.type === 'Identifier') {
// eslint-disable-next-line no-console
console.error(errorMessage);
return expression.name;
}
({ expression } = expression);
}
}
// eslint-disable-next-line no-console
console.error(errorMessage);
return '';
}