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