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

DO NOT MERGE: Show preserved triple slash references in diagnostics for top200 analysis #57656

Closed
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: 9 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -7944,5 +7944,14 @@
"'await using' statements cannot be used inside a class static block.": {
"category": "Error",
"code": 18054
},

"Declaration file contains hand-written type reference directives: {0}": {
"category": "Error",
"code": 18055
},
"Declaration file contains hand-written file reference directives: {0}": {
"category": "Error",
"code": 18056
}
}
13 changes: 12 additions & 1 deletion src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
FunctionTypeNode,
GeneratedIdentifierFlags,
GetAccessorDeclaration,
getBaseFileName,
getCommentRange,
getDirectoryPath,
getEffectiveBaseTypeNode,
Expand All @@ -62,6 +63,7 @@ import {
getLineAndCharacterOfPosition,
getNameOfDeclaration,
getNormalizedAbsolutePath,
getNormalizedAbsolutePathWithoutRoot,
getOriginalNodeId,
getOutputPathsFor,
getParseTreeNode,
Expand Down Expand Up @@ -532,7 +534,16 @@ export function transformDeclarations(context: TransformationContext) {
combinedStatements = setTextRange(factory.createNodeArray([...combinedStatements, createEmptyExports(factory)]), combinedStatements);
}
}
const updated = factory.updateSourceFile(node, combinedStatements, /*isDeclarationFile*/ true, references, getFileReferencesForUsedTypeReferences(), node.hasNoDefaultLib, getLibReferences());
const typeReferences = getFileReferencesForUsedTypeReferences();
const updated = factory.updateSourceFile(node, combinedStatements, /*isDeclarationFile*/ true, references, typeReferences, node.hasNoDefaultLib, getLibReferences());
const handWrittenTypeReferences = typeReferences.filter(ref => node.typeReferenceDirectives.some(d => d.fileName === ref.fileName));
const handWrittenFileReferences = references.filter(ref => node.referencedFiles.some(d => getNormalizedAbsolutePathWithoutRoot(getBaseFileName(d.fileName), "") === getNormalizedAbsolutePathWithoutRoot(getBaseFileName(ref.fileName), "")));
if (handWrittenTypeReferences.length) {
context.addDiagnostic(createDiagnosticForNode(node, Diagnostics.Declaration_file_contains_hand_written_type_reference_directives_Colon_0, arrayFrom(new Set(handWrittenTypeReferences.map(ref => `"${ref.fileName}"`))).join(", ")));
}
if (handWrittenFileReferences.length) {
context.addDiagnostic(createDiagnosticForNode(node, Diagnostics.Declaration_file_contains_hand_written_file_reference_directives_Colon_0, arrayFrom(new Set(handWrittenFileReferences.map(ref => `"${ref.fileName}"`))).join(", ")));
}
updated.exportedModulesFromDeclarationEmit = exportedModulesFromDeclarationEmit;
return updated;

Expand Down
31 changes: 31 additions & 0 deletions tests/baselines/reference/commonSourceDirectory.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/app/index.ts(2,1): error TS18056: Declaration file contains hand-written file reference directives: "../../types/bar.d.ts"


==== /app/tsconfig.json (0 errors) ====
{
"compilerOptions": {
"outDir": "bin",
"typeRoots": ["../types"],
"sourceMap": true,
"mapRoot": "myMapRoot",
"sourceRoot": "mySourceRoot",
"declaration": true
}
}

==== /app/index.ts (1 errors) ====
/// <reference path="../types/bar.d.ts"/>
import { x } from "foo";
~~~~~~
!!! error TS18056: Declaration file contains hand-written file reference directives: "../../types/bar.d.ts"
import { y } from "bar";
x + y;

==== /node_modules/foo/index.ts (0 errors) ====
export const x = 0;

==== /types/bar.d.ts (0 errors) ====
declare module "bar" {
export const y = 0;
}

6 changes: 1 addition & 5 deletions tests/baselines/reference/commonSourceDirectory.js

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

23 changes: 23 additions & 0 deletions tests/baselines/reference/commonSourceDirectory_dts.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/app/src/index.ts(2,1): error TS18056: Declaration file contains hand-written file reference directives: "../lib/bar.d.ts"


==== /app/tsconfig.json (0 errors) ====
{
"compilerOptions": {
"outDir": "bin",
"sourceMap": true,
"mapRoot": "myMapRoot",
"sourceRoot": "mySourceRoot",
"declaration": true
}
}

==== /app/lib/bar.d.ts (0 errors) ====
declare const y: number;

==== /app/src/index.ts (1 errors) ====
/// <reference path="../lib/bar.d.ts" />
export const x = y;
~~~~~~
!!! error TS18056: Declaration file contains hand-written file reference directives: "../lib/bar.d.ts"

