UNPKG

@autorest/go

Version:
142 lines 4.28 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import * as go from '../../../codemodel.go/src/index.js'; import * as helpers from './helpers.js'; // tracks packages that need to be imported export class ImportManager { imports; pkg; /** * creates a new instance of ImportManager for the specified package * * @param pkg the package that contains the import statements to emit */ constructor(pkg) { this.imports = new Array(); this.pkg = pkg; } /** * adds a package for importing if not already in the list * accepts an optional package alias. * * @param imp the package name to import * @param alias optional alias for the import */ add(imp, alias) { for (const existing of this.imports) { if (existing.imp === imp) { return; } } this.imports.push({ imp: imp, alias: alias }); } /** * adds the specified package for importing if not already in the list. * * @param pkg the package to import * @param alias optional package alias */ addForPkg(pkg, alias) { let pkgPath; switch (pkg.kind) { case 'module': pkgPath = pkg.identity; break; case 'package': { pkgPath = buildImportPath(pkg); break; } } this.add(pkgPath, alias); } /** * returns the number of packages in the list * * @returns the import count */ length() { return this.imports.length; } /** * returns the import list as Go source code * * @returns the text for the import statement */ text() { if (this.imports.length === 0) { return ''; } else if (this.imports.length === 1) { const first = this.imports[0]; return `import ${this.alias(first)}"${first.imp}"\n\n`; } this.imports.sort((a, b) => { return helpers.sortAscending(a.imp, b.imp); }); let text = 'import (\n'; for (const imp of this.imports) { text += `\t${this.alias(imp)}"${imp.imp}"\n`; } text += ')\n\n'; return text; } /** * adds an import statement for the specified type * as required if not already in the list. * * @param type the type for which to add the import */ addForType(type) { switch (type.kind) { case 'map': this.addForType(type.valueType); break; case 'slice': this.addForType(type.elementType); break; case 'client': case 'constant': case 'interface': case 'model': case 'polymorphicModel': if (go.getPackageName(type.pkg) !== go.getPackageName(this.pkg)) { this.add(buildImportPath(type.pkg)); } } // generic fallback for qualified types if (type.name !== undefined && type.module !== undefined) { this.add(type.module); } } /** * returns the import alias or the empty string * * @param entry the entry to check for an alias * @returns the import alias or the empty string */ alias(entry) { if (entry.alias) { return `${entry.alias} `; } return ''; } } /** * builds the complete package import path for the provided package * * @param pkg the package for which to build the import path * @returns the fully qualified package path */ function buildImportPath(pkg) { const pkgs = new Array(); let cur = pkg; while (cur.kind === 'package') { pkgs.unshift(cur.name); cur = cur.parent; } pkgs.unshift(cur.identity); return pkgs.join('/'); } //# sourceMappingURL=imports.js.map