Skip to content

Commit ca9be69

Browse files
committed
fixed errors on specifier-less imports
1 parent 908ddd0 commit ca9be69

File tree

44 files changed

+367
-88
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+367
-88
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4568,9 +4568,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
45684568
}
45694569

45704570
if (resolvedModule.resolvedUsingTsExtension && isDeclarationFileName(moduleReference)) {
4571-
const importOrExport = findAncestor(location, isImportDeclaration)?.importClause ||
4571+
const importOrExport = findAncestor(location, isImportDeclaration) ||
45724572
findAncestor(location, or(isImportEqualsDeclaration, isExportDeclaration));
4573-
if (errorNode && importOrExport && !importOrExport.isTypeOnly || findAncestor(location, isImportCall)) {
4573+
if (errorNode && importOrExport && !(isImportDeclaration(importOrExport) ? importOrExport.importClause : importOrExport)?.isTypeOnly || findAncestor(location, isImportCall)) {
45744574
error(
45754575
errorNode,
45764576
Diagnostics.A_declaration_file_cannot_be_imported_without_import_type_Did_you_mean_to_import_an_implementation_file_0_instead,
@@ -4579,9 +4579,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
45794579
}
45804580
}
45814581
else if (resolvedModule.resolvedUsingTsExtension && !shouldAllowImportingTsExtension(compilerOptions, currentSourceFile.fileName)) {
4582-
const importOrExport = findAncestor(location, isImportDeclaration)?.importClause ||
4582+
const importOrExport = findAncestor(location, isImportDeclaration) ||
45834583
findAncestor(location, or(isImportEqualsDeclaration, isExportDeclaration));
4584-
if (errorNode && !(importOrExport?.isTypeOnly || findAncestor(location, isImportTypeNode))) {
4584+
if (errorNode && !(importOrExport && (isImportDeclaration(importOrExport) ? importOrExport.importClause : importOrExport)?.isTypeOnly || findAncestor(location, isImportTypeNode))) {
45854585
const tsExtension = Debug.checkDefined(tryExtractTSExtension(moduleReference));
45864586
error(errorNode, Diagnostics.An_import_path_can_only_end_with_a_0_extension_when_allowImportingTsExtensions_is_enabled, tsExtension);
45874587
}

src/testRunner/unittests/programApi.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,17 @@ describe("unittests:: programApi:: Program.getTypeChecker / Program.getSemanticD
216216
const sourceFile = program.getSourceFile("main.ts")!;
217217
const typeChecker = program.getTypeChecker();
218218
typeChecker.getSymbolAtLocation((sourceFile.statements[0] as ts.ImportDeclaration).moduleSpecifier);
219-
assert.isEmpty(program.getSemanticDiagnostics());
219+
const diagnostics = program.getSemanticDiagnostics()
220+
assert.equal(diagnostics.length, 1);
221+
assert.equal(diagnostics[0].code, ts.Diagnostics.File_0_is_not_a_module.code);
222+
assert.equal(diagnostics[0].messageText, "File '/module.d.ts' is not a module.");
220223
});
221224
});
222225

223226
describe("unittests:: programApi:: CompilerOptions relative paths", () => {
224227
it("resolves relative paths by getCurrentDirectory", () => {
225228
const main = new documents.TextDocument("/main.ts", 'import "module";');
226-
const mod = new documents.TextDocument("/lib/module.ts", "declare const foo: any;");
229+
const mod = new documents.TextDocument("/lib/module.ts", "export declare const foo: any;");
227230

228231
const fs = vfs.createFromFileSystem(Harness.IO, /*ignoreCase*/ false, { documents: [main, mod], cwd: "/" });
229232
const program = ts.createProgram(["./main.ts"], {

src/testRunner/unittests/tscWatch/programUpdates.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2129,7 +2129,7 @@ import { x } from "../b";`,
21292129
sys: () => {
21302130
const module1: File = {
21312131
path: `/user/username/projects/myproject/a.ts`,
2132-
content: ``,
2132+
content: `export {};`,
21332133
};
21342134
const module2: File = {
21352135
path: `/user/username/projects/myproject/b.ts`,

tests/baselines/reference/allowsImportingTsExtension.errors.txt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
b.ts(2,16): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
2-
b.ts(3,30): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
3-
b.ts(5,25): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
2+
b.ts(3,8): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
3+
b.ts(4,30): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
4+
b.ts(6,25): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
45
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?
5-
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?
6-
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?
6+
c.ts(3,8): error TS2846: A declaration file cannot be imported without 'import type'. Did you mean to import an implementation file './a.js' instead?
7+
c.ts(4,30): error TS2846: A declaration file cannot be imported without 'import type'. Did you mean to import an implementation file './a.js' instead?
8+
c.ts(6,25): error TS2846: A declaration file cannot be imported without 'import type'. Did you mean to import an implementation file './a.js' instead?
79

810

911
==== a.ts (0 errors) ====
@@ -12,10 +14,13 @@ c.ts(5,25): error TS2846: A declaration file cannot be imported without 'import
1214
==== a.d.ts (0 errors) ====
1315
export class A {}
1416

15-
==== b.ts (3 errors) ====
17+
==== b.ts (4 errors) ====
1618
import type { A } from "./a.ts"; // ok
1719
import {} from "./a.ts"; // error
1820
~~~~~~~~
21+
!!! error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
22+
import "./a.ts"; // error
23+
~~~~~~~~
1924
!!! error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
2025
import { type A as _A } from "./a.ts"; // error
2126
~~~~~~~~
@@ -25,10 +30,13 @@ c.ts(5,25): error TS2846: A declaration file cannot be imported without 'import
2530
~~~~~~~~
2631
!!! error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
2732

28-
==== c.ts (3 errors) ====
33+
==== c.ts (4 errors) ====
2934
import type { A } from "./a.d.ts"; // ok
3035
import {} from "./a.d.ts"; // error
3136
~~~~~~~~~~
37+
!!! error TS2846: A declaration file cannot be imported without 'import type'. Did you mean to import an implementation file './a.js' instead?
38+
import "./a.d.ts"; // error
39+
~~~~~~~~~~
3240
!!! error TS2846: A declaration file cannot be imported without 'import type'. Did you mean to import an implementation file './a.js' instead?
3341
import { type A as _A } from "./a.d.ts"; // error
3442
~~~~~~~~~~

tests/baselines/reference/allowsImportingTsExtension.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ export class A {}
99
//// [b.ts]
1010
import type { A } from "./a.ts"; // ok
1111
import {} from "./a.ts"; // error
12+
import "./a.ts"; // error
1213
import { type A as _A } from "./a.ts"; // error
1314
type __A = import("./a.ts").A; // ok
1415
const aPromise = import("./a.ts"); // error
1516

1617
//// [c.ts]
1718
import type { A } from "./a.d.ts"; // ok
1819
import {} from "./a.d.ts"; // error
20+
import "./a.d.ts"; // error
1921
import { type A as _A } from "./a.d.ts"; // error
2022
type __A = import("./a.d.ts").A; // ok
2123
const aPromise = import("./a.d.ts"); // error
@@ -25,8 +27,8 @@ const aPromise = import("./a.d.ts"); // error
2527
export class A {
2628
}
2729
//// [b.js]
30+
import "./a.ts"; // error
2831
const aPromise = import("./a.ts"); // error
29-
export {};
3032
//// [c.js]
33+
import "./a.d.ts"; // error
3134
const aPromise = import("./a.d.ts"); // error
32-
export {};

tests/baselines/reference/allowsImportingTsExtension.symbols

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,34 @@ import type { A } from "./a.ts"; // ok
1313
>A : Symbol(A, Decl(b.ts, 0, 13))
1414

1515
import {} from "./a.ts"; // error
16+
import "./a.ts"; // error
1617
import { type A as _A } from "./a.ts"; // error
1718
>A : Symbol(A, Decl(a.ts, 0, 0))
18-
>_A : Symbol(_A, Decl(b.ts, 2, 8))
19+
>_A : Symbol(_A, Decl(b.ts, 3, 8))
1920

2021
type __A = import("./a.ts").A; // ok
21-
>__A : Symbol(__A, Decl(b.ts, 2, 38))
22+
>__A : Symbol(__A, Decl(b.ts, 3, 38))
2223
>A : Symbol(A, Decl(a.ts, 0, 0))
2324

2425
const aPromise = import("./a.ts"); // error
25-
>aPromise : Symbol(aPromise, Decl(b.ts, 4, 5))
26+
>aPromise : Symbol(aPromise, Decl(b.ts, 5, 5))
2627
>"./a.ts" : Symbol("a", Decl(a.ts, 0, 0))
2728

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

3233
import {} from "./a.d.ts"; // error
34+
import "./a.d.ts"; // error
3335
import { type A as _A } from "./a.d.ts"; // error
3436
>A : Symbol(A, Decl(a.ts, 0, 0))
35-
>_A : Symbol(_A, Decl(c.ts, 2, 8))
37+
>_A : Symbol(_A, Decl(c.ts, 3, 8))
3638

3739
type __A = import("./a.d.ts").A; // ok
38-
>__A : Symbol(__A, Decl(c.ts, 2, 40))
40+
>__A : Symbol(__A, Decl(c.ts, 3, 40))
3941
>A : Symbol(A, Decl(a.ts, 0, 0))
4042

4143
const aPromise = import("./a.d.ts"); // error
42-
>aPromise : Symbol(aPromise, Decl(c.ts, 4, 5))
44+
>aPromise : Symbol(aPromise, Decl(c.ts, 5, 5))
4345
>"./a.d.ts" : Symbol("a", Decl(a.ts, 0, 0))
4446

tests/baselines/reference/allowsImportingTsExtension.types

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type { A } from "./a.ts"; // ok
1616
> : ^
1717

1818
import {} from "./a.ts"; // error
19+
import "./a.ts"; // error
1920
import { type A as _A } from "./a.ts"; // error
2021
>A : typeof A
2122
> : ^^^^^^^^
@@ -40,6 +41,7 @@ import type { A } from "./a.d.ts"; // ok
4041
> : ^
4142

4243
import {} from "./a.d.ts"; // error
44+
import "./a.d.ts"; // error
4345
import { type A as _A } from "./a.d.ts"; // error
4446
>A : typeof A
4547
> : ^^^^^^^^

tests/baselines/reference/ambientExportDefaultErrors.errors.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1+
consumer.ts(4,8): error TS2307: Cannot find module 'foo' or its corresponding type declarations.
2+
consumer.ts(6,8): error TS2307: Cannot find module 'foo2' or its corresponding type declarations.
13
foo.d.ts(1,16): error TS2714: The expression of an export assignment must be an identifier or qualified name in an ambient context.
24
foo2.d.ts(1,10): error TS2714: The expression of an export assignment must be an identifier or qualified name in an ambient context.
35
indirection.d.ts(3,20): error TS2714: The expression of an export assignment must be an identifier or qualified name in an ambient context.
46
indirection2.d.ts(3,14): error TS2714: The expression of an export assignment must be an identifier or qualified name in an ambient context.
57

68

7-
==== consumer.ts (0 errors) ====
9+
==== consumer.ts (2 errors) ====
810
/// <reference path="./indirection.d.ts" />
911
/// <reference path="./indirection2.d.ts" />
1012
import "indirect";
1113
import "foo";
14+
~~~~~
15+
!!! error TS2307: Cannot find module 'foo' or its corresponding type declarations.
1216
import "indirect2";
1317
import "foo2";
18+
~~~~~~
19+
!!! error TS2307: Cannot find module 'foo2' or its corresponding type declarations.
1420
==== foo.d.ts (1 errors) ====
1521
export default 2 + 2;
1622
~~~~~

tests/baselines/reference/amdDependencyCommentName4.errors.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1+
amdDependencyCommentName4.ts(6,8): error TS2792: Cannot find module 'unaliasedModule1'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
12
amdDependencyCommentName4.ts(8,21): error TS2792: Cannot find module 'aliasedModule1'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
23
amdDependencyCommentName4.ts(11,26): error TS2792: Cannot find module 'aliasedModule2'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
34
amdDependencyCommentName4.ts(14,15): error TS2792: Cannot find module 'aliasedModule3'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
45
amdDependencyCommentName4.ts(17,21): error TS2792: Cannot find module 'aliasedModule4'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
6+
amdDependencyCommentName4.ts(20,8): error TS2792: Cannot find module 'unaliasedModule2'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
57

68

7-
==== amdDependencyCommentName4.ts (4 errors) ====
9+
==== amdDependencyCommentName4.ts (6 errors) ====
810
///<amd-dependency path='aliasedModule5' name='n1'/>
911
///<amd-dependency path='unaliasedModule3'/>
1012
///<amd-dependency path='aliasedModule6' name='n2'/>
1113
///<amd-dependency path='unaliasedModule4'/>
1214

1315
import "unaliasedModule1";
16+
~~~~~~~~~~~~~~~~~~
17+
!!! error TS2792: Cannot find module 'unaliasedModule1'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
1418

1519
import r1 = require("aliasedModule1");
1620
~~~~~~~~~~~~~~~~
@@ -32,4 +36,6 @@ amdDependencyCommentName4.ts(17,21): error TS2792: Cannot find module 'aliasedMo
3236
!!! error TS2792: Cannot find module 'aliasedModule4'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
3337
ns;
3438

35-
import "unaliasedModule2";
39+
import "unaliasedModule2";
40+
~~~~~~~~~~~~~~~~~~
41+
!!! error TS2792: Cannot find module 'unaliasedModule2'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?

tests/baselines/reference/autoAccessorDisallowedModifiers.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ autoAccessorDisallowedModifiers.ts(31,1): error TS1275: 'accessor' modifier can
2323
autoAccessorDisallowedModifiers.ts(32,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
2424
autoAccessorDisallowedModifiers.ts(33,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
2525
autoAccessorDisallowedModifiers.ts(34,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
26+
autoAccessorDisallowedModifiers.ts(34,17): error TS2792: Cannot find module 'x'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
2627
autoAccessorDisallowedModifiers.ts(35,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
2728
autoAccessorDisallowedModifiers.ts(35,25): error TS2792: Cannot find module 'x'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
2829
autoAccessorDisallowedModifiers.ts(36,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
2930
autoAccessorDisallowedModifiers.ts(37,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
3031
autoAccessorDisallowedModifiers.ts(38,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
3132

3233

33-
==== autoAccessorDisallowedModifiers.ts (30 errors) ====
34+
==== autoAccessorDisallowedModifiers.ts (31 errors) ====
3435
abstract class C1 {
3536
accessor accessor a: any;
3637
~~~~~~~~
@@ -115,6 +116,8 @@ autoAccessorDisallowedModifiers.ts(38,1): error TS1275: 'accessor' modifier can
115116
accessor import "x";
116117
~~~~~~~~
117118
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
119+
~~~
120+
!!! error TS2792: Cannot find module 'x'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
118121
accessor import {} from "x";
119122
~~~~~~~~
120123
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.

tests/baselines/reference/checkExportsObjectAssignPrototypeProperty.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
validator.ts(1,8): error TS2307: Cannot find module './' or its corresponding type declarations.
12
validator.ts(19,4): error TS2540: Cannot assign to 'readonlyProp' because it is a read-only property.
23
validator.ts(20,4): error TS2540: Cannot assign to 'readonlyAccessor' because it is a read-only property.
34
validator.ts(21,1): error TS2322: Type 'string' is not assignable to type 'number'.
45
validator.ts(22,1): error TS2322: Type 'string' is not assignable to type 'number'.
56
validator.ts(23,1): error TS2322: Type 'number' is not assignable to type 'string'.
67

78

8-
==== validator.ts (5 errors) ====
9+
==== validator.ts (6 errors) ====
910
import "./";
11+
~~~~
12+
!!! error TS2307: Cannot find module './' or its corresponding type declarations.
1013

1114
import Person = require("./mod1");
1215

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
es6ImportWithoutFromClauseInEs5_1.ts(1,8): error TS2307: Cannot find module 'es6ImportWithoutFromClauseInEs5_0' or its corresponding type declarations.
2+
3+
4+
==== es6ImportWithoutFromClauseInEs5_0.ts (0 errors) ====
5+
export var a = 10;
6+
7+
==== es6ImportWithoutFromClauseInEs5_1.ts (1 errors) ====
8+
import "es6ImportWithoutFromClauseInEs5_0";
9+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10+
!!! error TS2307: Cannot find module 'es6ImportWithoutFromClauseInEs5_0' or its corresponding type declarations.
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
client.ts(1,1): error TS1191: An import declaration cannot have modifiers.
2+
client.ts(1,15): error TS2307: Cannot find module 'server' or its corresponding type declarations.
23

34

45
==== server.ts (0 errors) ====
56
export var a = 10;
67

7-
==== client.ts (1 errors) ====
8+
==== client.ts (2 errors) ====
89
export import "server";
910
~~~~~~
10-
!!! error TS1191: An import declaration cannot have modifiers.
11+
!!! error TS1191: An import declaration cannot have modifiers.
12+
~~~~~~~~
13+
!!! error TS2307: Cannot find module 'server' or its corresponding type declarations.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
index.ts(1,8): error TS2307: Cannot find module './extention' or its corresponding type declarations.
2+
3+
4+
==== extension.d.ts (0 errors) ====
5+
declare global {
6+
namespace globalThis {
7+
var test: string;
8+
}
9+
}
10+
11+
export {}
12+
13+
==== index.ts (1 errors) ====
14+
import "./extention";
15+
~~~~~~~~~~~~~
16+
!!! error TS2307: Cannot find module './extention' or its corresponding type declarations.
17+
18+
globalThis.tests = "a-b";
19+
console.log(globalThis.test.split("-"));
20+

tests/baselines/reference/extendGlobalThis.types

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ globalThis.tests = "a-b";
2424
>globalThis.tests = "a-b" : "a-b"
2525
> : ^^^^^
2626
>globalThis.tests : any
27+
> : ^^^
2728
>globalThis : typeof globalThis
2829
> : ^^^^^^^^^^^^^^^^^
2930
>tests : any

tests/baselines/reference/jsxClassAttributeResolution.errors.txt

Lines changed: 0 additions & 28 deletions
This file was deleted.

tests/baselines/reference/jsxClassAttributeResolution.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const a = <App></App>;
1414
interface IntrinsicClassAttributesAlias<T> {
1515
ref: T
1616
}
17-
declare namespace JSX {
17+
export declare namespace JSX {
1818
type IntrinsicClassAttributes<T> = IntrinsicClassAttributesAlias<T>
1919
}
2020
//// [jsx-runtime.d.ts]

0 commit comments

Comments
 (0)