Skip to content

Commit 114329a

Browse files
authored
Handle when import alias has to parent (therefore is synthetic module symbol) (#33813)
1 parent 3c123b6 commit 114329a

7 files changed

+191
-5
lines changed

src/compiler/checker.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -5350,7 +5350,7 @@ namespace ts {
53505350
// so we don't even have placeholders to fill in.
53515351
if (length(realMembers)) {
53525352
const localName = getInternalSymbolName(symbol, symbolName);
5353-
serializeAsNamespaceDeclaration(realMembers, localName, modifierFlags, /*suppressNewPrivateContext*/ false);
5353+
serializeAsNamespaceDeclaration(realMembers, localName, modifierFlags, !!(symbol.flags & SymbolFlags.Function));
53545354
}
53555355
if (length(mergedMembers)) {
53565356
const localName = getInternalSymbolName(symbol, symbolName);
@@ -5593,7 +5593,10 @@ namespace ts {
55935593
/*decorators*/ undefined,
55945594
/*modifiers*/ undefined,
55955595
createImportClause(createIdentifier(localName), /*namedBindings*/ undefined),
5596-
createLiteral(getSpecifierForModuleSymbol(target.parent!, context))
5596+
// We use `target.parent || target` below as `target.parent` is unset when the target is a module which has been export assigned
5597+
// And then made into a default by the `esModuleInterop` or `allowSyntheticDefaultImports` flag
5598+
// In such cases, the `target` refers to the module itself already
5599+
createLiteral(getSpecifierForModuleSymbol(target.parent || target, context))
55975600
), ModifierFlags.None);
55985601
break;
55995602
case SyntaxKind.NamespaceImport:
@@ -5614,7 +5617,7 @@ namespace ts {
56145617
createIdentifier(localName)
56155618
)
56165619
])),
5617-
createLiteral(getSpecifierForModuleSymbol(target.parent!, context))
5620+
createLiteral(getSpecifierForModuleSymbol(target.parent || target, context))
56185621
), ModifierFlags.None);
56195622
break;
56205623
case SyntaxKind.ExportSpecifier:

src/compiler/transformers/declarations.ts

