Skip to content

Commit 00d3e60

Browse files
committed
Add allowJs and declaration emit enabled tests
1 parent 2fc2abe commit 00d3e60

File tree

129 files changed

+4023
-1
lines changed

Some content is hidden

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

129 files changed

+4023
-1
lines changed

src/compiler/checker.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -7136,6 +7136,28 @@ namespace ts {
71367136
), symbol.declarations && filter(symbol.declarations, d => isClassDeclaration(d) || isClassExpression(d))[0]), modifierFlags);
71377137
}
71387138

7139+
function getSomeTargetNameFromDeclarations(declarations: Declaration[] | undefined) {
7140+
return firstDefined(declarations, d => {
7141+
if (isImportSpecifier(d) || isExportSpecifier(d)) {
7142+
return idText(d.propertyName || d.name);
7143+
}
7144+
if (isBinaryExpression(d) || isExportAssignment(d)) {
7145+
const expression = isExportAssignment(d) ? d.expression : d.right;
7146+
if (isPropertyAccessExpression(expression)) {
7147+
return idText(expression.name);
7148+
}
7149+
}
7150+
if (isAliasSymbolDeclaration(d)) {
7151+
// This is... heuristic, at best. But it's probably better than always printing the name of the shorthand ambient module.
7152+
const name = getNameOfDeclaration(d);
7153+
if (name && isIdentifier(name)) {
7154+
return idText(name);
7155+
}
7156+
}
7157+
return undefined;
7158+
});
7159+
}
7160+
71397161
function serializeAsAlias(symbol: Symbol, localName: string, modifierFlags: ModifierFlags) {
71407162
// synthesize an alias, eg `export { symbolName as Name }`
71417163
// need to mark the alias `symbol` points at
@@ -7146,7 +7168,9 @@ namespace ts {
71467168
if (!target) {
71477169
return;
71487170
}
7149-
let verbatimTargetName = unescapeLeadingUnderscores(target.escapedName);
7171+
// If `target` refers to a shorthand module symbol, the name we're trying to pull out isn;t recoverable from the target symbol
7172+
// In such a scenario, we must fall back to looking for an alias declaration on `symbol` and pulling the target name from that
7173+
let verbatimTargetName = isShorthandAmbientModuleSymbol(target) && getSomeTargetNameFromDeclarations(symbol.declarations) || unescapeLeadingUnderscores(target.escapedName);
71507174
if (verbatimTargetName === InternalSymbolName.ExportEquals && (getESModuleInterop(compilerOptions) || compilerOptions.allowSyntheticDefaultImports)) {
71517175
// target refers to an `export=` symbol that was hoisted into a synthetic default - rename here to match
71527176
verbatimTargetName = InternalSymbolName.Default;

tests/baselines/reference/nodeModules1(module=node12).js

+14
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,17 @@ export { x };
5656
// esm format file
5757
const x = 1;
5858
export { x };
59+
60+
61+
//// [index.d.ts]
62+
declare const x = 1;
63+
export { x };
64+
//// [index.d.ts]
65+
declare const x = 1;
66+
export { x };
67+
//// [index.d.ts]
68+
declare const x = 1;
69+
export { x };
70+
//// [index.d.ts]
71+
declare const x = 1;
72+
export { x };

tests/baselines/reference/nodeModules1(module=nodenext).js

+14
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,17 @@ export { x };
5656
// esm format file
5757
const x = 1;
5858
export { x };
59+
60+
61+
//// [index.d.ts]
62+
declare const x = 1;
63+
export { x };
64+
//// [index.d.ts]
65+
declare const x = 1;
66+
export { x };
67+
//// [index.d.ts]
68+
declare const x = 1;
69+
export { x };
70+
//// [index.d.ts]
71+
declare const x = 1;
72+
export { x };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//// [tests/cases/conformance/node/allowJs/nodeModulesAllowJs1.ts] ////
2+
3+
//// [index.js]
4+
// cjs format file
5+
const x = 1;
6+
export {x};
7+
//// [index.js]
8+
// cjs format file
9+
const x = 1;
10+
export {x};
11+
//// [index.js]
12+
// esm format file
13+
const x = 1;
14+
export {x};
15+
//// [index.js]
16+
// esm format file
17+
const x = 1;
18+
export {x};
19+
//// [package.json]
20+
{
21+
"name": "package",
22+
"private": true,
23+
"type": "module"
24+
}
25+
//// [package.json]
26+
{
27+
"type": "commonjs"
28+
}
29+
//// [package.json]
30+
{
31+
}
32+
//// [package.json]
33+
{
34+
"type": "module"
35+
}
36+
37+
//// [index.js]
38+
"use strict";
39+
Object.defineProperty(exports, "__esModule", { value: true });
40+
exports.x = void 0;
41+
// cjs format file
42+
const x = 1;
43+
exports.x = x;
44+
//// [index.js]
45+
"use strict";
46+
Object.defineProperty(exports, "__esModule", { value: true });
47+
exports.x = void 0;
48+
// cjs format file
49+
const x = 1;
50+
exports.x = x;
51+
//// [index.js]
52+
// esm format file
53+
const x = 1;
54+
export { x };
55+
//// [index.js]
56+
// esm format file
57+
const x = 1;
58+
export { x };
59+
60+
61+
//// [index.d.ts]
62+
export const x: 1;
63+
//// [index.d.ts]
64+
export const x: 1;
65+
//// [index.d.ts]
66+
export const x: 1;
67+
//// [index.d.ts]
68+
export const x: 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== tests/cases/conformance/node/allowJs/subfolder/index.js ===
2+
// cjs format file
3+
const x = 1;
4+
>x : Symbol(x, Decl(index.js, 1, 5))
5+
6+
export {x};
7+
>x : Symbol(x, Decl(index.js, 2, 8))
8+
9+
=== tests/cases/conformance/node/allowJs/subfolder2/index.js ===
10+
// cjs format file
11+
const x = 1;
12+
>x : Symbol(x, Decl(index.js, 1, 5))
13+
14+
export {x};
15+
>x : Symbol(x, Decl(index.js, 2, 8))
16+
17+
=== tests/cases/conformance/node/allowJs/subfolder2/another/index.js ===
18+
// esm format file
19+
const x = 1;
20+
>x : Symbol(x, Decl(index.js, 1, 5))
21+
22+
export {x};
23+
>x : Symbol(x, Decl(index.js, 2, 8))
24+
25+
=== tests/cases/conformance/node/allowJs/index.js ===
26+
// esm format file
27+
const x = 1;
28+
>x : Symbol(x, Decl(index.js, 1, 5))
29+
30+
export {x};
31+
>x : Symbol(x, Decl(index.js, 2, 8))
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=== tests/cases/conformance/node/allowJs/subfolder/index.js ===
2+
// cjs format file
3+
const x = 1;
4+
>x : 1
5+
>1 : 1
6+
7+
export {x};
8+
>x : 1
9+
10+
=== tests/cases/conformance/node/allowJs/subfolder2/index.js ===
11+
// cjs format file
12+
const x = 1;
13+
>x : 1
14+
>1 : 1
15+
16+
export {x};
17+
>x : 1
18+
19+
=== tests/cases/conformance/node/allowJs/subfolder2/another/index.js ===
20+
// esm format file
21+
const x = 1;
22+
>x : 1
23+
>1 : 1
24+
25+
export {x};
26+
>x : 1
27+
28+
=== tests/cases/conformance/node/allowJs/index.js ===
29+
// esm format file
30+
const x = 1;
31+
>x : 1
32+
>1 : 1
33+
34+
export {x};
35+
>x : 1
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//// [tests/cases/conformance/node/allowJs/nodeModulesAllowJs1.ts] ////
2+
3+
//// [index.js]
4+
// cjs format file
5+
const x = 1;
6+
export {x};
7+
//// [index.js]
8+
// cjs format file
9+
const x = 1;
10+
export {x};
11+
//// [index.js]
12+
// esm format file
13+
const x = 1;
14+
export {x};
15+
//// [index.js]
16+
// esm format file
17+
const x = 1;
18+
export {x};
19+
//// [package.json]
20+
{
21+
"name": "package",
22+
"private": true,
23+
"type": "module"
24+
}
25+
//// [package.json]
26+
{
27+
"type": "commonjs"
28+
}
29+
//// [package.json]
30+
{
31+
}
32+
//// [package.json]
33+
{
34+
"type": "module"
35+
}
36+
37+
//// [index.js]
38+
"use strict";
39+
Object.defineProperty(exports, "__esModule", { value: true });
40+
exports.x = void 0;
41+
// cjs format file
42+
const x = 1;
43+
exports.x = x;
44+
//// [index.js]
45+
"use strict";
46+
Object.defineProperty(exports, "__esModule", { value: true });
47+
exports.x = void 0;
48+
// cjs format file
49+
const x = 1;
50+
exports.x = x;
51+
//// [index.js]
52+
// esm format file
53+
const x = 1;
54+
export { x };
55+
//// [index.js]
56+
// esm format file
57+
const x = 1;
58+
export { x };
59+
60+
61+
//// [index.d.ts]
62+
export const x: 1;
63+
//// [index.d.ts]
64+
export const x: 1;
65+
//// [index.d.ts]
66+
export const x: 1;
67+
//// [index.d.ts]
68+
export const x: 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== tests/cases/conformance/node/allowJs/subfolder/index.js ===
2+
// cjs format file
3+
const x = 1;
4+
>x : Symbol(x, Decl(index.js, 1, 5))
5+
6+
export {x};
7+
>x : Symbol(x, Decl(index.js, 2, 8))
8+
9+
=== tests/cases/conformance/node/allowJs/subfolder2/index.js ===
10+
// cjs format file
11+
const x = 1;
12+
>x : Symbol(x, Decl(index.js, 1, 5))
13+
14+
export {x};
15+
>x : Symbol(x, Decl(index.js, 2, 8))
16+
17+
=== tests/cases/conformance/node/allowJs/subfolder2/another/index.js ===
18+
// esm format file
19+
const x = 1;
20+
>x : Symbol(x, Decl(index.js, 1, 5))
21+
22+
export {x};
23+
>x : Symbol(x, Decl(index.js, 2, 8))
24+
25+
=== tests/cases/conformance/node/allowJs/index.js ===
26+
// esm format file
27+
const x = 1;
28+
>x : Symbol(x, Decl(index.js, 1, 5))
29+
30+
export {x};
31+
>x : Symbol(x, Decl(index.js, 2, 8))
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=== tests/cases/conformance/node/allowJs/subfolder/index.js ===
2+
// cjs format file
3+
const x = 1;
4+
>x : 1
5+
>1 : 1
6+
7+
export {x};
8+
>x : 1
9+
10+
=== tests/cases/conformance/node/allowJs/subfolder2/index.js ===
11+
// cjs format file
12+
const x = 1;
13+
>x : 1
14+
>1 : 1
15+
16+
export {x};
17+
>x : 1
18+
19+
=== tests/cases/conformance/node/allowJs/subfolder2/another/index.js ===
20+
// esm format file
21+
const x = 1;
22+
>x : 1
23+
>1 : 1
24+
25+
export {x};
26+
>x : 1
27+
28+
=== tests/cases/conformance/node/allowJs/index.js ===
29+
// esm format file
30+
const x = 1;
31+
>x : 1
32+
>1 : 1
33+
34+
export {x};
35+
>x : 1
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//// [tests/cases/conformance/node/allowJs/nodeModulesAllowJsDynamicImport.ts] ////
2+
3+
//// [index.js]
4+
// cjs format file
5+
export async function main() {
6+
const { readFile } = await import("fs");
7+
}
8+
//// [index.js]
9+
// esm format file
10+
export async function main() {
11+
const { readFile } = await import("fs");
12+
}
13+
//// [package.json]
14+
{
15+
"name": "package",
16+
"private": true,
17+
"type": "module"
18+
}
19+
//// [package.json]
20+
{
21+
"type": "commonjs"
22+
}
23+
//// [types.d.ts]
24+
declare module "fs";
25+
26+
//// [index.js]
27+
"use strict";
28+
Object.defineProperty(exports, "__esModule", { value: true });
29+
exports.main = void 0;
30+
// cjs format file
31+
async function main() {
32+
const { readFile } = await import("fs");
33+
}
34+
exports.main = main;
35+
//// [index.js]
36+
// esm format file
37+
export async function main() {
38+
const { readFile } = await import("fs");
39+
}
40+
41+
42+
//// [index.d.ts]
43+
export function main(): Promise<void>;
44+
//// [index.d.ts]
45+
export function main(): Promise<void>;

0 commit comments

Comments
 (0)