Skip to content

Handle when import alias has no parent in JS declarations (therefore is synthetic module symbol) #33813

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
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
9 changes: 6 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5350,7 +5350,7 @@ namespace ts {
// so we don't even have placeholders to fill in.
if (length(realMembers)) {
const localName = getInternalSymbolName(symbol, symbolName);
serializeAsNamespaceDeclaration(realMembers, localName, modifierFlags, /*suppressNewPrivateContext*/ false);
serializeAsNamespaceDeclaration(realMembers, localName, modifierFlags, !!(symbol.flags & SymbolFlags.Function));
}
if (length(mergedMembers)) {
const localName = getInternalSymbolName(symbol, symbolName);
Expand Down Expand Up @@ -5593,7 +5593,10 @@ namespace ts {
/*decorators*/ undefined,
/*modifiers*/ undefined,
createImportClause(createIdentifier(localName), /*namedBindings*/ undefined),
createLiteral(getSpecifierForModuleSymbol(target.parent!, context))
// We use `target.parent || target` below as `target.parent` is unset when the target is a module which has been export assigned
// And then made into a default by the `esModuleInterop` or `allowSyntheticDefaultImports` flag
// In such cases, the `target` refers to the module itself already
createLiteral(getSpecifierForModuleSymbol(target.parent || target, context))
), ModifierFlags.None);
break;
case SyntaxKind.NamespaceImport:
Expand All @@ -5614,7 +5617,7 @@ namespace ts {
createIdentifier(localName)
)
])),
createLiteral(getSpecifierForModuleSymbol(target.parent!, context))
createLiteral(getSpecifierForModuleSymbol(target.parent || target, context))
), ModifierFlags.None);
break;
case SyntaxKind.ExportSpecifier:
Expand Down
1 change: 1 addition & 0 deletions src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ namespace ts {
let combinedStatements: NodeArray<Statement>;
if (isSourceFileJS(currentSourceFile)) {
combinedStatements = createNodeArray(transformDeclarationsForJS(node));
refs.forEach(referenceVisitor);
emittedImports = filter(combinedStatements, isAnyImportSyntax);
}
else {
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/jsDeclarationsFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ export namespace b {
export function c(): void;
export namespace c {
export { Cls };
class Cls {
}
}
/**
* @param {number} a
Expand All @@ -156,6 +154,8 @@ export namespace f {
}
export function i(): void;
export function j(): void;
declare class Cls {
}
/**
* @param {{x: string}} a
* @param {{y: typeof b}} b
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//// [jsxDeclarationsWithEsModuleInteropNoCrash.jsx]
/// <reference path="/.lib/react16.d.ts" />
import PropTypes from 'prop-types';
import React from 'react';

const propTypes = {
bar: PropTypes.bool,
};

const defaultProps = {
bar: false,
};

function Foo({ bar }) {
return <div>{bar}</div>;
}

Foo.propTypes = propTypes;
Foo.defaultProps = defaultProps;

export default Foo;



//// [jsxDeclarationsWithEsModuleInteropNoCrash.d.ts]
/// <reference path="../../../..react16.d.ts" />
export default Foo;
declare function Foo({ bar }: {
bar: any;
}): JSX.Element;
declare namespace Foo {
export { propTypes };
export { defaultProps };
}
declare namespace propTypes {
import bar = PropTypes.bool;
export { bar };
}
declare namespace defaultProps {
const bar_1: boolean;
export { bar_1 as bar };
}
import PropTypes from "prop-types";
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
=== tests/cases/compiler/jsxDeclarationsWithEsModuleInteropNoCrash.jsx ===
/// <reference path="react16.d.ts" />
import PropTypes from 'prop-types';
>PropTypes : Symbol(PropTypes, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 1, 6))

import React from 'react';
>React : Symbol(React, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 2, 6))

const propTypes = {
>propTypes : Symbol(propTypes, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 4, 5))

bar: PropTypes.bool,
>bar : Symbol(bar, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 4, 19))
>PropTypes.bool : Symbol(PropTypes.bool, Decl(react16.d.ts, 62, 16))
>PropTypes : Symbol(PropTypes, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 1, 6))
>bool : Symbol(PropTypes.bool, Decl(react16.d.ts, 62, 16))

};

const defaultProps = {
>defaultProps : Symbol(defaultProps, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 8, 5))

bar: false,
>bar : Symbol(bar, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 8, 22))

};

function Foo({ bar }) {
>Foo : Symbol(Foo, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 10, 2), Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 14, 1), Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 16, 26))
>bar : Symbol(bar, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 12, 14))

return <div>{bar}</div>;
>div : Symbol(JSX.IntrinsicElements.div, Decl(react16.d.ts, 2420, 114))
>bar : Symbol(bar, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 12, 14))
>div : Symbol(JSX.IntrinsicElements.div, Decl(react16.d.ts, 2420, 114))
}

Foo.propTypes = propTypes;
>Foo.propTypes : Symbol(Foo.propTypes, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 14, 1))
>Foo : Symbol(Foo, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 10, 2), Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 14, 1), Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 16, 26))
>propTypes : Symbol(Foo.propTypes, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 14, 1))
>propTypes : Symbol(propTypes, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 4, 5))

Foo.defaultProps = defaultProps;
>Foo.defaultProps : Symbol(Foo.defaultProps, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 16, 26))
>Foo : Symbol(Foo, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 10, 2), Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 14, 1), Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 16, 26))
>defaultProps : Symbol(Foo.defaultProps, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 16, 26))
>defaultProps : Symbol(defaultProps, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 8, 5))

export default Foo;
>Foo : Symbol(Foo, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 10, 2), Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 14, 1), Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 16, 26))

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
=== tests/cases/compiler/jsxDeclarationsWithEsModuleInteropNoCrash.jsx ===
/// <reference path="react16.d.ts" />
import PropTypes from 'prop-types';
>PropTypes : typeof PropTypes

import React from 'react';
>React : typeof React

const propTypes = {
>propTypes : { bar: PropTypes.Requireable<boolean>; }
>{ bar: PropTypes.bool,} : { bar: PropTypes.Requireable<boolean>; }

bar: PropTypes.bool,
>bar : PropTypes.Requireable<boolean>
>PropTypes.bool : PropTypes.Requireable<boolean>
>PropTypes : typeof PropTypes
>bool : PropTypes.Requireable<boolean>

};

const defaultProps = {
>defaultProps : { bar: boolean; }
>{ bar: false,} : { bar: boolean; }

bar: false,
>bar : boolean
>false : false

};

function Foo({ bar }) {
>Foo : typeof Foo
>bar : any

return <div>{bar}</div>;
><div>{bar}</div> : JSX.Element
>div : any
>bar : any
>div : any
}

Foo.propTypes = propTypes;
>Foo.propTypes = propTypes : { bar: PropTypes.Requireable<boolean>; }
>Foo.propTypes : { bar: PropTypes.Requireable<boolean>; }
>Foo : typeof Foo
>propTypes : { bar: PropTypes.Requireable<boolean>; }
>propTypes : { bar: PropTypes.Requireable<boolean>; }

Foo.defaultProps = defaultProps;
>Foo.defaultProps = defaultProps : { bar: boolean; }
>Foo.defaultProps : { bar: boolean; }
>Foo : typeof Foo
>defaultProps : { bar: boolean; }
>defaultProps : { bar: boolean; }

export default Foo;
>Foo : typeof Foo

29 changes: 29 additions & 0 deletions tests/cases/compiler/jsxDeclarationsWithEsModuleInteropNoCrash.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @allowJs: true
// @checkJs: true
// @emitDeclarationOnly: true
// @declaration: true
// @strict: true
// @esModuleInterop: true
// @jsx: react
// @noImplicitAny: false
// @filename: jsxDeclarationsWithEsModuleInteropNoCrash.jsx
/// <reference path="/.lib/react16.d.ts" />
import PropTypes from 'prop-types';
import React from 'react';

const propTypes = {
bar: PropTypes.bool,
};

const defaultProps = {
bar: false,
};

function Foo({ bar }) {
return <div>{bar}</div>;
}

Foo.propTypes = propTypes;
Foo.defaultProps = defaultProps;

export default Foo;