UNPKG

@autorest/go

Version:
67 lines 2.23 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { values } from '@azure-tools/linq'; import { sortAscending } from './helpers.js'; // tracks packages that need to be imported export class ImportManager { imports; constructor() { this.imports = new Array(); } // adds a package for importing if not already in the list // accepts an optional package alias. add(imp, alias) { for (const existing of values(this.imports)) { if (existing.imp === imp) { return; } } this.imports.push({ imp: imp, alias: alias }); } // returns the number of packages in the list length() { return this.imports.length; } // returns the import list as Go source code 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 sortAscending(a.imp, b.imp); }); let text = 'import (\n'; for (const imp of values(this.imports)) { text += `\t${this.alias(imp)}"${imp.imp}"\n`; } text += ')\n\n'; return text; } addImportForType(type) { switch (type.kind) { case 'map': this.addImportForType(type.valueType); break; case 'slice': this.addImportForType(type.elementType); break; case 'qualifiedType': this.add(type.packageName); break; case 'time': this.add('time'); break; } } alias(entry) { if (entry.alias) { return `${entry.alias} `; } return ''; } } //# sourceMappingURL=imports.js.map