Skip to content

Commit

Permalink
Show synthesized triple slash references in diagnostics for analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Feb 27, 2024
1 parent df8d755 commit 2b69c9f
Show file tree
Hide file tree
Showing 499 changed files with 7,786 additions and 1,074 deletions.
9 changes: 9 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -7980,5 +7980,14 @@
"'await using' statements cannot be used inside a class static block.": {
"category": "Error",
"code": 18054
},

"Declaration file contains synthesized type reference directives: '{0}'": {
"category": "Error",
"code": 18055
},
"Declaration file contains synthesized 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 @@ -53,6 +53,7 @@ import {
FunctionTypeNode,
GeneratedIdentifierFlags,
GetAccessorDeclaration,
getBaseFileName,
getCommentRange,
getDirectoryPath,
getEffectiveBaseTypeNode,
Expand All @@ -63,6 +64,7 @@ import {
getLineAndCharacterOfPosition,
getNameOfDeclaration,
getNormalizedAbsolutePath,
getNormalizedAbsolutePathWithoutRoot,
getOriginalNodeId,
getOutputPathsFor,
getParseTreeNode,
Expand Down Expand Up @@ -547,7 +549,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 synthesizedTypeReferences = typeReferences.filter(ref => !node.typeReferenceDirectives.some(d => d.fileName === ref.fileName));
const synthesizedFileReferences = references.filter(ref => !node.referencedFiles.some(d => getNormalizedAbsolutePathWithoutRoot(getBaseFileName(d.fileName), "") === getNormalizedAbsolutePathWithoutRoot(getBaseFileName(ref.fileName), "")));
if (synthesizedTypeReferences.length) {
context.addDiagnostic(createDiagnosticForNode(node, Diagnostics.Declaration_file_contains_synthesized_type_reference_directives_Colon_0, synthesizedTypeReferences.map(ref => `"${ref.fileName}"`).join(", ")));
}
if (synthesizedFileReferences.length) {
context.addDiagnostic(createDiagnosticForNode(node, Diagnostics.Declaration_file_contains_synthesized_file_reference_directives_Colon_0, synthesizedFileReferences.map(ref => `"${ref.fileName}"`).join(", ")));
}
updated.exportedModulesFromDeclarationEmit = exportedModulesFromDeclarationEmit;
return updated;

Expand Down
34 changes: 34 additions & 0 deletions tests/baselines/reference/amdLikeInputDeclarationEmit.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
ExtendedClass.js(1,1): error TS18056: Declaration file contains synthesized file reference directives: '"../deps/BaseClass.d.ts"'


==== typing.d.ts (0 errors) ====
declare function define<T=unknown>(name: string, modules: string[], ready: (...modules: unknown[]) => T);
==== deps/BaseClass.d.ts (0 errors) ====
declare module "deps/BaseClass" {
class BaseClass {
static extends<A>(a: A): new () => A & BaseClass;
}
export = BaseClass;
}
==== ExtendedClass.js (1 errors) ====
define("lib/ExtendedClass", ["deps/BaseClass"],
~~~~~~
!!! error TS18056: Declaration file contains synthesized file reference directives: '"../deps/BaseClass.d.ts"'
/**
* {typeof import("deps/BaseClass")}
* @param {typeof import("deps/BaseClass")} BaseClass
* @returns
*/
(BaseClass) => {

const ExtendedClass = BaseClass.extends({
f: function() {
return "something";
}
});

// Exports the module in a way tsc recognize class export
const module = {};
module.exports = ExtendedClass
return module.exports;
});
40 changes: 0 additions & 40 deletions tests/baselines/reference/amdLikeInputDeclarationEmit.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
declFileAmbientExternalModuleWithSingleExportedModule_1.ts(2,1): error TS18056: Declaration file contains synthesized file reference directives: '"declFileAmbientExternalModuleWithSingleExportedModule_0.d.ts"'


==== declFileAmbientExternalModuleWithSingleExportedModule_1.ts (1 errors) ====
///<reference path='declFileAmbientExternalModuleWithSingleExportedModule_0.ts'/>
import SubModule = require('SubModule');
~~~~~~
!!! error TS18056: Declaration file contains synthesized file reference directives: '"declFileAmbientExternalModuleWithSingleExportedModule_0.d.ts"'
export var x: SubModule.m.m3.c;


==== declFileAmbientExternalModuleWithSingleExportedModule_0.ts (0 errors) ====
declare module "SubModule" {
export module m {
export module m3 {
interface c {
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,3 @@ declare module "SubModule" {
}
}
}
//// [declFileAmbientExternalModuleWithSingleExportedModule_1.d.ts]
/// <reference path="declFileAmbientExternalModuleWithSingleExportedModule_0.d.ts" />
import SubModule = require('SubModule');
export declare var x: SubModule.m.m3.c;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
src/datastore_result.ts(1,1): error TS18056: Declaration file contains synthesized file reference directives: '"../lib/lib.d.ts"'


==== lib/lib.d.ts (0 errors) ====
declare module "lib/result" {
export type Result<E extends Error, T> = (E & Failure<E>) | (T & Success<T>);
export interface Failure<E extends Error> { }
export interface Success<T> { }
}

==== src/datastore_result.ts (1 errors) ====
import { Result } from "lib/result";
~~~~~~
!!! error TS18056: Declaration file contains synthesized file reference directives: '"../lib/lib.d.ts"'

export type T<T> = Result<Error, T>;

==== src/conditional_directive_field.ts (0 errors) ====
import * as DatastoreResult from "src/datastore_result";

export const build = (): DatastoreResult.T<string> => {
return null;
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
packages/secondpackage/index.ts(1,1): error TS18056: Declaration file contains synthesized file reference directives: '"../../types/component.d.ts"'


==== types/component.d.ts (0 errors) ====
declare module '@namespace/component' {
export class Foo {}
}
==== packages/somepackage/index.d.ts (0 errors) ====
import { Foo } from "@namespace/component";
export declare const item: typeof Foo;
==== packages/secondpackage/index.ts (1 errors) ====
import { item } from "../somepackage";
~~~~~~
!!! error TS18056: Declaration file contains synthesized file reference directives: '"../../types/component.d.ts"'
export const reeexported = item;

Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,3 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.reeexported = void 0;
var somepackage_1 = require("../somepackage");
exports.reeexported = somepackage_1.item;


//// [index.d.ts]
/// <reference path="../../types/component.d.ts" />
export declare const reeexported: typeof import("@namespace/component").Foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
src/inferred-comp-export.ts(1,1): error TS18055: Declaration file contains synthesized type reference directives: '"react"'


==== node_modules/@types/react/index.d.ts (0 errors) ====
export = React;

declare namespace React {
export type Component<T = any, U = {}, V = {}> = { x: T, y: U, z: V };
export interface DOMAttributes<T> { }
}
==== node_modules/@emotion/core/index.d.ts (0 errors) ====
import {
Component
} from 'react'
export {};

declare module 'react' {
interface DOMAttributes<T> {
css?: any
}
}

==== src/get-comp.ts (0 errors) ====
import {Component} from 'react';

export function getComp(): Component {
return {} as any as Component
}
==== src/inferred-comp-export.ts (1 errors) ====
import { getComp } from "./get-comp";
~~~~~~
!!! error TS18055: Declaration file contains synthesized type reference directives: '"react"'

// this shouldn't need any triple-slash references - it should have a direct import to `react` and that's it
// This issue (#35343) _only_ reproduces in the test harness when the file in question is in a subfolder
export const obj = {
comp: getComp()
}
==== src/some-other-file.ts (0 errors) ====
export * from '@emotion/core';

Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,5 @@ __exportStar(require("@emotion/core"), exports);
//// [get-comp.d.ts]
import { Component } from 'react';
export declare function getComp(): Component;
//// [inferred-comp-export.d.ts]
/// <reference types="react" />
export declare const obj: {
comp: import("react").Component;
};
//// [some-other-file.d.ts]
export * from '@emotion/core';
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
export {};

declare module 'react' {
>'react' : error
>'react' : any

interface DOMAttributes<T> {
css?: any
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/src/index.ts(1,1): error TS18055: Declaration file contains synthesized type reference directives: '"dep"'


==== /src/index.ts (1 errors) ====
class Src implements NS.Dep { }
~~~~~
!!! error TS18055: Declaration file contains synthesized type reference directives: '"dep"'

==== /deps/dep/dep.d.ts (0 errors) ====
declare namespace NS {
interface Dep {
}
}
==== /deps/dep/package.json (0 errors) ====
{
"typings": "dep.d.ts"
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,3 @@ var Src = /** @class */ (function () {
}
return Src;
}());


//// [index.d.ts]
/// <reference types="dep" />
declare class Src implements NS.Dep {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
packages/b/src/index.ts(1,1): error TS18055: Declaration file contains synthesized type reference directives: '"@ts-bug/a"'


==== packages/b/tsconfig.json (0 errors) ====
{
"compilerOptions": {
"outDir": "dist",
"declaration": true,
"baseUrl": ".",
"paths": {
"@ts-bug/a": ["../a"]
}
}
}


==== packages/b/src/index.ts (1 errors) ====
import { a } from "@ts-bug/a";
~~~~~~
!!! error TS18055: Declaration file contains synthesized type reference directives: '"@ts-bug/a"'

export function b(text: string) {
return a(text);
}
==== packages/a/index.d.ts (0 errors) ====
declare module "@ts-bug/a" {
export type AText = {
value: string;
};
export function a(text: string): AText;
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,3 @@ function b(text) {
return (0, a_1.a)(text);
}
exports.b = b;


//// [index.d.ts]
/// <reference types="@ts-bug/a" />
export declare function b(text: string): import("@ts-bug/a").AText;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/usage1.ts(1,1): error TS18055: Declaration file contains synthesized type reference directives: '"node"'
/usage2.ts(1,1): error TS18055: Declaration file contains synthesized type reference directives: '"node"'
/usage3.ts(1,1): error TS18055: Declaration file contains synthesized type reference directives: '"node"'


==== /node_modules/@types/node/index.d.ts (0 errors) ====
declare module "url" {
export class Url {}
export function parse(): Url;
}

==== /usage1.ts (1 errors) ====
export { parse } from "url";
~~~~~~
!!! error TS18055: Declaration file contains synthesized type reference directives: '"node"'

==== /usage2.ts (1 errors) ====
import { parse } from "url";
~~~~~~
!!! error TS18055: Declaration file contains synthesized type reference directives: '"node"'
export const thing: import("url").Url = parse();

==== /usage3.ts (1 errors) ====
import { parse } from "url";
~~~~~~
!!! error TS18055: Declaration file contains synthesized type reference directives: '"node"'
export const thing = parse();

Loading

0 comments on commit 2b69c9f

Please sign in to comment.