Skip to content

Commit

Permalink
feat!: use type-only imports by default (#168)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Since this package operates exclusively on types, it should be safe to use type-only imports. To restore the previous behavior, set `useTypeImports: false`.

See https://www.typescriptlang.org/docs/handbook/modules/reference.html#type-only-imports-and-exports
  • Loading branch information
mrgrain authored Nov 18, 2023
1 parent abd7e4f commit a07e971
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/renderer/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface TypeScriptRendererOptions {
* Use explicit `type` imports when importing referenced modules.
*
* @see https://www.typescriptlang.org/docs/handbook/modules.html#importing-types
* @default false
* @default true
*/
readonly useTypeImports?: boolean;
}
Expand All @@ -59,7 +59,7 @@ export class TypeScriptRenderer {
this.options = {
importLocations: options.importLocations ?? {},
indent: options.indent ?? 2,
useTypeImports: options.useTypeImports ?? false,
useTypeImports: options.useTypeImports ?? true,
};
this.buffer = new CodeBuffer(' '.repeat(this.options.indent));
}
Expand Down
2 changes: 1 addition & 1 deletion test/projen/__snapshots__/projen-struct.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/projen/__snapshots__/ts-interface.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions test/projen/projen-struct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ test('can import type from the same package at the top level', () => {
const renderedFile = synthSnapshot(project)['src/MyInterface.ts'];

// ASSERT
expect(renderedFile).toContain("import { more, OtherInterface } from './';");
expect(renderedFile).toContain("import type { more, OtherInterface } from './';");
});

test('can import type from the same package when nested', () => {
Expand Down Expand Up @@ -608,7 +608,7 @@ test('can import type from the same package when nested', () => {

// ASSERT
expect(renderedFile).toContain(
"import { more, OtherInterface } from '../../../';",
"import type { more, OtherInterface } from '../../../';",
);
});

Expand Down Expand Up @@ -641,7 +641,7 @@ test("can import type from the same package when in a location that's not matchi
const renderedFile = synthSnapshot(project)['src/sub/MyInterface.ts'];

// ASSERT
expect(renderedFile).toContain("import { more, OtherInterface } from '../';");
expect(renderedFile).toContain("import type { more, OtherInterface } from '../';");
});

test('can import type from an other package', () => {
Expand All @@ -665,7 +665,7 @@ test('can import type from an other package', () => {
const renderedFile = synthSnapshot(project)['src/MyInterface.ts'];

// ASSERT
expect(renderedFile).toContain("import { typescript } from 'projen';");
expect(renderedFile).toContain("import type { typescript } from 'projen';");
});

test('can override import locations', () => {
Expand Down Expand Up @@ -707,8 +707,8 @@ test('can override import locations', () => {
const renderedFile = synthSnapshot(project)['src/MyInterface.ts'];

// ASSERT
expect(renderedFile).toContain("import { more, OtherInterface } from 'bar';");
expect(renderedFile).toContain("import { typescript } from 'banana';");
expect(renderedFile).toContain("import type { more, OtherInterface } from 'bar';");
expect(renderedFile).toContain("import type { typescript } from 'banana';");
});

test('can use struct as type in add', () => {
Expand Down Expand Up @@ -744,7 +744,7 @@ test('can use struct as type in add', () => {
expect(renderedNestedFile).toMatchSnapshot();
expect(renderedNestedFile).toContain('@default .projenRC.ts');
expect(renderedBaseFile).toMatchSnapshot();
expect(renderedBaseFile).toContain("import { MyInterface } from './'");
expect(renderedBaseFile).toContain("import type { MyInterface } from './'");
expect(renderedBaseFile).toContain(
'readonly projenrcTsOptions: MyInterface;',
);
Expand Down Expand Up @@ -782,7 +782,7 @@ test('can use struct as type in update', () => {

// ASSERT
expect(renderedNestedFile).toContain('@default .projenRC.ts');
expect(renderedBaseFile).toContain("import { MyInterface } from './'");
expect(renderedBaseFile).toContain("import type { MyInterface } from './'");
expect(renderedBaseFile).toContain(
'readonly projenrcTsOptions?: MyInterface',
);
Expand Down Expand Up @@ -833,7 +833,7 @@ test('can use an empty struct as type with name', () => {
const renderedFile = synthSnapshot(project)['src/MyInterface.ts'];

// ASSERT
expect(renderedFile).toContain("import { sub } from 'pkg';");
expect(renderedFile).toContain("import type { sub } from 'pkg';");
expect(renderedFile).toContain('readonly nestedProp: sub.MyOtherInterface');
});

Expand Down
6 changes: 3 additions & 3 deletions test/projen/ts-interface.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test('can import from an external package', () => {

// ASSERT
expect(renderedFile).toContain(
"import { javascript, typescript } from 'projen';",
"import type { javascript, typescript } from 'projen';",
);
});

Expand All @@ -69,7 +69,7 @@ test('can import from the same package', () => {
const renderedFile = synthSnapshot(project)['interface.ts'];

// ASSERT
expect(renderedFile).toContain("import { more, OtherInterface } from '../';");
expect(renderedFile).toContain("import type { more, OtherInterface } from '../';");
});

test('can override package imports', () => {
Expand All @@ -90,7 +90,7 @@ test('can override package imports', () => {

// ASSERT
expect(renderedFile).toContain(
"import { javascript, typescript } from 'banana';",
"import type { javascript, typescript } from 'banana';",
);
});

Expand Down

0 comments on commit a07e971

Please sign in to comment.