Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / @typescript-eslint / typescript-estree / dist / convert.js
index 737a98d93b9a364c79cb3bdf0621a0ebaff2c9f6..555b2f62626414369eeb5d012f958c5b5f2d1cc7 100644 (file)
@@ -14,7 +14,7 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
 var __importStar = (this && this.__importStar) || function (mod) {
     if (mod && mod.__esModule) return mod;
     var result = {};
-    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
+    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
     __setModuleDefault(result, mod);
     return result;
 };
@@ -25,6 +25,7 @@ exports.Converter = exports.convertError = void 0;
 const ts = __importStar(require("typescript"));
 const node_utils_1 = require("./node-utils");
 const ts_estree_1 = require("./ts-estree");
+const version_check_1 = require("./version-check");
 const SyntaxKind = ts.SyntaxKind;
 /**
  * Extends and formats a given error object
@@ -185,6 +186,14 @@ class Converter {
         }
         return result;
     }
+    convertBindingNameWithTypeAnnotation(name, tsType, parent) {
+        const id = this.convertPattern(name);
+        if (tsType) {
+            id.typeAnnotation = this.convertTypeAnnotation(tsType, parent);
+            this.fixParentLocation(id, id.typeAnnotation.range);
+        }
+        return id;
+    }
     /**
      * Converts a child into a type annotation. This creates an intermediary
      * TypeAnnotation node to match what Flow does.
@@ -194,8 +203,8 @@ class Converter {
      */
     convertTypeAnnotation(child, parent) {
         // in FunctionType and ConstructorType typeAnnotation has 2 characters `=>` and in other places is just colon
-        const offset = parent.kind === SyntaxKind.FunctionType ||
-            parent.kind === SyntaxKind.ConstructorType
+        const offset = (parent === null || parent === void 0 ? void 0 : parent.kind) === SyntaxKind.FunctionType ||
+            (parent === null || parent === void 0 ? void 0 : parent.kind) === SyntaxKind.ConstructorType
             ? 2
             : 1;
         const annotationStartCol = child.getFullStart() - offset;
@@ -542,7 +551,7 @@ class Converter {
                 return this.createNode(node, {
                     type: ts_estree_1.AST_NODE_TYPES.CatchClause,
                     param: node.variableDeclaration
-                        ? this.convertChild(node.variableDeclaration.name)
+                        ? this.convertBindingNameWithTypeAnnotation(node.variableDeclaration.name, node.variableDeclaration.type)
                         : null,
                     body: this.convertChild(node.block),
                 });
@@ -626,16 +635,12 @@ class Converter {
             case SyntaxKind.VariableDeclaration: {
                 const result = this.createNode(node, {
                     type: ts_estree_1.AST_NODE_TYPES.VariableDeclarator,
-                    id: this.convertPattern(node.name),
+                    id: this.convertBindingNameWithTypeAnnotation(node.name, node.type, node),
                     init: this.convertChild(node.initializer),
                 });
                 if (node.exclamationToken) {
                     result.definite = true;
                 }
-                if (node.type) {
-                    result.id.typeAnnotation = this.convertTypeAnnotation(node.type, node);
-                    this.fixParentLocation(result.id, result.id.typeAnnotation.range);
-                }
                 return result;
             }
             case SyntaxKind.VariableStatement: {
@@ -1240,12 +1245,14 @@ class Converter {
                     local: this.convertChild(node.name),
                     imported: this.convertChild((_f = node.propertyName) !== null && _f !== void 0 ? _f : node.name),
                 });
-            case SyntaxKind.ImportClause:
+            case SyntaxKind.ImportClause: {
+                const local = this.convertChild(node.name);
                 return this.createNode(node, {
                     type: ts_estree_1.AST_NODE_TYPES.ImportDefaultSpecifier,
-                    local: this.convertChild(node.name),
-                    range: [node.getStart(this.ast), node.name.end],
+                    local,
+                    range: local.range,
                 });
+            }
             case SyntaxKind.ExportDeclaration:
                 if (((_g = node.exportClause) === null || _g === void 0 ? void 0 : _g.kind) === SyntaxKind.NamedExports) {
                     return this.createNode(node, {
@@ -1582,18 +1589,17 @@ class Converter {
                     raw: 'false',
                 });
             case SyntaxKind.NullKeyword: {
-                if (this.inTypeMode) {
+                if (!version_check_1.typescriptVersionIsAtLeast['4.0'] && this.inTypeMode) {
+                    // 4.0 started nesting null types inside a LiteralType node, but we still need to support pre-4.0
                     return this.createNode(node, {
                         type: ts_estree_1.AST_NODE_TYPES.TSNullKeyword,
                     });
                 }
-                else {
-                    return this.createNode(node, {
-                        type: ts_estree_1.AST_NODE_TYPES.Literal,
-                        value: null,
-                        raw: 'null',
-                    });
-                }
+                return this.createNode(node, {
+                    type: ts_estree_1.AST_NODE_TYPES.Literal,
+                    value: null,
+                    raw: 'null',
+                });
             }
             case SyntaxKind.EmptyStatement:
                 return this.createNode(node, {
@@ -2100,30 +2106,12 @@ class Converter {
                 return this.fixExports(node, result);
             }
             // TypeScript specific types
-            case SyntaxKind.OptionalType: {
-                return this.createNode(node, {
-                    type: ts_estree_1.AST_NODE_TYPES.TSOptionalType,
-                    typeAnnotation: this.convertType(node.type),
-                });
-            }
             case SyntaxKind.ParenthesizedType: {
                 return this.createNode(node, {
                     type: ts_estree_1.AST_NODE_TYPES.TSParenthesizedType,
                     typeAnnotation: this.convertType(node.type),
                 });
             }
-            case SyntaxKind.TupleType: {
-                // In TS 4.0, the `elementTypes` property was changed to `elements`.
-                // To support both at compile time, we cast to access the newer version
-                // if the former does not exist.
-                const elementTypes = node.elementTypes
-                    ? node.elementTypes.map(el => this.convertType(el))
-                    : node.elements.map((el) => this.convertType(el));
-                return this.createNode(node, {
-                    type: ts_estree_1.AST_NODE_TYPES.TSTupleType,
-                    elementTypes,
-                });
-            }
             case SyntaxKind.UnionType: {
                 return this.createNode(node, {
                     type: ts_estree_1.AST_NODE_TYPES.TSUnionType,
@@ -2136,12 +2124,6 @@ class Converter {
                     types: node.types.map(el => this.convertType(el)),
                 });
             }
-            case SyntaxKind.RestType: {
-                return this.createNode(node, {
-                    type: ts_estree_1.AST_NODE_TYPES.TSRestType,
-                    typeAnnotation: this.convertType(node.type),
-                });
-            }
             case SyntaxKind.AsExpression: {
                 return this.createNode(node, {
                     type: ts_estree_1.AST_NODE_TYPES.TSAsExpression,
@@ -2156,10 +2138,20 @@ class Converter {
                 });
             }
             case SyntaxKind.LiteralType: {
-                return this.createNode(node, {
-                    type: ts_estree_1.AST_NODE_TYPES.TSLiteralType,
-                    literal: this.convertType(node.literal),
-                });
+                if (version_check_1.typescriptVersionIsAtLeast['4.0'] &&
+                    node.literal.kind === SyntaxKind.NullKeyword) {
+                    // 4.0 started nesting null types inside a LiteralType node
+                    // but our AST is designed around the old way of null being a keyword
+                    return this.createNode(node.literal, {
+                        type: ts_estree_1.AST_NODE_TYPES.TSNullKeyword,
+                    });
+                }
+                else {
+                    return this.createNode(node, {
+                        type: ts_estree_1.AST_NODE_TYPES.TSLiteralType,
+                        literal: this.convertType(node.literal),
+                    });
+                }
             }
             case SyntaxKind.TypeAssertionExpression: {
                 return this.createNode(node, {
@@ -2193,6 +2185,49 @@ class Converter {
                     type: ts_estree_1.AST_NODE_TYPES.TSAbstractKeyword,
                 });
             }
+            // Tuple
+            case SyntaxKind.TupleType: {
+                // In TS 4.0, the `elementTypes` property was changed to `elements`.
+                // To support both at compile time, we cast to access the newer version
+                // if the former does not exist.
+                const elementTypes = 'elementTypes' in node
+                    ? node.elementTypes.map((el) => this.convertType(el))
+                    : node.elements.map((el) => this.convertType(el));
+                return this.createNode(node, {
+                    type: ts_estree_1.AST_NODE_TYPES.TSTupleType,
+                    elementTypes,
+                });
+            }
+            case SyntaxKind.NamedTupleMember: {
+                const member = this.createNode(node, {
+                    type: ts_estree_1.AST_NODE_TYPES.TSNamedTupleMember,
+                    elementType: this.convertType(node.type, node),
+                    label: this.convertChild(node.name, node),
+                    optional: node.questionToken != null,
+                });
+                if (node.dotDotDotToken) {
+                    // adjust the start to account for the "..."
+                    member.range[0] = member.label.range[0];
+                    member.loc.start = member.label.loc.start;
+                    return this.createNode(node, {
+                        type: ts_estree_1.AST_NODE_TYPES.TSRestType,
+                        typeAnnotation: member,
+                    });
+                }
+                return member;
+            }
+            case SyntaxKind.OptionalType: {
+                return this.createNode(node, {
+                    type: ts_estree_1.AST_NODE_TYPES.TSOptionalType,
+                    typeAnnotation: this.convertType(node.type),
+                });
+            }
+            case SyntaxKind.RestType: {
+                return this.createNode(node, {
+                    type: ts_estree_1.AST_NODE_TYPES.TSRestType,
+                    typeAnnotation: this.convertType(node.type),
+                });
+            }
             default:
                 return this.deeplyCopy(node);
         }