Skip to content

Commit 1ed30c6

Browse files
authored
Elide type reference comments which are also imported (#22752)
* Elide type reference comments which are also imported * Explicit undefined * Accept reordered symbols
1 parent 1e5641b commit 1ed30c6

File tree

5 files changed

+209
-2
lines changed

5 files changed

+209
-2
lines changed

src/compiler/transformers/declarations.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,30 @@ namespace ts {
191191
refs.forEach(referenceVisitor);
192192
const statements = visitNodes(node.statements, visitDeclarationStatements);
193193
let combinedStatements = setTextRange(createNodeArray(filterCandidateImports(statements)), node.statements);
194+
const emittedImports = filter(combinedStatements, isAnyImportSyntax);
194195
if (isExternalModule(node) && !resultHasExternalModuleIndicator) {
195196
combinedStatements = setTextRange(createNodeArray([...combinedStatements, createExportDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, createNamedExports([]), /*moduleSpecifier*/ undefined)]), combinedStatements);
196197
}
197198
const updated = updateSourceFileNode(node, combinedStatements, /*isDeclarationFile*/ true, references, getFileReferencesForUsedTypeReferences(), node.hasNoDefaultLib);
198199
return updated;
199200

200201
function getFileReferencesForUsedTypeReferences() {
201-
return necessaryTypeRefernces ? map(arrayFrom(necessaryTypeRefernces.keys()), getFileReferenceForTypeName) : [];
202+
return necessaryTypeRefernces ? mapDefined(arrayFrom(necessaryTypeRefernces.keys()), getFileReferenceForTypeName) : [];
202203
}
203204

204-
function getFileReferenceForTypeName(typeName: string): FileReference {
205+
function getFileReferenceForTypeName(typeName: string): FileReference | undefined {
206+
// Elide type references for which we have imports
207+
for (const importStatement of emittedImports) {
208+
if (isImportEqualsDeclaration(importStatement) && isExternalModuleReference(importStatement.moduleReference)) {
209+
const expr = importStatement.moduleReference.expression;
210+
if (isStringLiteralLike(expr) && expr.text === typeName) {
211+
return undefined;
212+
}
213+
}
214+
else if (isImportDeclaration(importStatement) && isStringLiteral(importStatement.moduleSpecifier) && importStatement.moduleSpecifier.text === typeName) {
215+
return undefined;
216+
}
217+
}
205218
return { fileName: typeName, pos: -1, end: -1 };
206219
}
207220

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//// [tests/cases/compiler/declarationEmitUnnessesaryTypeReferenceNotAdded.ts] ////
2+
3+
//// [minimist.d.ts]
4+
declare namespace thing {
5+
interface ParsedArgs {}
6+
}
7+
declare function thing(x: any): thing.ParsedArgs;
8+
export = thing;
9+
//// [package.json]
10+
{
11+
"name": "minimist",
12+
"version": "0.0.1",
13+
"types": "./minimist.d.ts"
14+
}
15+
//// [process.d.ts]
16+
declare const thing: any;
17+
export = thing;
18+
//// [package.json]
19+
{
20+
"name": "process",
21+
"version": "0.0.1",
22+
"types": "./process.d.ts"
23+
}
24+
//// [index.ts]
25+
import minimist = require('minimist');
26+
import process = require('process');
27+
28+
export default function parseArgs(): minimist.ParsedArgs {
29+
return minimist(process.argv.slice(2));
30+
}
31+
32+
33+
//// [index.js]
34+
"use strict";
35+
Object.defineProperty(exports, "__esModule", { value: true });
36+
var minimist = require("minimist");
37+
var process = require("process");
38+
function parseArgs() {
39+
return minimist(process.argv.slice(2));
40+
}
41+
exports.default = parseArgs;
42+
43+
44+
//// [index.d.ts]
45+
import minimist = require('minimist');
46+
export default function parseArgs(): minimist.ParsedArgs;
47+
48+
49+
//// [DtsFileErrors]
50+
51+
52+
error TS2688: Cannot find type definition file for 'minimist'.
53+
error TS2688: Cannot find type definition file for 'process'.
54+
/index.d.ts(1,27): error TS2307: Cannot find module 'minimist'.
55+
56+
57+
!!! error TS2688: Cannot find type definition file for 'minimist'.
58+
!!! error TS2688: Cannot find type definition file for 'process'.
59+
==== /index.d.ts (1 errors) ====
60+
import minimist = require('minimist');
61+
~~~~~~~~~~
62+
!!! error TS2307: Cannot find module 'minimist'.
63+
export default function parseArgs(): minimist.ParsedArgs;
64+
65+
==== /node_modules/@types/minimist/minimist.d.ts (0 errors) ====
66+
declare namespace thing {
67+
interface ParsedArgs {}
68+
}
69+
declare function thing(x: any): thing.ParsedArgs;
70+
export = thing;
71+
==== /node_modules/@types/process/process.d.ts (0 errors) ====
72+
declare const thing: any;
73+
export = thing;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
=== /index.ts ===
2+
import minimist = require('minimist');
3+
>minimist : Symbol(minimist, Decl(index.ts, 0, 0))
4+
5+
import process = require('process');
6+
>process : Symbol(process, Decl(index.ts, 0, 38))
7+
8+
export default function parseArgs(): minimist.ParsedArgs {
9+
>parseArgs : Symbol(parseArgs, Decl(index.ts, 1, 36))
10+
>minimist : Symbol(minimist, Decl(index.ts, 0, 0))
11+
>ParsedArgs : Symbol(minimist.ParsedArgs, Decl(minimist.d.ts, 0, 25))
12+
13+
return minimist(process.argv.slice(2));
14+
>minimist : Symbol(minimist, Decl(index.ts, 0, 0))
15+
>process : Symbol(process, Decl(index.ts, 0, 38))
16+
}
17+
18+
=== /node_modules/@types/minimist/minimist.d.ts ===
19+
declare namespace thing {
20+
>thing : Symbol(thing, Decl(minimist.d.ts, 2, 1), Decl(minimist.d.ts, 0, 0))
21+
22+
interface ParsedArgs {}
23+
>ParsedArgs : Symbol(ParsedArgs, Decl(minimist.d.ts, 0, 25))
24+
}
25+
declare function thing(x: any): thing.ParsedArgs;
26+
>thing : Symbol(thing, Decl(minimist.d.ts, 2, 1), Decl(minimist.d.ts, 0, 0))
27+
>x : Symbol(x, Decl(minimist.d.ts, 3, 23))
28+
>thing : Symbol(thing, Decl(minimist.d.ts, 2, 1), Decl(minimist.d.ts, 0, 0))
29+
>ParsedArgs : Symbol(thing.ParsedArgs, Decl(minimist.d.ts, 0, 25))
30+
31+
export = thing;
32+
>thing : Symbol(thing, Decl(minimist.d.ts, 2, 1), Decl(minimist.d.ts, 0, 0))
33+
34+
=== /node_modules/@types/process/process.d.ts ===
35+
declare const thing: any;
36+
>thing : Symbol(thing, Decl(process.d.ts, 0, 13))
37+
38+
export = thing;
39+
>thing : Symbol(thing, Decl(process.d.ts, 0, 13))
40+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
=== /index.ts ===
2+
import minimist = require('minimist');
3+
>minimist : (x: any) => minimist.ParsedArgs
4+
5+
import process = require('process');
6+
>process : any
7+
8+
export default function parseArgs(): minimist.ParsedArgs {
9+
>parseArgs : () => minimist.ParsedArgs
10+
>minimist : any
11+
>ParsedArgs : minimist.ParsedArgs
12+
13+
return minimist(process.argv.slice(2));
14+
>minimist(process.argv.slice(2)) : minimist.ParsedArgs
15+
>minimist : (x: any) => minimist.ParsedArgs
16+
>process.argv.slice(2) : any
17+
>process.argv.slice : any
18+
>process.argv : any
19+
>process : any
20+
>argv : any
21+
>slice : any
22+
>2 : 2
23+
}
24+
25+
=== /node_modules/@types/minimist/minimist.d.ts ===
26+
declare namespace thing {
27+
>thing : (x: any) => ParsedArgs
28+
29+
interface ParsedArgs {}
30+
>ParsedArgs : ParsedArgs
31+
}
32+
declare function thing(x: any): thing.ParsedArgs;
33+
>thing : (x: any) => thing.ParsedArgs
34+
>x : any
35+
>thing : any
36+
>ParsedArgs : thing.ParsedArgs
37+
38+
export = thing;
39+
>thing : (x: any) => thing.ParsedArgs
40+
41+
=== /node_modules/@types/process/process.d.ts ===
42+
declare const thing: any;
43+
>thing : any
44+
45+
export = thing;
46+
>thing : any
47+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// @declaration: true
2+
// @target: es5
3+
// @module: commonjs
4+
// @strict: true
5+
// @noImplicitReferences: true
6+
// @currentDirectory: /
7+
// @filename: /node_modules/@types/minimist/minimist.d.ts
8+
declare namespace thing {
9+
interface ParsedArgs {}
10+
}
11+
declare function thing(x: any): thing.ParsedArgs;
12+
export = thing;
13+
// @filename: /node_modules/@types/minimist/package.json
14+
{
15+
"name": "minimist",
16+
"version": "0.0.1",
17+
"types": "./minimist.d.ts"
18+
}
19+
// @filename: /node_modules/@types/process/process.d.ts
20+
declare const thing: any;
21+
export = thing;
22+
// @filename: /node_modules/@types/process/package.json
23+
{
24+
"name": "process",
25+
"version": "0.0.1",
26+
"types": "./process.d.ts"
27+
}
28+
// @filename: /index.ts
29+
import minimist = require('minimist');
30+
import process = require('process');
31+
32+
export default function parseArgs(): minimist.ParsedArgs {
33+
return minimist(process.argv.slice(2));
34+
}

0 commit comments

Comments
 (0)