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

Proposal: Always allow type-only imports to reference .ts extensions #54746

Merged
merged 3 commits into from
Jul 24, 2023
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
9 changes: 7 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4905,8 +4905,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
else if (resolvedModule.resolvedUsingTsExtension && !shouldAllowImportingTsExtension(compilerOptions, currentSourceFile.fileName)) {
const tsExtension = Debug.checkDefined(tryExtractTSExtension(moduleReference));
error(errorNode, Diagnostics.An_import_path_can_only_end_with_a_0_extension_when_allowImportingTsExtensions_is_enabled, tsExtension);
const importOrExport =
findAncestor(location, isImportDeclaration)?.importClause ||
findAncestor(location, or(isImportEqualsDeclaration, isExportDeclaration));
if (!(importOrExport?.isTypeOnly || findAncestor(location, isImportTypeNode))) {
const tsExtension = Debug.checkDefined(tryExtractTSExtension(moduleReference));
error(errorNode, Diagnostics.An_import_path_can_only_end_with_a_0_extension_when_allowImportingTsExtensions_is_enabled, tsExtension);
}
}

if (sourceFile.symbol) {
Expand Down
13 changes: 13 additions & 0 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
arrayFrom,
CancellationToken,
cast,
changeAnyExtension,
CodeAction,
CodeFixAction,
CodeFixContextBase,
Expand Down Expand Up @@ -42,10 +43,13 @@ import {
getExportInfoMap,
getMeaningFromDeclaration,
getMeaningFromLocation,
getModeForUsageLocation,
getNameForExportedSymbol,
getNodeId,
getOutputExtension,
getQuoteFromPreference,
getQuotePreference,
getResolvedModule,
getSourceFileOfNode,
getSymbolId,
getTokenAtPosition,
Expand Down Expand Up @@ -1344,6 +1348,15 @@ function promoteFromTypeOnly(changes: textChanges.ChangeTracker, aliasDeclaratio

function promoteImportClause(importClause: ImportClause) {
changes.delete(sourceFile, getTypeKeywordOfTypeOnlyImport(importClause, sourceFile));
// Change .ts extension to .js if necessary
if (!compilerOptions.allowImportingTsExtensions) {
const moduleSpecifier = tryGetModuleSpecifierFromDeclaration(importClause.parent);
const resolvedModule = moduleSpecifier && getResolvedModule(sourceFile, moduleSpecifier.text, getModeForUsageLocation(sourceFile, moduleSpecifier));
if (resolvedModule?.resolvedUsingTsExtension) {
const changedExtension = changeAnyExtension(moduleSpecifier!.text, getOutputExtension(moduleSpecifier!.text, compilerOptions));
changes.replaceNode(sourceFile, moduleSpecifier!, factory.createStringLiteral(changedExtension));
}
}
if (convertExistingToTypeOnly) {
const namedImports = tryCast(importClause.namedBindings, isNamedImports);
if (namedImports && namedImports.elements.length > 1) {
Expand Down
40 changes: 40 additions & 0 deletions tests/baselines/reference/allowsImportingTsExtension.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
b.ts(2,16): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
b.ts(3,30): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
b.ts(5,25): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
c.ts(2,16): error TS2846: A declaration file cannot be imported without 'import type'. Did you mean to import an implementation file './a.js' instead?
c.ts(3,30): error TS2846: A declaration file cannot be imported without 'import type'. Did you mean to import an implementation file './a.js' instead?
c.ts(5,25): error TS2846: A declaration file cannot be imported without 'import type'. Did you mean to import an implementation file './a.js' instead?


==== a.ts (0 errors) ====
export class A {}

==== a.d.ts (0 errors) ====
export class A {}

==== b.ts (3 errors) ====
import type { A } from "./a.ts"; // ok
import {} from "./a.ts"; // error
~~~~~~~~
!!! error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
import { type A as _A } from "./a.ts"; // error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting i would have expected above two to be not errors per your description since its not in emitted?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s a bit of a gray area, but I decided to treat this as if verbatimModuleSyntax is enabled. Depending on settings, this import could get preserved as import {} from "./a.ts", whereas import type is unambiguously elided. It feels better to rely on the strong, unambiguous signal that the import will always be elided.

~~~~~~~~
!!! error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
type __A = import("./a.ts").A; // ok
const aPromise = import("./a.ts"); // error
~~~~~~~~
!!! error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.

==== c.ts (3 errors) ====
import type { A } from "./a.d.ts"; // ok
import {} from "./a.d.ts"; // error
~~~~~~~~~~
!!! error TS2846: A declaration file cannot be imported without 'import type'. Did you mean to import an implementation file './a.js' instead?
import { type A as _A } from "./a.d.ts"; // error
~~~~~~~~~~
!!! error TS2846: A declaration file cannot be imported without 'import type'. Did you mean to import an implementation file './a.js' instead?
type __A = import("./a.d.ts").A; // ok
const aPromise = import("./a.d.ts"); // error
~~~~~~~~~~
!!! error TS2846: A declaration file cannot be imported without 'import type'. Did you mean to import an implementation file './a.js' instead?

32 changes: 32 additions & 0 deletions tests/baselines/reference/allowsImportingTsExtension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//// [tests/cases/conformance/externalModules/typeOnly/allowsImportingTsExtension.ts] ////

//// [a.ts]
export class A {}

//// [a.d.ts]
export class A {}

//// [b.ts]
import type { A } from "./a.ts"; // ok
import {} from "./a.ts"; // error
import { type A as _A } from "./a.ts"; // error
type __A = import("./a.ts").A; // ok
const aPromise = import("./a.ts"); // error

//// [c.ts]
import type { A } from "./a.d.ts"; // ok
import {} from "./a.d.ts"; // error
import { type A as _A } from "./a.d.ts"; // error
type __A = import("./a.d.ts").A; // ok
const aPromise = import("./a.d.ts"); // error


//// [a.js]
export class A {
}
//// [b.js]
const aPromise = import("./a.ts"); // error
export {};
//// [c.js]
const aPromise = import("./a.d.ts"); // error
export {};
44 changes: 44 additions & 0 deletions tests/baselines/reference/allowsImportingTsExtension.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//// [tests/cases/conformance/externalModules/typeOnly/allowsImportingTsExtension.ts] ////

=== a.ts ===
export class A {}
>A : Symbol(A, Decl(a.ts, 0, 0))

=== a.d.ts ===
export class A {}
>A : Symbol(A, Decl(a.d.ts, 0, 0))

=== b.ts ===
import type { A } from "./a.ts"; // ok
>A : Symbol(A, Decl(b.ts, 0, 13))

import {} from "./a.ts"; // error
import { type A as _A } from "./a.ts"; // error
>A : Symbol(A, Decl(a.ts, 0, 0))
>_A : Symbol(_A, Decl(b.ts, 2, 8))

type __A = import("./a.ts").A; // ok
>__A : Symbol(__A, Decl(b.ts, 2, 38))
>A : Symbol(A, Decl(a.ts, 0, 0))

const aPromise = import("./a.ts"); // error
>aPromise : Symbol(aPromise, Decl(b.ts, 4, 5))
>"./a.ts" : Symbol("a", Decl(a.ts, 0, 0))

=== c.ts ===
import type { A } from "./a.d.ts"; // ok
>A : Symbol(A, Decl(c.ts, 0, 13))

import {} from "./a.d.ts"; // error
import { type A as _A } from "./a.d.ts"; // error
>A : Symbol(A, Decl(a.ts, 0, 0))
>_A : Symbol(_A, Decl(c.ts, 2, 8))

type __A = import("./a.d.ts").A; // ok
>__A : Symbol(__A, Decl(c.ts, 2, 40))
>A : Symbol(A, Decl(a.ts, 0, 0))

const aPromise = import("./a.d.ts"); // error
>aPromise : Symbol(aPromise, Decl(c.ts, 4, 5))
>"./a.d.ts" : Symbol("a", Decl(a.ts, 0, 0))

44 changes: 44 additions & 0 deletions tests/baselines/reference/allowsImportingTsExtension.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//// [tests/cases/conformance/externalModules/typeOnly/allowsImportingTsExtension.ts] ////

=== a.ts ===
export class A {}
>A : A

=== a.d.ts ===
export class A {}
>A : A

=== b.ts ===
import type { A } from "./a.ts"; // ok
>A : A

import {} from "./a.ts"; // error
import { type A as _A } from "./a.ts"; // error
>A : typeof A
>_A : typeof A

type __A = import("./a.ts").A; // ok
>__A : A

const aPromise = import("./a.ts"); // error
>aPromise : Promise<typeof import("a")>
>import("./a.ts") : Promise<typeof import("a")>
>"./a.ts" : "./a.ts"

=== c.ts ===
import type { A } from "./a.d.ts"; // ok
>A : A

import {} from "./a.d.ts"; // error
import { type A as _A } from "./a.d.ts"; // error
>A : typeof A
>_A : typeof A

type __A = import("./a.d.ts").A; // ok
>__A : A

const aPromise = import("./a.d.ts"); // error
>aPromise : Promise<typeof import("a")>
>import("./a.d.ts") : Promise<typeof import("a")>
>"./a.d.ts" : "./a.d.ts"

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// @allowImportingTsExtensions: false
// @target: esnext
// @module: esnext

// @Filename: a.ts

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there already an equivalent test case of the 5 assertions below for the case where a.ts is a.d.ts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, but I'll add one here so it can be compared easily

export class A {}

// @Filename: a.d.ts
export class A {}

// @Filename: b.ts
import type { A } from "./a.ts"; // ok
import {} from "./a.ts"; // error
import { type A as _A } from "./a.ts"; // error
type __A = import("./a.ts").A; // ok
const aPromise = import("./a.ts"); // error

// @Filename: c.ts
import type { A } from "./a.d.ts"; // ok
import {} from "./a.d.ts"; // error
import { type A as _A } from "./a.d.ts"; // error
type __A = import("./a.d.ts").A; // ok
const aPromise = import("./a.d.ts"); // error
34 changes: 34 additions & 0 deletions tests/cases/fourslash/completionsImport_promoteTypeOnly6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/// <reference path="fourslash.ts" />
// @module: nodenext
// @allowImportingTsExtensions: false

// @Filename: /exports.ts
//// export interface SomeInterface {}
//// export class SomePig {}

// @Filename: /a.ts
//// import type { SomePig } from "./exports.ts";
//// new SomePig/**/

verify.completions({
marker: "",
includes: [{
name: "SomePig",
source: completion.CompletionSource.TypeOnlyAlias,
hasAction: true,
}]
});

verify.applyCodeActionFromCompletion("", {
name: "SomePig",
source: completion.CompletionSource.TypeOnlyAlias,
description: `Remove 'type' from import declaration from "./exports.ts"`,
newFileContent:
`import { SomePig } from "./exports.js";
new SomePig`,
preferences: {
includeCompletionsForModuleExports: true,
allowIncompleteCompletions: true,
includeInsertTextCompletions: true,
},
});
34 changes: 34 additions & 0 deletions tests/cases/fourslash/completionsImport_promoteTypeOnly7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/// <reference path="fourslash.ts" />
// @module: nodenext
// @allowImportingTsExtensions: true

// @Filename: /exports.ts
//// export interface SomeInterface {}
//// export class SomePig {}

// @Filename: /a.ts
//// import type { SomePig } from "./exports.ts";
//// new SomePig/**/

verify.completions({
marker: "",
includes: [{
name: "SomePig",
source: completion.CompletionSource.TypeOnlyAlias,
hasAction: true,
}]
});

verify.applyCodeActionFromCompletion("", {
name: "SomePig",
source: completion.CompletionSource.TypeOnlyAlias,
description: `Remove 'type' from import declaration from "./exports.ts"`,
newFileContent:
`import { SomePig } from "./exports.ts";
new SomePig`,
preferences: {
includeCompletionsForModuleExports: true,
allowIncompleteCompletions: true,
includeInsertTextCompletions: true,
},
});
Loading