Skip to content

Fix relative paths in declaration emit for commonjs imports with trailing property access #40986

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

Merged
merged 1 commit into from
Oct 9, 2020
Merged
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
6 changes: 3 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6788,16 +6788,16 @@ namespace ts {
if (isPropertyAccessExpression((node as VariableDeclaration).initializer!)) {
// const x = require('y').z
const initializer = (node as VariableDeclaration).initializer! as PropertyAccessExpression; // require('y').z
const uniqueName = factory.createUniqueName((getExternalModuleRequireArgument(node) as StringLiteral).text); // _y
const uniqueName = factory.createUniqueName(localName); // _x
const specifier = getSpecifierForModuleSymbol(target.parent || target, context); // 'y'
// import _y = require('y');
// import _x = require('y');
addResult(factory.createImportEqualsDeclaration(
/*decorators*/ undefined,
/*modifiers*/ undefined,
uniqueName,
factory.createExternalModuleReference(factory.createStringLiteral(specifier))
), ModifierFlags.None);
// import x = _y.z
// import x = _x.z
addResult(factory.createImportEqualsDeclaration(
/*decorators*/ undefined,
/*modifiers*/ undefined,
Expand Down
22 changes: 22 additions & 0 deletions tests/baselines/reference/jsDeclarationsCommonjsRelativePath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [tests/cases/conformance/jsdoc/declarations/jsDeclarationsCommonjsRelativePath.ts] ////

//// [thing.js]
'use strict';
class Thing {}
module.exports = { Thing }

//// [reexport.js]
'use strict';
const Thing = require('./thing').Thing
module.exports = { Thing }




//// [thing.d.ts]
export class Thing {
}
//// [reexport.d.ts]
import Thing_1 = require("./thing");
import Thing = Thing_1.Thing;
export { Thing };
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
=== tests/cases/conformance/jsdoc/declarations/reexport.js ===
'use strict';
const Thing = require('./thing').Thing
>Thing : Symbol(Thing, Decl(reexport.js, 1, 5))
>require('./thing').Thing : Symbol(Thing, Decl(thing.js, 2, 18))
>require : Symbol(require)
>'./thing' : Symbol("tests/cases/conformance/jsdoc/declarations/thing", Decl(thing.js, 0, 0))
>Thing : Symbol(Thing, Decl(thing.js, 2, 18))

module.exports = { Thing }
>module.exports : Symbol("tests/cases/conformance/jsdoc/declarations/reexport", Decl(reexport.js, 0, 0))
>module : Symbol(export=, Decl(reexport.js, 1, 38))
>exports : Symbol(export=, Decl(reexport.js, 1, 38))
>Thing : Symbol(Thing, Decl(reexport.js, 2, 18))

=== tests/cases/conformance/jsdoc/declarations/thing.js ===
'use strict';
class Thing {}
>Thing : Symbol(Thing, Decl(thing.js, 0, 13))

module.exports = { Thing }
>module.exports : Symbol("tests/cases/conformance/jsdoc/declarations/thing", Decl(thing.js, 0, 0))
>module : Symbol(export=, Decl(thing.js, 1, 14))
>exports : Symbol(export=, Decl(thing.js, 1, 14))
>Thing : Symbol(Thing, Decl(thing.js, 2, 18))

35 changes: 35 additions & 0 deletions tests/baselines/reference/jsDeclarationsCommonjsRelativePath.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
=== tests/cases/conformance/jsdoc/declarations/reexport.js ===
'use strict';
>'use strict' : "use strict"

const Thing = require('./thing').Thing
>Thing : typeof Thing
>require('./thing').Thing : typeof Thing
>require('./thing') : { Thing: typeof Thing; }
>require : any
>'./thing' : "./thing"
>Thing : typeof Thing

module.exports = { Thing }
>module.exports = { Thing } : { Thing: typeof Thing; }
>module.exports : { Thing: typeof Thing; }
>module : { "\"tests/cases/conformance/jsdoc/declarations/reexport\"": { Thing: typeof Thing; }; }
>exports : { Thing: typeof Thing; }
>{ Thing } : { Thing: typeof Thing; }
>Thing : typeof Thing

=== tests/cases/conformance/jsdoc/declarations/thing.js ===
'use strict';
>'use strict' : "use strict"

class Thing {}
>Thing : Thing

module.exports = { Thing }
>module.exports = { Thing } : { Thing: typeof Thing; }
>module.exports : { Thing: typeof Thing; }
>module : { "\"tests/cases/conformance/jsdoc/declarations/thing\"": { Thing: typeof Thing; }; }
>exports : { Thing: typeof Thing; }
>{ Thing } : { Thing: typeof Thing; }
>Thing : typeof Thing

4 changes: 2 additions & 2 deletions tests/baselines/reference/jsDeclarationsTypeReferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ module.exports = {
//// [index.d.ts]
/// <reference types="node" />
export const thing: Something;
import fs_1 = require("fs");
import Something = fs_1.Something;
import Something_1 = require("fs");
import Something = Something_1.Something;
4 changes: 2 additions & 2 deletions tests/baselines/reference/jsDeclarationsTypeReferences3.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ export namespace A {
const thing: Something;
}
}
import fs_1 = require("fs");
import Something = fs_1.Something;
import Something_1 = require("fs");
import Something = Something_1.Something;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @checkJs: true
// @declaration: true
// @emitDeclarationOnly: true
// @module: commonjs
// @Filename: thing.js
'use strict';
class Thing {}
module.exports = { Thing }

// @Filename: reexport.js
'use strict';
const Thing = require('./thing').Thing
module.exports = { Thing }