Skip to content

Commit 2b69c9f

Browse files
committed
Show synthesized triple slash references in diagnostics for analysis
1 parent df8d755 commit 2b69c9f

File tree

499 files changed

+7786
-1074
lines changed

Some content is hidden

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

499 files changed

+7786
-1074
lines changed

src/compiler/diagnosticMessages.json

+9
Original file line numberDiff line numberDiff line change
@@ -7980,5 +7980,14 @@
79807980
"'await using' statements cannot be used inside a class static block.": {
79817981
"category": "Error",
79827982
"code": 18054
7983+
},
7984+
7985+
"Declaration file contains synthesized type reference directives: '{0}'": {
7986+
"category": "Error",
7987+
"code": 18055
7988+
},
7989+
"Declaration file contains synthesized file reference directives: '{0}'": {
7990+
"category": "Error",
7991+
"code": 18056
79837992
}
79847993
}

src/compiler/transformers/declarations.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import {
5353
FunctionTypeNode,
5454
GeneratedIdentifierFlags,
5555
GetAccessorDeclaration,
56+
getBaseFileName,
5657
getCommentRange,
5758
getDirectoryPath,
5859
getEffectiveBaseTypeNode,
@@ -63,6 +64,7 @@ import {
6364
getLineAndCharacterOfPosition,
6465
getNameOfDeclaration,
6566
getNormalizedAbsolutePath,
67+
getNormalizedAbsolutePathWithoutRoot,
6668
getOriginalNodeId,
6769
getOutputPathsFor,
6870
getParseTreeNode,
@@ -547,7 +549,16 @@ export function transformDeclarations(context: TransformationContext) {
547549
combinedStatements = setTextRange(factory.createNodeArray([...combinedStatements, createEmptyExports(factory)]), combinedStatements);
548550
}
549551
}
550-
const updated = factory.updateSourceFile(node, combinedStatements, /*isDeclarationFile*/ true, references, getFileReferencesForUsedTypeReferences(), node.hasNoDefaultLib, getLibReferences());
552+
const typeReferences = getFileReferencesForUsedTypeReferences();
553+
const updated = factory.updateSourceFile(node, combinedStatements, /*isDeclarationFile*/ true, references, typeReferences, node.hasNoDefaultLib, getLibReferences());
554+
const synthesizedTypeReferences = typeReferences.filter(ref => !node.typeReferenceDirectives.some(d => d.fileName === ref.fileName));
555+
const synthesizedFileReferences = references.filter(ref => !node.referencedFiles.some(d => getNormalizedAbsolutePathWithoutRoot(getBaseFileName(d.fileName), "") === getNormalizedAbsolutePathWithoutRoot(getBaseFileName(ref.fileName), "")));
556+
if (synthesizedTypeReferences.length) {
557+
context.addDiagnostic(createDiagnosticForNode(node, Diagnostics.Declaration_file_contains_synthesized_type_reference_directives_Colon_0, synthesizedTypeReferences.map(ref => `"${ref.fileName}"`).join(", ")));
558+
}
559+
if (synthesizedFileReferences.length) {
560+
context.addDiagnostic(createDiagnosticForNode(node, Diagnostics.Declaration_file_contains_synthesized_file_reference_directives_Colon_0, synthesizedFileReferences.map(ref => `"${ref.fileName}"`).join(", ")));
561+
}
551562
updated.exportedModulesFromDeclarationEmit = exportedModulesFromDeclarationEmit;
552563
return updated;
553564

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
ExtendedClass.js(1,1): error TS18056: Declaration file contains synthesized file reference directives: '"../deps/BaseClass.d.ts"'
2+
3+
4+
==== typing.d.ts (0 errors) ====
5+
declare function define<T=unknown>(name: string, modules: string[], ready: (...modules: unknown[]) => T);
6+
==== deps/BaseClass.d.ts (0 errors) ====
7+
declare module "deps/BaseClass" {
8+
class BaseClass {
9+
static extends<A>(a: A): new () => A & BaseClass;
10+
}
11+
export = BaseClass;
12+
}
13+
==== ExtendedClass.js (1 errors) ====
14+
define("lib/ExtendedClass", ["deps/BaseClass"],
15+
~~~~~~
16+
!!! error TS18056: Declaration file contains synthesized file reference directives: '"../deps/BaseClass.d.ts"'
17+
/**
18+
* {typeof import("deps/BaseClass")}
19+
* @param {typeof import("deps/BaseClass")} BaseClass
20+
* @returns
21+
*/
22+
(BaseClass) => {
23+
24+
const ExtendedClass = BaseClass.extends({
25+
f: function() {
26+
return "something";
27+
}
28+
});
29+
30+
// Exports the module in a way tsc recognize class export
31+
const module = {};
32+
module.exports = ExtendedClass
33+
return module.exports;
34+
});

tests/baselines/reference/amdLikeInputDeclarationEmit.js

-40
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
declFileAmbientExternalModuleWithSingleExportedModule_1.ts(2,1): error TS18056: Declaration file contains synthesized file reference directives: '"declFileAmbientExternalModuleWithSingleExportedModule_0.d.ts"'
2+
3+
4+
==== declFileAmbientExternalModuleWithSingleExportedModule_1.ts (1 errors) ====
5+
///<reference path='declFileAmbientExternalModuleWithSingleExportedModule_0.ts'/>
6+
import SubModule = require('SubModule');
7+
~~~~~~
8+
!!! error TS18056: Declaration file contains synthesized file reference directives: '"declFileAmbientExternalModuleWithSingleExportedModule_0.d.ts"'
9+
export var x: SubModule.m.m3.c;
10+
11+
12+
==== declFileAmbientExternalModuleWithSingleExportedModule_0.ts (0 errors) ====
13+
declare module "SubModule" {
14+
export module m {
15+
export module m3 {
16+
interface c {
17+
}
18+
}
19+
}
20+
}
21+

tests/baselines/reference/declFileAmbientExternalModuleWithSingleExportedModule.js

-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,3 @@ declare module "SubModule" {
3333
}
3434
}
3535
}
36-
//// [declFileAmbientExternalModuleWithSingleExportedModule_1.d.ts]
37-
/// <reference path="declFileAmbientExternalModuleWithSingleExportedModule_0.d.ts" />
38-
import SubModule = require('SubModule');
39-
export declare var x: SubModule.m.m3.c;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
src/datastore_result.ts(1,1): error TS18056: Declaration file contains synthesized file reference directives: '"../lib/lib.d.ts"'
2+
3+
4+
==== lib/lib.d.ts (0 errors) ====
5+
declare module "lib/result" {
6+
export type Result<E extends Error, T> = (E & Failure<E>) | (T & Success<T>);
7+
export interface Failure<E extends Error> { }
8+
export interface Success<T> { }
9+
}
10+
11+
==== src/datastore_result.ts (1 errors) ====
12+
import { Result } from "lib/result";
13+
~~~~~~
14+
!!! error TS18056: Declaration file contains synthesized file reference directives: '"../lib/lib.d.ts"'
15+
16+
export type T<T> = Result<Error, T>;
17+
18+
==== src/conditional_directive_field.ts (0 errors) ====
19+
import * as DatastoreResult from "src/datastore_result";
20+
21+
export const build = (): DatastoreResult.T<string> => {
22+
return null;
23+
};
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
packages/secondpackage/index.ts(1,1): error TS18056: Declaration file contains synthesized file reference directives: '"../../types/component.d.ts"'
2+
3+
4+
==== types/component.d.ts (0 errors) ====
5+
declare module '@namespace/component' {
6+
export class Foo {}
7+
}
8+
==== packages/somepackage/index.d.ts (0 errors) ====
9+
import { Foo } from "@namespace/component";
10+
export declare const item: typeof Foo;
11+
==== packages/secondpackage/index.ts (1 errors) ====
12+
import { item } from "../somepackage";
13+
~~~~~~
14+
!!! error TS18056: Declaration file contains synthesized file reference directives: '"../../types/component.d.ts"'
15+
export const reeexported = item;
16+

