Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add importMappings ToStringOpts #28

Merged
merged 1 commit into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface ToStringOpts {
prefix?: string;
/** Optional per-file overrides for the prettier config, i.e. to use longer-than-normal line lengths. */
prettierOverrides?: Options;
/** optional importMappings */
importMappings?: { [key: string]: string };
}

export class Code extends Node {
Expand All @@ -40,7 +42,7 @@ export class Code extends Node {
* to return a `Promise<String>`.
*/
toStringWithImports(opts?: ToStringOpts): Promise<string> {
const { path = '', forceDefaultImport, forceModuleImport, prefix, prettierOverrides = {} } = opts || {};
const { path = '', forceDefaultImport, forceModuleImport, prefix, prettierOverrides = {}, importMappings = {} } = opts || {};
const ourModulePath = path.replace(/\.[tj]sx?/, '');
if (forceDefaultImport || forceModuleImport) {
this.deepReplaceNamedImports(forceDefaultImport || [], forceModuleImport || []);
Expand All @@ -49,7 +51,7 @@ export class Code extends Node {
const imports = this.deepFindImports();
const defs = this.deepFindDefs();
assignAliasesIfNeeded(defs, imports, ourModulePath);
const importPart = emitImports(imports, ourModulePath);
const importPart = emitImports(imports, ourModulePath, importMappings);
const bodyPart = this.generateCode();
const maybePrefix = prefix ? `${prefix}\n` : '';
return maybePrettyWithConfig(maybePrefix + importPart + '\n' + bodyPart, prettierOverrides);
Expand Down
18 changes: 16 additions & 2 deletions src/Import-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ describe('Import', () => {
expect(maybeRelativePath('zaz/Zaz', './foo/Foo')).toEqual('../foo/Foo');
});

it('can interpret importMappings', () => {
const parsed = Import.from('Empty@./google/protobuf/empty');
expect(parsed).toBeInstanceOf(ImportsName);

const sym = parsed as ImportsName;
expect(sym.symbol).toEqual('Empty');
expect(sym.source).toEqual('./google/protobuf/empty');

const importMappings = { './google/protobuf/empty': './external/protoapis/google/protobuf/empty' }
expect(emit(sym, importMappings)).toMatchInlineSnapshot(
`"import { Empty } from './external/protoapis/google/protobuf/empty';"`
);
});

it('can handle relative imports with a current directory', () => {
expect(maybeRelativePath('./zaz/Zaz', './foo/Foo')).toEqual('../foo/Foo');
});
Expand Down Expand Up @@ -174,7 +188,7 @@ describe('Import', () => {
);
});

function emit(spec: Import): string {
return emitImports([spec], '').trim();
function emit(spec: Import, importMappings = {}): string {
return emitImports([spec], '', importMappings).trim();
}
});
5 changes: 4 additions & 1 deletion src/Import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export class SideEffect extends Imported {
}

/** Generates the `import ...` lines for the given `imports`. */
export function emitImports(imports: Import[], ourModulePath: string): string {
export function emitImports(imports: Import[], ourModulePath: string, importMappings: { [key: string]: string }): string {
if (imports.length == 0) {
return '';
}
Expand All @@ -312,6 +312,9 @@ export function emitImports(imports: Import[], ourModulePath: string): string {
if (sameModule(ourModulePath, modulePath)) {
return;
}
if (modulePath in importMappings) {
modulePath = importMappings[modulePath];
}
const importPath = maybeRelativePath(ourModulePath, modulePath);

// Output star imports individually
Expand Down