+1
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ namespace ts {
285285
let combinedStatements: NodeArray<Statement>;
286286
if (isSourceFileJS(currentSourceFile)) {
287287
combinedStatements = createNodeArray(transformDeclarationsForJS(node));
288+
refs.forEach(referenceVisitor);
288289
emittedImports = filter(combinedStatements, isAnyImportSyntax);
289290
}
290291
else {

tests/baselines/reference/jsDeclarationsFunctions.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ export namespace b {
130130
export function c(): void;
131131
export namespace c {
132132
export { Cls };
133-
class Cls {
134-
}
135133
}
136134
/**
137135
* @param {number} a
@@ -156,6 +154,8 @@ export namespace f {
156154
}
157155
export function i(): void;
158156
export function j(): void;
157+
declare class Cls {
158+
}
159159
/**
160160
* @param {{x: string}} a
161161
* @param {{y: typeof b}} b
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//// [jsxDeclarationsWithEsModuleInteropNoCrash.jsx]
2+
/// <reference path="/.lib/react16.d.ts" />
3+
import PropTypes from 'prop-types';
4+
import React from 'react';
5+
6+
const propTypes = {
7+
bar: PropTypes.bool,
8+
};
9+
10+
const defaultProps = {
11+
bar: false,
12+
};
13+
14+
function Foo({ bar }) {
15+
return <div>{bar}</div>;
16+
}
17+
18+
Foo.propTypes = propTypes;
19+
Foo.defaultProps = defaultProps;
20+
21+
export default Foo;
22+
23+
24+
25+
//// [jsxDeclarationsWithEsModuleInteropNoCrash.d.ts]
26+
/// <reference path="../../../..react16.d.ts" />
27+
export default Foo;
28+
declare function Foo({ bar }: {
29+
bar: any;
30+
}): JSX.Element;
31+
declare namespace Foo {
32+
export { propTypes };
33+
export { defaultProps };
34+
}
35+
declare namespace propTypes {
36+
import bar = PropTypes.bool;
37+
export { bar };
38+
}
39+
declare namespace defaultProps {
40+
const bar_1: boolean;
41+
export { bar_1 as bar };
42+
}
43+
import PropTypes from "prop-types";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
=== tests/cases/compiler/jsxDeclarationsWithEsModuleInteropNoCrash.jsx ===
2+
/// <reference path="react16.d.ts" />
3+
import PropTypes from 'prop-types';
4+
>PropTypes : Symbol(PropTypes, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 1, 6))
5+
6+
import React from 'react';
7+
>React : Symbol(React, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 2, 6))
8+
9+
const propTypes = {
10+
>propTypes : Symbol(propTypes, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 4, 5))
11+
12+
bar: PropTypes.bool,
13+
>bar : Symbol(bar, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 4, 19))
14+
>PropTypes.bool : Symbol(PropTypes.bool, Decl(react16.d.ts, 62, 16))
15+
>PropTypes : Symbol(PropTypes, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 1, 6))
16+
>bool : Symbol(PropTypes.bool, Decl(react16.d.ts, 62, 16))
17+
18+
};
19+
20+
const defaultProps = {
21+
>defaultProps : Symbol(defaultProps, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 8, 5))
22+
23+
bar: false,
24+
>bar : Symbol(bar, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 8, 22))
25+
26+
};
27+
28+
function Foo({ bar }) {
29+
>Foo : Symbol(Foo, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 10, 2), Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 14, 1), Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 16, 26))
30+
>bar : Symbol(bar, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 12, 14))
31+
32+
return <div>{bar}</div>;
33+
>div : Symbol(JSX.IntrinsicElements.div, Decl(react16.d.ts, 2420, 114))
34+
>bar : Symbol(bar, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 12, 14))
35+
>div : Symbol(JSX.IntrinsicElements.div, Decl(react16.d.ts, 2420, 114))
36+
}
37+
38+
Foo.propTypes = propTypes;
39+
>Foo.propTypes : Symbol(Foo.propTypes, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 14, 1))
40+
>Foo : Symbol(Foo, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 10, 2), Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 14, 1), Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 16, 26))
41+
>propTypes : Symbol(Foo.propTypes, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 14, 1))
42+
>propTypes : Symbol(propTypes, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 4, 5))
43+
44+
Foo.defaultProps = defaultProps;
45+
>Foo.defaultProps : Symbol(Foo.defaultProps, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 16, 26))
46+
>Foo : Symbol(Foo, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 10, 2), Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 14, 1), Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 16, 26))
47+
>defaultProps : Symbol(Foo.defaultProps, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 16, 26))
48+
>defaultProps : Symbol(defaultProps, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 8, 5))
49+
50+
export default Foo;
51+
>Foo : Symbol(Foo, Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 10, 2), Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 14, 1), Decl(jsxDeclarationsWithEsModuleInteropNoCrash.jsx, 16, 26))
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
=== tests/cases/compiler/jsxDeclarationsWithEsModuleInteropNoCrash.jsx ===
2+
/// <reference path="react16.d.ts" />
3+
import PropTypes from 'prop-types';
4+
>PropTypes : typeof PropTypes
5+
6+
import React from 'react';
7+
>React : typeof React
8+
9+
const propTypes = {
10+
>propTypes : { bar: PropTypes.Requireable<boolean>; }
11+
>{ bar: PropTypes.bool,} : { bar: PropTypes.Requireable<boolean>; }
12+
13+
bar: PropTypes.bool,
14+
>bar : PropTypes.Requireable<boolean>
15+
>PropTypes.bool : PropTypes.Requireable<boolean>
16+
>PropTypes : typeof PropTypes
17+
>bool : PropTypes.Requireable<boolean>
18+
19+
};
20+
21+
const defaultProps = {
22+
>defaultProps : { bar: boolean; }
23+
>{ bar: false,} : { bar: boolean; }
24+
25+
bar: false,
26+
>bar : boolean
27+
>false : false
28+
29+
};
30+
31+
function Foo({ bar }) {
32+
>Foo : typeof Foo
33+
>bar : any
34+
35+
return <div>{bar}</div>;
36+
><div>{bar}</div> : JSX.Element
37+
>div : any
38+
>bar : any
39+
>div : any
40+
}
41+
42+
Foo.propTypes = propTypes;
43+
>Foo.propTypes = propTypes : { bar: PropTypes.Requireable<boolean>; }
44+
>Foo.propTypes : { bar: PropTypes.Requireable<boolean>; }
45+
>Foo : typeof Foo
46+
>propTypes : { bar: PropTypes.Requireable<boolean>; }
47+
>propTypes : { bar: PropTypes.Requireable<boolean>; }
48+
49+
Foo.defaultProps = defaultProps;
50+
>Foo.defaultProps = defaultProps : { bar: boolean; }
51+
>Foo.defaultProps : { bar: boolean; }
52+
>Foo : typeof Foo
53+
>defaultProps : { bar: boolean; }
54+
>defaultProps : { bar: boolean; }
55+
56+
export default Foo;
57+
>Foo : typeof Foo
58+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @emitDeclarationOnly: true
4+
// @declaration: true
5+
// @strict: true
6+
// @esModuleInterop: true
7+
// @jsx: react
8+
// @noImplicitAny: false
9+
// @filename: jsxDeclarationsWithEsModuleInteropNoCrash.jsx
10+
/// <reference path="/.lib/react16.d.ts" />
11+
import PropTypes from 'prop-types';
12+
import React from 'react';
13+
14+
const propTypes = {
15+
bar: PropTypes.bool,
16+
};
17+
18+
const defaultProps = {
19+
bar: false,
20+
};
21+
22+
function Foo({ bar }) {
23+
return <div>{bar}</div>;
24+
}
25+
26+
Foo.propTypes = propTypes;
27+
Foo.defaultProps = defaultProps;
28+
29+
export default Foo;

0 commit comments

Comments
 (0)