tests/baselines/reference/declarationEmitCrossFileImportTypeOfAmbientModule.js

-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,3 @@ Object.defineProperty(exports, "__esModule", { value: true });
1818
exports.reeexported = void 0;
1919
var somepackage_1 = require("../somepackage");
2020
exports.reeexported = somepackage_1.item;
21-
22-
23-
//// [index.d.ts]
24-
/// <reference path="../../types/component.d.ts" />
25-
export declare const reeexported: typeof import("@namespace/component").Foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
src/inferred-comp-export.ts(1,1): error TS18055: Declaration file contains synthesized type reference directives: '"react"'
2+
3+
4+
==== node_modules/@types/react/index.d.ts (0 errors) ====
5+
export = React;
6+
7+
declare namespace React {
8+
export type Component<T = any, U = {}, V = {}> = { x: T, y: U, z: V };
9+
export interface DOMAttributes<T> { }
10+
}
11+
==== node_modules/@emotion/core/index.d.ts (0 errors) ====
12+
import {
13+
Component
14+
} from 'react'
15+
export {};
16+
17+
declare module 'react' {
18+
interface DOMAttributes<T> {
19+
css?: any
20+
}
21+
}
22+
23+
==== src/get-comp.ts (0 errors) ====
24+
import {Component} from 'react';
25+
26+
export function getComp(): Component {
27+
return {} as any as Component
28+
}
29+
==== src/inferred-comp-export.ts (1 errors) ====
30+
import { getComp } from "./get-comp";
31+
~~~~~~
32+
!!! error TS18055: Declaration file contains synthesized type reference directives: '"react"'
33+
34+
// this shouldn't need any triple-slash references - it should have a direct import to `react` and that's it
35+
// This issue (#35343) _only_ reproduces in the test harness when the file in question is in a subfolder
36+
export const obj = {
37+
comp: getComp()
38+
}
39+
==== src/some-other-file.ts (0 errors) ====
40+
export * from '@emotion/core';
41+

