Skip to content
Open
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
27 changes: 27 additions & 0 deletions tsc/internal/checker/emitresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,33 @@ func (r *EmitResolver) IsExpandoFunctionDeclaration(node *ast.Node) bool {
return r.IsExpandoFunctionDeclarationUnsafe(node)
}

// CanDeclareExpandoAssignmentAsAlias reports whether an expando assignment `host.name = id` can be declared
// as `export { id as name }` in a namespace declared at location. That holds when the assignment is the only
// declaration of the member, `id` resolves to the same symbol at location, and the declared type of that
// symbol is the type of the member (it isn't narrowed, widened from a literal, or reassigned).
func (r *EmitResolver) CanDeclareExpandoAssignmentAsAlias(node *ast.Node, location *ast.Node) bool {
if !ast.IsParseTreeNode(node) || !ast.IsBinaryExpression(node) || !ast.IsIdentifier(node.AsBinaryExpression().Right) {
return false
}
r.checkerMu.Lock()
defer r.checkerMu.Unlock()
c := r.checker
member := node.Symbol()
if member == nil || len(member.Declarations()) != 1 {
return false
}
right := node.AsBinaryExpression().Right
target := c.getResolvedSymbol(right)
if target == c.unknownSymbol {
return false
}
if c.resolveName(location, right.Text(), ast.SymbolFlagsValue|ast.SymbolFlagsExportValue, nil /*nameNotFoundMessage*/, false /*isUse*/, false /*excludeGlobals*/) != target {
return false
}
// An exported declaration resolves to its local symbol, which only carries ExportValue
return c.isTypeIdenticalTo(c.getTypeOfSymbol(member), c.getTypeOfSymbol(c.getExportSymbolOfValueSymbolIfExported(target)))
}

