Skip to content

Commit dcd7de1

Browse files
committed
Fix relative paths in commonjs decl emit w/property access
```js const x = require('./foo').y ``` was incorrectly using the unmangled require path as the temp name in emit: ``` import ./foo_1 = require('./foo') import x = ./foo_1.y ``` It now uses the imported identifier: ``` import x_1 = require('./foo') import x = x_1.y ``` Discovered while fixing #37832
1 parent cf3e28e commit dcd7de1

7 files changed

+103
-7
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6788,16 +6788,16 @@ namespace ts {
67886788
if (isPropertyAccessExpression((node as VariableDeclaration).initializer!)) {
67896789
// const x = require('y').z
67906790
const initializer = (node as VariableDeclaration).initializer! as PropertyAccessExpression; // require('y').z
6791-
const uniqueName = factory.createUniqueName((getExternalModuleRequireArgument(node) as StringLiteral).text); // _y
6791+
const uniqueName = factory.createUniqueName(localName); // _x
67926792
const specifier = getSpecifierForModuleSymbol(target.parent || target, context); // 'y'
6793-
// import _y = require('y');
6793+
// import _x = require('y');
67946794
addResult(factory.createImportEqualsDeclaration(
67956795
/*decorators*/ undefined,
67966796
/*modifiers*/ undefined,
67976797
uniqueName,
67986798
factory.createExternalModuleReference(factory.createStringLiteral(specifier))
67996799
), ModifierFlags.None);
6800-
// import x = _y.z
6800+
// import x = _x.z
68016801
addResult(factory.createImportEqualsDeclaration(
68026802
/*decorators*/ undefined,
68036803
/*modifiers*/ undefined,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/conformance/jsdoc/declarations/jsDeclarationsCommonjsRelativePath.ts] ////
2+
3+
//// [thing.js]
4+
'use strict';
5+
class Thing {}
6+
module.exports = { Thing }
7+
8+
//// [reexport.js]
9+
'use strict';
10+
const Thing = require('./thing').Thing
11+
module.exports = { Thing }
12+
13+
14+
15+
16+
//// [thing.d.ts]
17+
export class Thing {
18+
}
19+
//// [reexport.d.ts]
20+
import Thing_1 = require("./thing");
21+
import Thing = Thing_1.Thing;
22+
export { Thing };
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== tests/cases/conformance/jsdoc/declarations/reexport.js ===
2+
'use strict';
3+
const Thing = require('./thing').Thing
4+
>Thing : Symbol(Thing, Decl(reexport.js, 1, 5))
5+
>require('./thing').Thing : Symbol(Thing, Decl(thing.js, 2, 18))
6+
>require : Symbol(require)
7+
>'./thing' : Symbol("tests/cases/conformance/jsdoc/declarations/thing", Decl(thing.js, 0, 0))
8+
>Thing : Symbol(Thing, Decl(thing.js, 2, 18))
9+
10+
module.exports = { Thing }
11+
>module.exports : Symbol("tests/cases/conformance/jsdoc/declarations/reexport", Decl(reexport.js, 0, 0))
12+
>module : Symbol(export=, Decl(reexport.js, 1, 38))
13+
>exports : Symbol(export=, Decl(reexport.js, 1, 38))
14+
>Thing : Symbol(Thing, Decl(reexport.js, 2, 18))
15+
16+
=== tests/cases/conformance/jsdoc/declarations/thing.js ===
17+
'use strict';
18+
class Thing {}
19+
>Thing : Symbol(Thing, Decl(thing.js, 0, 13))
20+
21+
module.exports = { Thing }
22+
>module.exports : Symbol("tests/cases/conformance/jsdoc/declarations/thing", Decl(thing.js, 0, 0))
23+
>module : Symbol(export=, Decl(thing.js, 1, 14))
24+
>exports : Symbol(export=, Decl(thing.js, 1, 14))
25+
>Thing : Symbol(Thing, Decl(thing.js, 2, 18))
26+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
=== tests/cases/conformance/jsdoc/declarations/reexport.js ===
2+
'use strict';
3+
>'use strict' : "use strict"
4+
5+
const Thing = require('./thing').Thing
6+
>Thing : typeof Thing
7+
>require('./thing').Thing : typeof Thing
8+
>require('./thing') : { Thing: typeof Thing; }
9+
>require : any
10+
>'./thing' : "./thing"
11+
>Thing : typeof Thing
12+
13+
module.exports = { Thing }
14+
>module.exports = { Thing } : { Thing: typeof Thing; }
15+
>module.exports : { Thing: typeof Thing; }
16+
>module : { "\"tests/cases/conformance/jsdoc/declarations/reexport\"": { Thing: typeof Thing; }; }
17+
>exports : { Thing: typeof Thing; }
18+
>{ Thing } : { Thing: typeof Thing; }
19+
>Thing : typeof Thing
20+
21+
=== tests/cases/conformance/jsdoc/declarations/thing.js ===
22+
'use strict';
23+
>'use strict' : "use strict"
24+
25+
class Thing {}
26+
>Thing : Thing
27+
28+
module.exports = { Thing }
29+
>module.exports = { Thing } : { Thing: typeof Thing; }
30+
>module.exports : { Thing: typeof Thing; }
31+
>module : { "\"tests/cases/conformance/jsdoc/declarations/thing\"": { Thing: typeof Thing; }; }
32+
>exports : { Thing: typeof Thing; }
33+
>{ Thing } : { Thing: typeof Thing; }
34+
>Thing : typeof Thing
35+

tests/baselines/reference/jsDeclarationsTypeReferences.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ module.exports = {
2828
//// [index.d.ts]
2929
/// <reference types="node" />
3030
export const thing: Something;
31-
import fs_1 = require("fs");
32-
import Something = fs_1.Something;
31+
import Something_1 = require("fs");
32+
import Something = Something_1.Something;

tests/baselines/reference/jsDeclarationsTypeReferences3.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ export namespace A {
3030
const thing: Something;
3131
}
3232
}
33-
import fs_1 = require("fs");
34-
import Something = fs_1.Something;
33+
import Something_1 = require("fs");
34+
import Something = Something_1.Something;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @checkJs: true
2+
// @declaration: true
3+
// @emitDeclarationOnly: true
4+
// @module: commonjs
5+
// @Filename: thing.js
6+
'use strict';
7+
class Thing {}
8+
module.exports = { Thing }
9+
10+
// @Filename: reexport.js
11+
'use strict';
12+
const Thing = require('./thing').Thing
13+
module.exports = { Thing }

0 commit comments

Comments
 (0)