tests/baselines/reference/declarationEmitExportAssignedNamespaceNoTripleSlashTypesReference.js

-5
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,5 @@ __exportStar(require("@emotion/core"), exports);
7878
//// [get-comp.d.ts]
7979
import { Component } from 'react';
8080
export declare function getComp(): Component;
81-
//// [inferred-comp-export.d.ts]
82-
/// <reference types="react" />
83-
export declare const obj: {
84-
comp: import("react").Component;
85-
};
8681
//// [some-other-file.d.ts]
8782
export * from '@emotion/core';

tests/baselines/reference/declarationEmitExportAssignedNamespaceNoTripleSlashTypesReference.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
export {};
2323

2424
declare module 'react' {
25-
>'react' : error
25+
>'react' : any
2626

2727
interface DOMAttributes<T> {
2828
css?: any
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/src/index.ts(1,1): error TS18055: Declaration file contains synthesized type reference directives: '"dep"'
2+
3+
4+
==== /src/index.ts (1 errors) ====
5+
class Src implements NS.Dep { }
6+
~~~~~
7+
!!! error TS18055: Declaration file contains synthesized type reference directives: '"dep"'
8+
9+
==== /deps/dep/dep.d.ts (0 errors) ====
10+
declare namespace NS {
11+
interface Dep {
12+
}
13+
}
14+
==== /deps/dep/package.json (0 errors) ====
15+
{
16+
"typings": "dep.d.ts"
17+
}

tests/baselines/reference/declarationEmitHasTypesRefOnNamespaceUse.js

-6
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,3 @@ var Src = /** @class */ (function () {
1919
}
2020
return Src;
2121
}());
22-
23-
24-
//// [index.d.ts]
25-
/// <reference types="dep" />
26-
declare class Src implements NS.Dep {
27-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
packages/b/src/index.ts(1,1): error TS18055: Declaration file contains synthesized type reference directives: '"@ts-bug/a"'
2+
3+
4+
==== packages/b/tsconfig.json (0 errors) ====
5+
{
6+
"compilerOptions": {
7+
"outDir": "dist",
8+
"declaration": true,
9+
"baseUrl": ".",
10+
"paths": {
11+
"@ts-bug/a": ["../a"]
12+
}
13+
}
14+
}
15+
16+
17+
==== packages/b/src/index.ts (1 errors) ====
18+
import { a } from "@ts-bug/a";
19+
~~~~~~
20+
!!! error TS18055: Declaration file contains synthesized type reference directives: '"@ts-bug/a"'
21+
22+
export function b(text: string) {
23+
return a(text);
24+
}
25+
==== packages/a/index.d.ts (0 errors) ====
26+
declare module "@ts-bug/a" {
27+
export type AText = {
28+
value: string;
29+
};
30+
export function a(text: string): AText;
31+
}
32+

tests/baselines/reference/declarationEmitPathMappingMonorepo.js

-5
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,3 @@ function b(text) {
2424
return (0, a_1.a)(text);
2525
}
2626
exports.b = b;
27-
28-
29-
//// [index.d.ts]
30-
/// <reference types="@ts-bug/a" />
31-
export declare function b(text: string): import("@ts-bug/a").AText;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/usage1.ts(1,1): error TS18055: Declaration file contains synthesized type reference directives: '"node"'
2+
/usage2.ts(1,1): error TS18055: Declaration file contains synthesized type reference directives: '"node"'
3+
/usage3.ts(1,1): error TS18055: Declaration file contains synthesized type reference directives: '"node"'
4+
5+
6+
==== /node_modules/@types/node/index.d.ts (0 errors) ====
7+
declare module "url" {
8+
export class Url {}
9+
export function parse(): Url;
10+
}
11+
12+
==== /usage1.ts (1 errors) ====
13+
export { parse } from "url";
14+
~~~~~~
15+
!!! error TS18055: Declaration file contains synthesized type reference directives: '"node"'
16+
17+
==== /usage2.ts (1 errors) ====
18+
import { parse } from "url";
19+
~~~~~~
20+
!!! error TS18055: Declaration file contains synthesized type reference directives: '"node"'
21+
export const thing: import("url").Url = parse();
22+
23+
==== /usage3.ts (1 errors) ====
24+
import { parse } from "url";
25+
~~~~~~
26+
!!! error TS18055: Declaration file contains synthesized type reference directives: '"node"'
27+
export const thing = parse();
28+

0 commit comments

Comments
 (0)