UNPKG

3.12 kBJavaScriptView Raw
1"use strict";
2var _a;
3Object.defineProperty(exports, "__esModule", { value: true });
4exports.AbstractFileVisitor = void 0;
5const ts = require("typescript");
6const plugin_constants_1 = require("../plugin-constants");
7const [major, minor] = (_a = ts.versionMajorMinor) === null || _a === void 0 ? void 0 : _a.split('.').map((x) => +x);
8class AbstractFileVisitor {
9 updateImports(sourceFile, factory, program) {
10 if (!factory) {
11 const importEqualsDeclaration = major == 4 && minor >= 2
12 ? ts.createImportEqualsDeclaration(undefined, undefined, false, plugin_constants_1.OPENAPI_NAMESPACE, ts.createExternalModuleReference(ts.createLiteral(plugin_constants_1.OPENAPI_PACKAGE_NAME)))
13 : ts.createImportEqualsDeclaration(undefined, undefined, plugin_constants_1.OPENAPI_NAMESPACE, ts.createExternalModuleReference(ts.createLiteral(plugin_constants_1.OPENAPI_PACKAGE_NAME)));
14 return ts.updateSourceFileNode(sourceFile, [
15 importEqualsDeclaration,
16 ...sourceFile.statements
17 ]);
18 }
19 const importEqualsDeclaration = major >= 4 && minor >= 2
20 ? minor >= 8
21 ? factory.createImportEqualsDeclaration(undefined, false, factory.createIdentifier(plugin_constants_1.OPENAPI_NAMESPACE), factory.createExternalModuleReference(factory.createStringLiteral(plugin_constants_1.OPENAPI_PACKAGE_NAME)))
22 : factory.createImportEqualsDeclaration(undefined, undefined, false, plugin_constants_1.OPENAPI_NAMESPACE, factory.createExternalModuleReference(factory.createStringLiteral(plugin_constants_1.OPENAPI_PACKAGE_NAME)))
23 : factory.createImportEqualsDeclaration(undefined, undefined, plugin_constants_1.OPENAPI_NAMESPACE, factory.createExternalModuleReference(factory.createStringLiteral(plugin_constants_1.OPENAPI_PACKAGE_NAME)));
24 const compilerOptions = program.getCompilerOptions();
25 if (compilerOptions.module >= ts.ModuleKind.ES2015 &&
26 compilerOptions.module <= ts.ModuleKind.ESNext) {
27 const importAsDeclaration = (minor >= 8 && major >= 4) || major >= 5
28 ? factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamespaceImport(factory.createIdentifier(plugin_constants_1.OPENAPI_NAMESPACE))), factory.createStringLiteral(plugin_constants_1.OPENAPI_PACKAGE_NAME), undefined)
29 : factory.createImportDeclaration(undefined, undefined, factory.createImportClause(false, undefined, factory.createNamespaceImport(factory.createIdentifier(plugin_constants_1.OPENAPI_NAMESPACE))), factory.createStringLiteral(plugin_constants_1.OPENAPI_PACKAGE_NAME), undefined);
30 return factory.updateSourceFile(sourceFile, [
31 importAsDeclaration,
32 ...sourceFile.statements
33 ]);
34 }
35 else {
36 return factory.updateSourceFile(sourceFile, [
37 importEqualsDeclaration,
38 ...sourceFile.statements
39 ]);
40 }
41 }
42}
43exports.AbstractFileVisitor = AbstractFileVisitor;