6 changes: 1 addition & 5 deletions tests/baselines/reference/commonSourceDirectory_dts.js

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

Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
client.ts(2,1): error TS18056: Declaration file contains hand-written file reference directives: "declFile.d.ts"
declFile.d.ts(2,5): error TS1038: A 'declare' modifier cannot be used in an already ambient context.
declFile.d.ts(3,5): error TS1038: A 'declare' modifier cannot be used in an already ambient context.
declFile.d.ts(5,5): error TS1038: A 'declare' modifier cannot be used in an already ambient context.
declFile.d.ts(7,5): error TS1038: A 'declare' modifier cannot be used in an already ambient context.


==== client.ts (0 errors) ====
==== client.ts (1 errors) ====
///<reference path="declFile.d.ts"/>
var x = new M.C(); // Declaration file wont get emitted because there are errors in declaration file
~~~
!!! error TS18056: Declaration file contains hand-written file reference directives: "declFile.d.ts"

==== declFile.d.ts (4 errors) ====
declare module M {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,3 @@ var x = new M.C(); // Declaration file wont get emitted because there are errors
//// [client.js]
///<reference path="declFile.d.ts"/>
var x = new M.C(); // Declaration file wont get emitted because there are errors in declaration file


//// [client.d.ts]
/// <reference path="declFile.d.ts" />
declare var x: M.C;
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
client.ts(2,1): error TS18056: Declaration file contains hand-written file reference directives: "declFile.d.ts"
declFile.d.ts(2,5): error TS1038: A 'declare' modifier cannot be used in an already ambient context.
declFile.d.ts(3,5): error TS1038: A 'declare' modifier cannot be used in an already ambient context.
declFile.d.ts(5,5): error TS1038: A 'declare' modifier cannot be used in an already ambient context.
declFile.d.ts(7,5): error TS1038: A 'declare' modifier cannot be used in an already ambient context.


==== client.ts (0 errors) ====
==== client.ts (1 errors) ====
///<reference path="declFile.d.ts"/>
var x = new M.C(); // Declaration file wont get emitted because there are errors in declaration file
~~~
!!! error TS18056: Declaration file contains hand-written file reference directives: "declFile.d.ts"

==== declFile.d.ts (4 errors) ====
declare module M {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/a/app.ts(2,1): error TS18055: Declaration file contains hand-written type reference directives: "jquery"


==== /a/node_modules/@types/jquery/index.d.ts (0 errors) ====
interface JQuery {

}

==== /a/app.ts (1 errors) ====
/// <reference types="jquery"/>
namespace Test {
~~~~~~~~~
!!! error TS18055: Declaration file contains hand-written type reference directives: "jquery"
export var x: JQuery;
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,3 @@ var Test;
declare namespace Test {
var x: JQuery;
}


//// [DtsFileErrors]


out.d.ts(1,23): error TS2688: Cannot find type definition file for 'jquery'.


==== /a/node_modules/@types/jquery/index.d.ts (0 errors) ====
interface JQuery {

}

==== out.d.ts (1 errors) ====
/// <reference types="jquery" />
~~~~~~
!!! error TS2688: Cannot find type definition file for 'jquery'.
declare namespace Test {
var x: JQuery;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/a/app.ts(2,1): error TS18055: Declaration file contains hand-written type reference directives: "node"


==== /a/node_modules/@types/node/index.d.ts (0 errors) ====
interface Error2 {
stack2: string;
}

==== /a/app.ts (1 errors) ====
/// <reference types="node"/>
function foo(): Error2 {
~~~~~~~~
!!! error TS18055: Declaration file contains hand-written type reference directives: "node"
return undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,3 @@ function foo(): Error2 {
function foo() {
return undefined;
}


//// [app.d.ts]
/// <reference types="node" />
declare function foo(): Error2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/app.mts(2,1): error TS18055: Declaration file contains hand-written type reference directives: "node"


==== /tsconfig.json (0 errors) ====
{
"compilerOptions": {
"module": "nodenext",
"types": [],
"declaration": true,
"emitDeclarationOnly": true,
}
}

==== /app.mts (1 errors) ====
/// <reference types="node" />
export async function drainStream(stream: NodeJS.ReadableStream): Promise<void> {
~~~~~~
!!! error TS18055: Declaration file contains hand-written type reference directives: "node"
}

==== /node_modules/@types/node/package.json (0 errors) ====
{
"name": "@types/node",
"version": "1.0.0",
"types": "index.d.ts"
}

==== /node_modules/@types/node/globals.d.ts (0 errors) ====
declare namespace NodeJS {
interface ReadableStream {}
}

==== /node_modules/@types/node/index.d.ts (0 errors) ====
/// <reference path="globals.d.ts" />

This file was deleted.

Loading
Loading