1 | 'use strict';
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = extractValueFromTSNonNullExpression;
|
---|
7 | var extractValueFromThisExpression = require('./ThisExpression').default;
|
---|
8 | var extractValueFromCallExpression = require('./CallExpression').default;
|
---|
9 |
|
---|
10 | function navigate(obj, prop, value) {
|
---|
11 | if (value.computed) {
|
---|
12 | return value.optional ? obj + '?.[' + prop + ']' : obj + '[' + prop + ']';
|
---|
13 | }
|
---|
14 | return value.optional ? obj + '?.' + prop : obj + '.' + prop;
|
---|
15 | }
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Extractor function for a TSNonNullExpression type value node.
|
---|
19 | * A TSNonNullExpression is accessing a TypeScript Non-Null Assertion
|
---|
20 | * Operator !
|
---|
21 | *
|
---|
22 | * @param - value - AST Value object with type `TSNonNullExpression`
|
---|
23 | * @returns - The extracted value converted to correct type
|
---|
24 | * and maintaing `obj.property` convention.
|
---|
25 | */
|
---|
26 | function extractValueFromTSNonNullExpression(value) {
|
---|
27 | // eslint-disable-next-line global-require
|
---|
28 | // const getValue = require('.').default;
|
---|
29 | var 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.';
|
---|
30 |
|
---|
31 | // it's just the name
|
---|
32 | if (value.type === 'Identifier') {
|
---|
33 | var name = value.name;
|
---|
34 |
|
---|
35 | return name;
|
---|
36 | }
|
---|
37 |
|
---|
38 | if (value.type === 'Literal') {
|
---|
39 | return value.value;
|
---|
40 | }
|
---|
41 |
|
---|
42 | if (value.type === 'TSAsExpression') {
|
---|
43 | return extractValueFromTSNonNullExpression(value.expression);
|
---|
44 | }
|
---|
45 |
|
---|
46 | if (value.type === 'CallExpression') {
|
---|
47 | return extractValueFromCallExpression(value);
|
---|
48 | }
|
---|
49 |
|
---|
50 | if (value.type === 'ThisExpression') {
|
---|
51 | return extractValueFromThisExpression();
|
---|
52 | }
|
---|
53 |
|
---|
54 | // does not contains properties & is not parenthesized
|
---|
55 | if (value.type === 'TSNonNullExpression' && (!value.extra || value.extra.parenthesized === false)) {
|
---|
56 | var expression = value.expression;
|
---|
57 |
|
---|
58 | return extractValueFromTSNonNullExpression(expression) + '!';
|
---|
59 | }
|
---|
60 |
|
---|
61 | // does not contains properties & is parenthesized
|
---|
62 | if (value.type === 'TSNonNullExpression' && value.extra && value.extra.parenthesized === true) {
|
---|
63 | var _expression = value.expression;
|
---|
64 |
|
---|
65 | return '(' + extractValueFromTSNonNullExpression(_expression) + '!' + ')';
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (value.type === 'MemberExpression') {
|
---|
69 | // contains a property & is not parenthesized
|
---|
70 | if (!value.extra || value.extra.parenthesized === false) {
|
---|
71 | return navigate(extractValueFromTSNonNullExpression(value.object), extractValueFromTSNonNullExpression(value.property), value);
|
---|
72 | }
|
---|
73 |
|
---|
74 | // contains a property & is parenthesized
|
---|
75 | if (value.extra && value.extra.parenthesized === true) {
|
---|
76 | var result = navigate(extractValueFromTSNonNullExpression(value.object), extractValueFromTSNonNullExpression(value.property), value);
|
---|
77 | return '(' + result + ')';
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | // try to fail silently, if specs for TSNonNullExpression change
|
---|
82 | // not throw, only log error. Similar to how it was done previously
|
---|
83 | if (value.expression) {
|
---|
84 | var _expression2 = value.expression;
|
---|
85 |
|
---|
86 | while (_expression2) {
|
---|
87 | if (_expression2.type === 'Identifier') {
|
---|
88 | // eslint-disable-next-line no-console
|
---|
89 | console.error(errorMessage);
|
---|
90 | return _expression2.name;
|
---|
91 | }
|
---|
92 | var _expression3 = _expression2;
|
---|
93 | _expression2 = _expression3.expression;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | // eslint-disable-next-line no-console
|
---|
98 | console.error(errorMessage);
|
---|
99 | return '';
|
---|
100 | } |
---|