func (r *EmitResolver) isSymbolAccessible(symbol *ast.Symbol, enclosingDeclaration *ast.Node, meaning ast.SymbolFlags, shouldComputeAliasToMarkVisible bool) printer.SymbolAccessibilityResult {
return r.checker.IsSymbolAccessible(symbol, enclosingDeclaration, meaning, shouldComputeAliasToMarkVisible)
}
Expand Down
1 change: 1 addition & 0 deletions tsc/internal/printer/emitresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ type EmitResolver interface {
IsEntityNameVisible(entityName *ast.Node, enclosingDeclaration *ast.Node) SymbolAccessibilityResult // previously SymbolVisibilityResult in strada - ErrorModuleName never set
IsExpandoFunctionDeclaration(node *ast.Node) bool
IsExpandoFunctionDeclarationUnsafe(node *ast.Node) bool
CanDeclareExpandoAssignmentAsAlias(node *ast.Node, location *ast.Node) bool
IsLiteralConstDeclaration(node *ast.Node) bool
RequiresAddingImplicitUndefined(node *ast.Node, symbol *ast.Symbol, enclosingDeclaration *ast.Node) bool
IsDeclarationVisible(node *ast.Node) bool
Expand Down
8 changes: 7 additions & 1 deletion tsc/internal/transformers/declarations/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -2908,12 +2908,18 @@ func (tx *DeclarationTransformer) transformExpandoAssignment(node *ast.BinaryExp

preexistingExpandoHasExport := core.Some(tx.expandoMembers[hostId], ast.IsExportDeclaration)

if ast.IsIdentifier(node.Right) {
// The namespace is declared next to the host, so the alias must mean the same thing there
hostContainer := core.IfElse(ast.IsVariableDeclaration(declaration), declaration.Parent.Parent, declaration).Parent
if ast.IsIdentifier(node.Right) && tx.resolver.CanDeclareExpandoAssignmentAsAlias(node.AsNode(), hostContainer) {
Comment on lines +2912 to +2913
if !preexistingExpandoHasExport {
tx.addExportModifierToExpandoMembers(hostId)
}
// alias-like, emit an `export {name}` or `export {name as alias}`
// Expando members are collected from the source file, so make the aliased name visible from the host's scope
oldEnclosing := tx.enclosingDeclaration
tx.enclosingDeclaration = hostContainer
result := tx.transformBinaryExpressionToExportDeclaration(node.AsNode(), exportName)
tx.enclosingDeclaration = oldEnclosing
tx.expandoMembers[hostId] = append(tx.expandoMembers[hostId], result)
return
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
//// [tests/cases/compiler/declarationEmitExpandoIdentifierValue.ts] ////

//// [dep.ts]
export const imported = 1;
export let importedLet = "s";

//// [lib.ts]
import { imported, importedLet } from "./dep";

// Not an alias: the identifier means something else, or has another type, where the namespace is declared

export function loop() {}
for (const k of ["a", "b"]) {
loop.last = k;
}

const shadowed = 1;
export function shadow() {}
{
const shadowed = "s";
shadow.v = shadowed;
}

declare const wide: string | number;
export function narrowed() {}
if (typeof wide === "string") {
narrowed.s = wide;
}

const VERSION = "1.2.3";
export function literal() {}
literal.version = VERSION;

const initial: number = 1;
export function reassigned() {}
reassigned.n = initial;
reassigned.n = 5;

export function fromImport() {}
fromImport.literal = imported;

// Still an alias

export function keepsImport() {}
keepsImport.value = importedLet;

let mutable = 1;
export function keepsLet() {}
keepsLet.m = mutable;

class C {
x = 1;
}
export function keepsClass() {}
keepsClass.C = C;

function helper() {
return 1;
}
export function keepsFunction() {}
keepsFunction.helper = helper;

export const arrowHost = () => {};
const arrowValue: string = "x";
arrowHost.v = arrowValue;

export namespace N {
const k: number = 1;
export function f() {}
f.k = k;
{
const k = "s";
f.inner = k;
}
}

//// [use.ts]
import { loop, shadow, narrowed, literal, reassigned, fromImport, keepsImport, keepsLet, keepsClass, keepsFunction, arrowHost, N } from "./lib";
const s1: string = loop.last;
const s2: string = shadow.v;
const s3: string = narrowed.s;
literal.version = "2.0.0";
reassigned.n = 6;
fromImport.literal = 2;
const s4: string = keepsImport.value;
const n1: number = keepsLet.m;
const n0: number = new keepsClass.C().x;
const n2: number = keepsFunction.helper();
const s5: string = arrowHost.v;
const n3: number = N.f.k;
const s6: string = N.f.inner;


//// [dep.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.importedLet = exports.imported = void 0;
exports.imported = 1;
exports.importedLet = "s";
//// [lib.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.N = exports.arrowHost = void 0;
exports.loop = loop;
exports.shadow = shadow;
exports.narrowed = narrowed;
exports.literal = literal;
exports.reassigned = reassigned;
exports.fromImport = fromImport;
exports.keepsImport = keepsImport;
exports.keepsLet = keepsLet;
exports.keepsClass = keepsClass;
exports.keepsFunction = keepsFunction;
const dep_1 = require("./dep");
// Not an alias: the identifier means something else, or has another type, where the namespace is declared
function loop() { }
for (const k of ["a", "b"]) {
loop.last = k;
}
const shadowed = 1;
function shadow() { }
{
const shadowed = "s";
shadow.v = shadowed;
}
function narrowed() { }
if (typeof wide === "string") {
narrowed.s = wide;
}
const VERSION = "1.2.3";
function literal() { }
literal.version = VERSION;
const initial = 1;
function reassigned() { }
reassigned.n = initial;
reassigned.n = 5;
function fromImport() { }
fromImport.literal = dep_1.imported;
// Still an alias
function keepsImport() { }
keepsImport.value = dep_1.importedLet;
let mutable = 1;
function keepsLet() { }
keepsLet.m = mutable;
class C {
x = 1;
}
function keepsClass() { }
keepsClass.C = C;
function helper() {
return 1;
}
function keepsFunction() { }
keepsFunction.helper = helper;
const arrowHost = () => { };
exports.arrowHost = arrowHost;
const arrowValue = "x";
exports.arrowHost.v = arrowValue;
var N;
(function (N) {
const k = 1;
function f() { }
N.f = f;
f.k = k;
{
const k = "s";
f.inner = k;
}
})(N || (exports.N = N = {}));
//// [use.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lib_1 = require("./lib");
const s1 = lib_1.loop.last;
const s2 = lib_1.shadow.v;
const s3 = lib_1.narrowed.s;
lib_1.literal.version = "2.0.0";
lib_1.reassigned.n = 6;
lib_1.fromImport.literal = 2;
const s4 = lib_1.keepsImport.value;
const n1 = lib_1.keepsLet.m;
const n0 = new lib_1.keepsClass.C().x;
const n2 = lib_1.keepsFunction.helper();
const s5 = lib_1.arrowHost.v;
const n3 = lib_1.N.f.k;
const s6 = lib_1.N.f.inner;


//// [dep.d.ts]
export declare const imported = 1;
export declare let importedLet: string;
//// [lib.d.ts]
import { importedLet } from "./dep";
export declare function loop(): void;
export declare namespace loop {
var last: string;
}
export declare function shadow(): void;
export declare namespace shadow {
var v: string;
}
export declare function narrowed(): void;
export declare namespace narrowed {
var s: string;
}
export declare function literal(): void;
export declare namespace literal {
var version: string;
}
export declare function reassigned(): void;
export declare namespace reassigned {
var n: number;
var n: number;
}
export declare function fromImport(): void;
export declare namespace fromImport {
var _a: number;
export { _a as literal };
}
export declare function keepsImport(): void;
export declare namespace keepsImport {
export { importedLet as value };
}
declare let mutable: number;
export declare function keepsLet(): void;
export declare namespace keepsLet {
export { mutable as m };
}
declare class C {
x: number;
}
export declare function keepsClass(): void;
export declare namespace keepsClass {
export { C };
}
declare function helper(): number;
export declare function keepsFunction(): void;
export declare namespace keepsFunction {
export { helper };
}
export declare function arrowHost(): void;
export declare namespace arrowHost {
export { arrowValue as v };
}
declare const arrowValue: string;
export declare namespace N {
const k: number;
export function f(): void;
export namespace f {
export { k };
export var inner: string;
}
export {};
}
export {};
//// [use.d.ts]
export {};
Loading