Skip to content

Commit

Permalink
fixed errors on specifier-less imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed May 27, 2024
1 parent 908ddd0 commit ca9be69
Show file tree
Hide file tree
Showing 44 changed files with 367 additions and 88 deletions.
8 changes: 4 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4568,9 +4568,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

if (resolvedModule.resolvedUsingTsExtension && isDeclarationFileName(moduleReference)) {
const importOrExport = findAncestor(location, isImportDeclaration)?.importClause ||
const importOrExport = findAncestor(location, isImportDeclaration) ||
findAncestor(location, or(isImportEqualsDeclaration, isExportDeclaration));
if (errorNode && importOrExport && !importOrExport.isTypeOnly || findAncestor(location, isImportCall)) {
if (errorNode && importOrExport && !(isImportDeclaration(importOrExport) ? importOrExport.importClause : importOrExport)?.isTypeOnly || findAncestor(location, isImportCall)) {
error(
errorNode,
Diagnostics.A_declaration_file_cannot_be_imported_without_import_type_Did_you_mean_to_import_an_implementation_file_0_instead,
Expand All @@ -4579,9 +4579,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
else if (resolvedModule.resolvedUsingTsExtension && !shouldAllowImportingTsExtension(compilerOptions, currentSourceFile.fileName)) {
const importOrExport = findAncestor(location, isImportDeclaration)?.importClause ||
const importOrExport = findAncestor(location, isImportDeclaration) ||
findAncestor(location, or(isImportEqualsDeclaration, isExportDeclaration));
if (errorNode && !(importOrExport?.isTypeOnly || findAncestor(location, isImportTypeNode))) {
if (errorNode && !(importOrExport && (isImportDeclaration(importOrExport) ? importOrExport.importClause : 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);
}
Expand Down
7 changes: 5 additions & 2 deletions src/testRunner/unittests/programApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,17 @@ describe("unittests:: programApi:: Program.getTypeChecker / Program.getSemanticD
const sourceFile = program.getSourceFile("main.ts")!;
const typeChecker = program.getTypeChecker();
typeChecker.getSymbolAtLocation((sourceFile.statements[0] as ts.ImportDeclaration).moduleSpecifier);
assert.isEmpty(program.getSemanticDiagnostics());
const diagnostics = program.getSemanticDiagnostics()
assert.equal(diagnostics.length, 1);
assert.equal(diagnostics[0].code, ts.Diagnostics.File_0_is_not_a_module.code);
assert.equal(diagnostics[0].messageText, "File '/module.d.ts' is not a module.");
});
});

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

const fs = vfs.createFromFileSystem(Harness.IO, /*ignoreCase*/ false, { documents: [main, mod], cwd: "/" });
const program = ts.createProgram(["./main.ts"], {
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/tscWatch/programUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2129,7 +2129,7 @@ import { x } from "../b";`,
sys: () => {
const module1: File = {
path: `/user/username/projects/myproject/a.ts`,
content: ``,
content: `export {};`,
};
const module2: File = {
path: `/user/username/projects/myproject/b.ts`,
Expand Down
20 changes: 14 additions & 6 deletions tests/baselines/reference/allowsImportingTsExtension.errors.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
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.
b.ts(3,8): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
b.ts(4,30): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
b.ts(6,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?
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?
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?
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?


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

==== b.ts (3 errors) ====
==== b.ts (4 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 "./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
~~~~~~~~
Expand All @@ -25,10 +30,13 @@ c.ts(5,25): error TS2846: A declaration file cannot be imported without 'import
~~~~~~~~
!!! error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.

==== c.ts (3 errors) ====
==== c.ts (4 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 "./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
~~~~~~~~~~
Expand Down
6 changes: 4 additions & 2 deletions tests/baselines/reference/allowsImportingTsExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ export class A {}
//// [b.ts]
import type { A } from "./a.ts"; // ok
import {} from "./a.ts"; // error
import "./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 "./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
Expand All @@ -25,8 +27,8 @@ const aPromise = import("./a.d.ts"); // error
export class A {
}
//// [b.js]
import "./a.ts"; // error
const aPromise = import("./a.ts"); // error
export {};
//// [c.js]
import "./a.d.ts"; // error
const aPromise = import("./a.d.ts"); // error
export {};
14 changes: 8 additions & 6 deletions tests/baselines/reference/allowsImportingTsExtension.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,34 @@ import type { A } from "./a.ts"; // ok
>A : Symbol(A, Decl(b.ts, 0, 13))

import {} from "./a.ts"; // error
import "./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))
>_A : Symbol(_A, Decl(b.ts, 3, 8))

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

const aPromise = import("./a.ts"); // error
>aPromise : Symbol(aPromise, Decl(b.ts, 4, 5))
>aPromise : Symbol(aPromise, Decl(b.ts, 5, 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 "./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))
>_A : Symbol(_A, Decl(c.ts, 3, 8))

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

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

2 changes: 2 additions & 0 deletions tests/baselines/reference/allowsImportingTsExtension.types
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type { A } from "./a.ts"; // ok
> : ^

import {} from "./a.ts"; // error
import "./a.ts"; // error
import { type A as _A } from "./a.ts"; // error
>A : typeof A
> : ^^^^^^^^
Expand All @@ -40,6 +41,7 @@ import type { A } from "./a.d.ts"; // ok
> : ^

import {} from "./a.d.ts"; // error
import "./a.d.ts"; // error
import { type A as _A } from "./a.d.ts"; // error
>A : typeof A
> : ^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
consumer.ts(4,8): error TS2307: Cannot find module 'foo' or its corresponding type declarations.
consumer.ts(6,8): error TS2307: Cannot find module 'foo2' or its corresponding type declarations.
foo.d.ts(1,16): error TS2714: The expression of an export assignment must be an identifier or qualified name in an ambient context.
foo2.d.ts(1,10): error TS2714: The expression of an export assignment must be an identifier or qualified name in an ambient context.
indirection.d.ts(3,20): error TS2714: The expression of an export assignment must be an identifier or qualified name in an ambient context.
indirection2.d.ts(3,14): error TS2714: The expression of an export assignment must be an identifier or qualified name in an ambient context.


==== consumer.ts (0 errors) ====
==== consumer.ts (2 errors) ====
/// <reference path="./indirection.d.ts" />
/// <reference path="./indirection2.d.ts" />
import "indirect";
import "foo";
~~~~~
!!! error TS2307: Cannot find module 'foo' or its corresponding type declarations.
import "indirect2";
import "foo2";
~~~~~~
!!! error TS2307: Cannot find module 'foo2' or its corresponding type declarations.
==== foo.d.ts (1 errors) ====
export default 2 + 2;
~~~~~
Expand Down
10 changes: 8 additions & 2 deletions tests/baselines/reference/amdDependencyCommentName4.errors.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
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?
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?
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?
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?
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?
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?


==== amdDependencyCommentName4.ts (4 errors) ====
==== amdDependencyCommentName4.ts (6 errors) ====
///<amd-dependency path='aliasedModule5' name='n1'/>
///<amd-dependency path='unaliasedModule3'/>
///<amd-dependency path='aliasedModule6' name='n2'/>
///<amd-dependency path='unaliasedModule4'/>

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

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

import "unaliasedModule2";
import "unaliasedModule2";
~~~~~~~~~~~~~~~~~~
!!! error TS2792: Cannot find module 'unaliasedModule2'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ autoAccessorDisallowedModifiers.ts(31,1): error TS1275: 'accessor' modifier can
autoAccessorDisallowedModifiers.ts(32,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
autoAccessorDisallowedModifiers.ts(33,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
autoAccessorDisallowedModifiers.ts(34,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
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?
autoAccessorDisallowedModifiers.ts(35,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
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?
autoAccessorDisallowedModifiers.ts(36,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
autoAccessorDisallowedModifiers.ts(37,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
autoAccessorDisallowedModifiers.ts(38,1): error TS1275: 'accessor' modifier can only appear on a property declaration.


==== autoAccessorDisallowedModifiers.ts (30 errors) ====
==== autoAccessorDisallowedModifiers.ts (31 errors) ====
abstract class C1 {
accessor accessor a: any;
~~~~~~~~
Expand Down Expand Up @@ -115,6 +116,8 @@ autoAccessorDisallowedModifiers.ts(38,1): error TS1275: 'accessor' modifier can
accessor import "x";
~~~~~~~~
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
~~~
!!! error TS2792: Cannot find module 'x'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
accessor import {} from "x";
~~~~~~~~
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
validator.ts(1,8): error TS2307: Cannot find module './' or its corresponding type declarations.
validator.ts(19,4): error TS2540: Cannot assign to 'readonlyProp' because it is a read-only property.
validator.ts(20,4): error TS2540: Cannot assign to 'readonlyAccessor' because it is a read-only property.
validator.ts(21,1): error TS2322: Type 'string' is not assignable to type 'number'.
validator.ts(22,1): error TS2322: Type 'string' is not assignable to type 'number'.
validator.ts(23,1): error TS2322: Type 'number' is not assignable to type 'string'.


==== validator.ts (5 errors) ====
==== validator.ts (6 errors) ====
import "./";
~~~~
!!! error TS2307: Cannot find module './' or its corresponding type declarations.

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
es6ImportWithoutFromClauseInEs5_1.ts(1,8): error TS2307: Cannot find module 'es6ImportWithoutFromClauseInEs5_0' or its corresponding type declarations.


==== es6ImportWithoutFromClauseInEs5_0.ts (0 errors) ====
export var a = 10;

==== es6ImportWithoutFromClauseInEs5_1.ts (1 errors) ====
import "es6ImportWithoutFromClauseInEs5_0";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2307: Cannot find module 'es6ImportWithoutFromClauseInEs5_0' or its corresponding type declarations.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
client.ts(1,1): error TS1191: An import declaration cannot have modifiers.
client.ts(1,15): error TS2307: Cannot find module 'server' or its corresponding type declarations.


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

==== client.ts (1 errors) ====
==== client.ts (2 errors) ====
export import "server";
~~~~~~
!!! error TS1191: An import declaration cannot have modifiers.
!!! error TS1191: An import declaration cannot have modifiers.
~~~~~~~~
!!! error TS2307: Cannot find module 'server' or its corresponding type declarations.
20 changes: 20 additions & 0 deletions tests/baselines/reference/extendGlobalThis.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
index.ts(1,8): error TS2307: Cannot find module './extention' or its corresponding type declarations.


==== extension.d.ts (0 errors) ====
declare global {
namespace globalThis {
var test: string;
}
}

export {}

==== index.ts (1 errors) ====
import "./extention";
~~~~~~~~~~~~~
!!! error TS2307: Cannot find module './extention' or its corresponding type declarations.

globalThis.tests = "a-b";
console.log(globalThis.test.split("-"));

1 change: 1 addition & 0 deletions tests/baselines/reference/extendGlobalThis.types
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ globalThis.tests = "a-b";
>globalThis.tests = "a-b" : "a-b"
> : ^^^^^
>globalThis.tests : any
> : ^^^
>globalThis : typeof globalThis
> : ^^^^^^^^^^^^^^^^^
>tests : any
Expand Down
28 changes: 0 additions & 28 deletions tests/baselines/reference/jsxClassAttributeResolution.errors.txt

This file was deleted.

2 changes: 1 addition & 1 deletion tests/baselines/reference/jsxClassAttributeResolution.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const a = <App></App>;
interface IntrinsicClassAttributesAlias<T> {
ref: T
}
declare namespace JSX {
export declare namespace JSX {
type IntrinsicClassAttributes<T> = IntrinsicClassAttributesAlias<T>
}
//// [jsx-runtime.d.ts]
Expand Down
Loading

0 comments on commit ca9be69

Please sign in to comment.