Skip to content

Commit

Permalink
Add allowJs and declaration emit enabled tests
Browse files Browse the repository at this point in the history
  • Loading branch information
weswigham committed Jun 8, 2021
1 parent 2fc2abe commit 00d3e60
Show file tree
Hide file tree
Showing 129 changed files with 4,023 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7136,6 +7136,28 @@ namespace ts {
), symbol.declarations && filter(symbol.declarations, d => isClassDeclaration(d) || isClassExpression(d))[0]), modifierFlags);
}

function getSomeTargetNameFromDeclarations(declarations: Declaration[] | undefined) {
return firstDefined(declarations, d => {
if (isImportSpecifier(d) || isExportSpecifier(d)) {
return idText(d.propertyName || d.name);
}
if (isBinaryExpression(d) || isExportAssignment(d)) {
const expression = isExportAssignment(d) ? d.expression : d.right;
if (isPropertyAccessExpression(expression)) {
return idText(expression.name);
}
}
if (isAliasSymbolDeclaration(d)) {
// This is... heuristic, at best. But it's probably better than always printing the name of the shorthand ambient module.
const name = getNameOfDeclaration(d);
if (name && isIdentifier(name)) {
return idText(name);
}
}
return undefined;
});
}

function serializeAsAlias(symbol: Symbol, localName: string, modifierFlags: ModifierFlags) {
// synthesize an alias, eg `export { symbolName as Name }`
// need to mark the alias `symbol` points at
Expand All @@ -7146,7 +7168,9 @@ namespace ts {
if (!target) {
return;
}
let verbatimTargetName = unescapeLeadingUnderscores(target.escapedName);
// If `target` refers to a shorthand module symbol, the name we're trying to pull out isn;t recoverable from the target symbol
// In such a scenario, we must fall back to looking for an alias declaration on `symbol` and pulling the target name from that
let verbatimTargetName = isShorthandAmbientModuleSymbol(target) && getSomeTargetNameFromDeclarations(symbol.declarations) || unescapeLeadingUnderscores(target.escapedName);
if (verbatimTargetName === InternalSymbolName.ExportEquals && (getESModuleInterop(compilerOptions) || compilerOptions.allowSyntheticDefaultImports)) {
// target refers to an `export=` symbol that was hoisted into a synthetic default - rename here to match
verbatimTargetName = InternalSymbolName.Default;
Expand Down
14 changes: 14 additions & 0 deletions tests/baselines/reference/nodeModules1(module=node12).js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,17 @@ export { x };
// esm format file
const x = 1;
export { x };


//// [index.d.ts]
declare const x = 1;
export { x };
//// [index.d.ts]
declare const x = 1;
export { x };
//// [index.d.ts]
declare const x = 1;
export { x };
//// [index.d.ts]
declare const x = 1;
export { x };
14 changes: 14 additions & 0 deletions tests/baselines/reference/nodeModules1(module=nodenext).js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,17 @@ export { x };
// esm format file
const x = 1;
export { x };


//// [index.d.ts]
declare const x = 1;
export { x };
//// [index.d.ts]
declare const x = 1;
export { x };
//// [index.d.ts]
declare const x = 1;
export { x };
//// [index.d.ts]
declare const x = 1;
export { x };
68 changes: 68 additions & 0 deletions tests/baselines/reference/nodeModulesAllowJs1(module=node12).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//// [tests/cases/conformance/node/allowJs/nodeModulesAllowJs1.ts] ////

//// [index.js]
// cjs format file
const x = 1;
export {x};
//// [index.js]
// cjs format file
const x = 1;
export {x};
//// [index.js]
// esm format file
const x = 1;
export {x};
//// [index.js]
// esm format file
const x = 1;
export {x};
//// [package.json]
{
"name": "package",
"private": true,
"type": "module"
}
//// [package.json]
{
"type": "commonjs"
}
//// [package.json]
{
}
//// [package.json]
{
"type": "module"
}

//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.x = void 0;
// cjs format file
const x = 1;
exports.x = x;
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.x = void 0;
// cjs format file
const x = 1;
exports.x = x;
//// [index.js]
// esm format file
const x = 1;
export { x };
//// [index.js]
// esm format file
const x = 1;
export { x };


//// [index.d.ts]
export const x: 1;
//// [index.d.ts]
export const x: 1;
//// [index.d.ts]
export const x: 1;
//// [index.d.ts]
export const x: 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== tests/cases/conformance/node/allowJs/subfolder/index.js ===
// cjs format file
const x = 1;
>x : Symbol(x, Decl(index.js, 1, 5))

export {x};
>x : Symbol(x, Decl(index.js, 2, 8))

=== tests/cases/conformance/node/allowJs/subfolder2/index.js ===
// cjs format file
const x = 1;
>x : Symbol(x, Decl(index.js, 1, 5))

export {x};
>x : Symbol(x, Decl(index.js, 2, 8))

=== tests/cases/conformance/node/allowJs/subfolder2/another/index.js ===
// esm format file
const x = 1;
>x : Symbol(x, Decl(index.js, 1, 5))

export {x};
>x : Symbol(x, Decl(index.js, 2, 8))

=== tests/cases/conformance/node/allowJs/index.js ===
// esm format file
const x = 1;
>x : Symbol(x, Decl(index.js, 1, 5))

export {x};
>x : Symbol(x, Decl(index.js, 2, 8))

36 changes: 36 additions & 0 deletions tests/baselines/reference/nodeModulesAllowJs1(module=node12).types
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
=== tests/cases/conformance/node/allowJs/subfolder/index.js ===
// cjs format file
const x = 1;
>x : 1
>1 : 1

export {x};
>x : 1

=== tests/cases/conformance/node/allowJs/subfolder2/index.js ===
// cjs format file
const x = 1;
>x : 1
>1 : 1

export {x};
>x : 1

=== tests/cases/conformance/node/allowJs/subfolder2/another/index.js ===
// esm format file
const x = 1;
>x : 1
>1 : 1

export {x};
>x : 1

=== tests/cases/conformance/node/allowJs/index.js ===
// esm format file
const x = 1;
>x : 1
>1 : 1

export {x};
>x : 1

68 changes: 68 additions & 0 deletions tests/baselines/reference/nodeModulesAllowJs1(module=nodenext).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//// [tests/cases/conformance/node/allowJs/nodeModulesAllowJs1.ts] ////

//// [index.js]
// cjs format file
const x = 1;
export {x};
//// [index.js]
// cjs format file
const x = 1;
export {x};
//// [index.js]
// esm format file
const x = 1;
export {x};
//// [index.js]
// esm format file
const x = 1;
export {x};
//// [package.json]
{
"name": "package",
"private": true,
"type": "module"
}
//// [package.json]
{
"type": "commonjs"
}
//// [package.json]
{
}
//// [package.json]
{
"type": "module"
}

//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.x = void 0;
// cjs format file
const x = 1;
exports.x = x;
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.x = void 0;
// cjs format file
const x = 1;
exports.x = x;
//// [index.js]
// esm format file
const x = 1;
export { x };
//// [index.js]
// esm format file
const x = 1;
export { x };


//// [index.d.ts]
export const x: 1;
//// [index.d.ts]
export const x: 1;
//// [index.d.ts]
export const x: 1;
//// [index.d.ts]
export const x: 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== tests/cases/conformance/node/allowJs/subfolder/index.js ===
// cjs format file
const x = 1;
>x : Symbol(x, Decl(index.js, 1, 5))

export {x};
>x : Symbol(x, Decl(index.js, 2, 8))

=== tests/cases/conformance/node/allowJs/subfolder2/index.js ===
// cjs format file
const x = 1;
>x : Symbol(x, Decl(index.js, 1, 5))

export {x};
>x : Symbol(x, Decl(index.js, 2, 8))

=== tests/cases/conformance/node/allowJs/subfolder2/another/index.js ===
// esm format file
const x = 1;
>x : Symbol(x, Decl(index.js, 1, 5))

export {x};
>x : Symbol(x, Decl(index.js, 2, 8))

=== tests/cases/conformance/node/allowJs/index.js ===
// esm format file
const x = 1;
>x : Symbol(x, Decl(index.js, 1, 5))

export {x};
>x : Symbol(x, Decl(index.js, 2, 8))

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
=== tests/cases/conformance/node/allowJs/subfolder/index.js ===
// cjs format file
const x = 1;
>x : 1
>1 : 1

export {x};
>x : 1

=== tests/cases/conformance/node/allowJs/subfolder2/index.js ===
// cjs format file
const x = 1;
>x : 1
>1 : 1

export {x};
>x : 1

=== tests/cases/conformance/node/allowJs/subfolder2/another/index.js ===
// esm format file
const x = 1;
>x : 1
>1 : 1

export {x};
>x : 1

=== tests/cases/conformance/node/allowJs/index.js ===
// esm format file
const x = 1;
>x : 1
>1 : 1

export {x};
>x : 1

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//// [tests/cases/conformance/node/allowJs/nodeModulesAllowJsDynamicImport.ts] ////

//// [index.js]
// cjs format file
export async function main() {
const { readFile } = await import("fs");
}
//// [index.js]
// esm format file
export async function main() {
const { readFile } = await import("fs");
}
//// [package.json]
{
"name": "package",
"private": true,
"type": "module"
}
//// [package.json]
{
"type": "commonjs"
}
//// [types.d.ts]
declare module "fs";

//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.main = void 0;
// cjs format file
async function main() {
const { readFile } = await import("fs");
}
exports.main = main;
//// [index.js]
// esm format file
export async function main() {
const { readFile } = await import("fs");
}


//// [index.d.ts]
export function main(): Promise<void>;
//// [index.d.ts]
export function main(): Promise<void>;
Loading

0 comments on commit 00d3e60

Please sign in to comment.