Skip to content

Add intra expression inference sites based on JSX attributes #52837

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
merged 3 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 11 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ import {
isJsxAttributeLike,
isJsxAttributes,
isJsxElement,
isJsxExpression,
isJsxOpeningElement,
isJsxOpeningFragment,
isJsxOpeningLikeElement,
Expand Down Expand Up @@ -30160,7 +30161,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
*/
function createJsxAttributesTypeFromAttributesProperty(openingLikeElement: JsxOpeningLikeElement, checkMode: CheckMode | undefined) {
const attributes = openingLikeElement.attributes;
const attributesType = getContextualType(attributes, ContextFlags.None);
const contextualType = getContextualType(attributes, ContextFlags.None);
const allAttributesTable = strictNullChecks ? createSymbolTable() : undefined;
let attributesTable = createSymbolTable();
let spread: Type = emptyJsxObjectType;
Expand Down Expand Up @@ -30189,12 +30190,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (attributeDecl.name.escapedText === jsxChildrenPropertyName) {
explicitlySpecifyChildrenAttribute = true;
}
if (attributesType) {
const prop = getPropertyOfType(attributesType, member.escapedName);
if (contextualType) {
const prop = getPropertyOfType(contextualType, member.escapedName);
if (prop && prop.declarations && isDeprecatedSymbol(prop)) {
addDeprecatedSuggestion(attributeDecl.name, prop.declarations, attributeDecl.name.escapedText as string);
}
}
if (contextualType && checkMode && checkMode & CheckMode.Inferential && !(checkMode & CheckMode.SkipContextSensitive) &&
attributeDecl.initializer && isJsxExpression(attributeDecl.initializer) && attributeDecl.initializer.expression && isContextSensitive(attributeDecl.initializer.expression)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
attributeDecl.initializer && isJsxExpression(attributeDecl.initializer) && attributeDecl.initializer.expression && isContextSensitive(attributeDecl.initializer.expression)) {
isContextSensitive(attributeDecl)) {

since it handles attribute declarations and does all that unpacking and existence checking inside it already and there's no need to duplicate the checks. Just toss some ! on the reference below for control flow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pushed out the requested change

const inferenceContext = getInferenceContext(attributes);
Debug.assert(inferenceContext); // In CheckMode.Inferential we should always have an inference context
const inferenceNode = attributeDecl.initializer.expression;
addIntraExpressionInferenceSite(inferenceContext, inferenceNode, exprType);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
else {
Debug.assert(attributeDecl.kind === SyntaxKind.JsxSpreadAttribute);
Expand Down
199 changes: 199 additions & 0 deletions tests/baselines/reference/intraExpressionInferencesJsx.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
=== tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInferencesJsx.tsx ===
/// <reference path="react16.d.ts" />

// repro from #52798

type A = {
>A : Symbol(A, Decl(intraExpressionInferencesJsx.tsx, 0, 0))

a: boolean;
>a : Symbol(a, Decl(intraExpressionInferencesJsx.tsx, 4, 10))

};

type B = {
>B : Symbol(B, Decl(intraExpressionInferencesJsx.tsx, 6, 2))

b: string;
>b : Symbol(b, Decl(intraExpressionInferencesJsx.tsx, 8, 10))

};

type C = {
>C : Symbol(C, Decl(intraExpressionInferencesJsx.tsx, 10, 2))

c: number;
>c : Symbol(c, Decl(intraExpressionInferencesJsx.tsx, 12, 10))

};

type Animations = {
>Animations : Symbol(Animations, Decl(intraExpressionInferencesJsx.tsx, 14, 2))

[key: string]: { value: number } & (
>key : Symbol(key, Decl(intraExpressionInferencesJsx.tsx, 17, 3))
>value : Symbol(value, Decl(intraExpressionInferencesJsx.tsx, 17, 18))

| ({ kind: "a"; func?(): Partial<A> } & A)
>kind : Symbol(kind, Decl(intraExpressionInferencesJsx.tsx, 18, 8))
>func : Symbol(func, Decl(intraExpressionInferencesJsx.tsx, 18, 19))
>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --))
>A : Symbol(A, Decl(intraExpressionInferencesJsx.tsx, 0, 0))
>A : Symbol(A, Decl(intraExpressionInferencesJsx.tsx, 0, 0))

| ({ kind: "b"; func?(): Partial<B> } & B)
>kind : Symbol(kind, Decl(intraExpressionInferencesJsx.tsx, 19, 8))
>func : Symbol(func, Decl(intraExpressionInferencesJsx.tsx, 19, 19))
>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --))
>B : Symbol(B, Decl(intraExpressionInferencesJsx.tsx, 6, 2))
>B : Symbol(B, Decl(intraExpressionInferencesJsx.tsx, 6, 2))

| ({ kind: "c"; func?(): Partial<C> } & C)
>kind : Symbol(kind, Decl(intraExpressionInferencesJsx.tsx, 20, 8))
>func : Symbol(func, Decl(intraExpressionInferencesJsx.tsx, 20, 19))
>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --))
>C : Symbol(C, Decl(intraExpressionInferencesJsx.tsx, 10, 2))
>C : Symbol(C, Decl(intraExpressionInferencesJsx.tsx, 10, 2))

);
};

type StyleParam<T extends Animations> = Record<keyof T, string>;
>StyleParam : Symbol(StyleParam, Decl(intraExpressionInferencesJsx.tsx, 22, 2))
>T : Symbol(T, Decl(intraExpressionInferencesJsx.tsx, 24, 16))
>Animations : Symbol(Animations, Decl(intraExpressionInferencesJsx.tsx, 14, 2))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(intraExpressionInferencesJsx.tsx, 24, 16))

type AnimatedViewProps<T extends Animations> = {
>AnimatedViewProps : Symbol(AnimatedViewProps, Decl(intraExpressionInferencesJsx.tsx, 24, 64))
>T : Symbol(T, Decl(intraExpressionInferencesJsx.tsx, 26, 23))
>Animations : Symbol(Animations, Decl(intraExpressionInferencesJsx.tsx, 14, 2))

style: (animationsValues: StyleParam<T>) => string;
>style : Symbol(style, Decl(intraExpressionInferencesJsx.tsx, 26, 48))
>animationsValues : Symbol(animationsValues, Decl(intraExpressionInferencesJsx.tsx, 27, 10))
>StyleParam : Symbol(StyleParam, Decl(intraExpressionInferencesJsx.tsx, 22, 2))
>T : Symbol(T, Decl(intraExpressionInferencesJsx.tsx, 26, 23))

animations: T;
>animations : Symbol(animations, Decl(intraExpressionInferencesJsx.tsx, 27, 53))
>T : Symbol(T, Decl(intraExpressionInferencesJsx.tsx, 26, 23))

};

const Component = <T extends Animations>({
>Component : Symbol(Component, Decl(intraExpressionInferencesJsx.tsx, 31, 5))
>T : Symbol(T, Decl(intraExpressionInferencesJsx.tsx, 31, 19))
>Animations : Symbol(Animations, Decl(intraExpressionInferencesJsx.tsx, 14, 2))

animations,
>animations : Symbol(animations, Decl(intraExpressionInferencesJsx.tsx, 31, 42))

style,
>style : Symbol(style, Decl(intraExpressionInferencesJsx.tsx, 32, 13))

}: AnimatedViewProps<T>) => <></>;
>AnimatedViewProps : Symbol(AnimatedViewProps, Decl(intraExpressionInferencesJsx.tsx, 24, 64))
>T : Symbol(T, Decl(intraExpressionInferencesJsx.tsx, 31, 19))

<Component
>Component : Symbol(Component, Decl(intraExpressionInferencesJsx.tsx, 31, 5))

animations={{
>animations : Symbol(animations, Decl(intraExpressionInferencesJsx.tsx, 36, 10))

test: {
>test : Symbol(test, Decl(intraExpressionInferencesJsx.tsx, 37, 15))

kind: "a",
>kind : Symbol(kind, Decl(intraExpressionInferencesJsx.tsx, 38, 11))

value: 1,
>value : Symbol(value, Decl(intraExpressionInferencesJsx.tsx, 39, 16))

a: true,
>a : Symbol(a, Decl(intraExpressionInferencesJsx.tsx, 40, 15))

},
}}
style={(anim) => {
>style : Symbol(style, Decl(intraExpressionInferencesJsx.tsx, 43, 4))
>anim : Symbol(anim, Decl(intraExpressionInferencesJsx.tsx, 44, 10))

return "";
}}
/>;
<Component
>Component : Symbol(Component, Decl(intraExpressionInferencesJsx.tsx, 31, 5))

animations={{
>animations : Symbol(animations, Decl(intraExpressionInferencesJsx.tsx, 48, 10))

test: {
>test : Symbol(test, Decl(intraExpressionInferencesJsx.tsx, 49, 15))

kind: "a",
>kind : Symbol(kind, Decl(intraExpressionInferencesJsx.tsx, 50, 11))

value: 1,
>value : Symbol(value, Decl(intraExpressionInferencesJsx.tsx, 51, 16))

a: true,
>a : Symbol(a, Decl(intraExpressionInferencesJsx.tsx, 52, 15))

func() {
>func : Symbol(func, Decl(intraExpressionInferencesJsx.tsx, 53, 14))

return {
a: true,
>a : Symbol(a, Decl(intraExpressionInferencesJsx.tsx, 55, 16))

};
},
},
}}
style={(anim) => {
>style : Symbol(style, Decl(intraExpressionInferencesJsx.tsx, 60, 4))
>anim : Symbol(anim, Decl(intraExpressionInferencesJsx.tsx, 61, 10))

return "";
}}
/>;
<Component
>Component : Symbol(Component, Decl(intraExpressionInferencesJsx.tsx, 31, 5))

animations={{
>animations : Symbol(animations, Decl(intraExpressionInferencesJsx.tsx, 65, 10))

test: {
>test : Symbol(test, Decl(intraExpressionInferencesJsx.tsx, 66, 15))

kind: "a",
>kind : Symbol(kind, Decl(intraExpressionInferencesJsx.tsx, 67, 11))

value: 1,
>value : Symbol(value, Decl(intraExpressionInferencesJsx.tsx, 68, 16))

a: true,
>a : Symbol(a, Decl(intraExpressionInferencesJsx.tsx, 69, 15))

func: () => {
>func : Symbol(func, Decl(intraExpressionInferencesJsx.tsx, 70, 14))

return {
a: true,
>a : Symbol(a, Decl(intraExpressionInferencesJsx.tsx, 72, 16))

};
},
},
}}
style={(anim) => {
>style : Symbol(style, Decl(intraExpressionInferencesJsx.tsx, 77, 4))
>anim : Symbol(anim, Decl(intraExpressionInferencesJsx.tsx, 78, 10))

return "";
}}
/>;

Loading