Skip to content

Commit 9729ada

Browse files
Extracted local context key in React extension
1 parent 063d180 commit 9729ada

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Specifies keys for the definition and retrieval of local contexts within the React extension of the TS LCE.
3+
*
4+
* (see also "Local Contexts" in the developer documentation)
5+
*/
6+
export class ReactContextKeys {
7+
8+
/**
9+
* List of JSX tags that is used in some environment, e.g. inside the code of a function (`Array<LCEJSXDependency>`)
10+
*/
11+
public static readonly JSX_DEPENDENCIES = "jsx-dependency-context";
12+
13+
}

typescript/src/react/processors/jsx-dependency.processor.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@ import { LCEClassDeclaration } from "../../core/concepts/class-declaration.conce
99
import { LCEVariableDeclaration } from "../../core/concepts/variable-declaration.concept";
1010
import { LCEFunctionDeclaration } from "../../core/concepts/function-declaration.concept";
1111
import { VariableDeclarationProcessor } from "../../core/processors/variable-declaration.processor";
12+
import { ReactContextKeys } from "../context.keys";
1213

1314
export class JSXDependencyContextProcessor extends Processor {
14-
/**
15-
* represents a list of JSX tags that is used in some environment, e.g. inside the code of a function
16-
*/
17-
public static readonly JSX_DEPENDENCY_CONTEXT = "jsx-dependency-context";
18-
1915
public static readonly JSX_DEPENDENCY_METADATA: "jsx-dependencies";
2016

2117
public executionCondition: ExecutionCondition = new ExecutionCondition(
@@ -34,13 +30,11 @@ export class JSXDependencyContextProcessor extends Processor {
3430
);
3531

3632
public override preChildrenProcessing(processingContext: ProcessingContext) {
37-
processingContext.localContexts.currentContexts.set(JSXDependencyContextProcessor.JSX_DEPENDENCY_CONTEXT, new Array<LCEJSXDependency>());
33+
processingContext.localContexts.currentContexts.set(ReactContextKeys.JSX_DEPENDENCIES, new Array<LCEJSXDependency>());
3834
}
3935

4036
public override postChildrenProcessing({ localContexts, metadataAssignments }: ProcessingContext): ConceptMap {
41-
const jsxContext: LCEJSXDependency[] = (
42-
localContexts.getNextContext(JSXDependencyContextProcessor.JSX_DEPENDENCY_CONTEXT) as [LCEJSXDependency[], number]
43-
)[0];
37+
const jsxContext: LCEJSXDependency[] = (localContexts.getNextContext(ReactContextKeys.JSX_DEPENDENCIES) as [LCEJSXDependency[], number])[0];
4438

4539
const aggregatedDependencies = new Map<string, LCEJSXDependency>();
4640
for (const dep of jsxContext) {
@@ -72,23 +66,23 @@ export class JSXDependencyProcessor extends Processor {
7266
let name = "";
7367

7468
// try to determine name of the tag, abort processing, if not possible
75-
if(node.name.type === AST_NODE_TYPES.JSXIdentifier) {
69+
if (node.name.type === AST_NODE_TYPES.JSXIdentifier) {
7670
name = node.name.name;
7771
} else if (node.name.type === AST_NODE_TYPES.JSXMemberExpression) {
7872
let depth = 0;
7973
name = node.name.property.name;
8074
let currentExpression = node.name.object;
81-
while(currentExpression.type === AST_NODE_TYPES.JSXMemberExpression) {
82-
if(depth > 20) {
83-
console.log("ERROR: Could not resolve JSX member expression:")
75+
while (currentExpression.type === AST_NODE_TYPES.JSXMemberExpression) {
76+
if (depth > 20) {
77+
console.log("ERROR: Could not resolve JSX member expression:");
8478
console.log(name);
8579
return new Map();
8680
}
8781
name = currentExpression.property.name + "." + name;
8882
currentExpression = currentExpression.object;
8983
depth++;
9084
}
91-
if(currentExpression.type === AST_NODE_TYPES.JSXIdentifier) {
85+
if (currentExpression.type === AST_NODE_TYPES.JSXIdentifier) {
9286
name = currentExpression.name + "." + name;
9387
} else {
9488
name = currentExpression.namespace.name + "." + name;
@@ -98,17 +92,16 @@ export class JSXDependencyProcessor extends Processor {
9892
}
9993

10094
const dep = new LCEJSXDependency(new FQN(name), name, 1);
101-
if(!STANDARD_HTML_ELEMENTS.includes(name)) {
95+
if (!STANDARD_HTML_ELEMENTS.includes(name)) {
10296
// Custom Element: try to resolve reference and register dependency
10397
DependencyResolutionProcessor.scheduleFqnResolution(localContexts, name, dep);
10498
DependencyResolutionProcessor.registerDependency(localContexts, name);
10599
}
106100

107-
const jsxDependencyContext = (localContexts.getNextContext(JSXDependencyContextProcessor.JSX_DEPENDENCY_CONTEXT) as [LCEJSXDependency[], number]);
108-
if(jsxDependencyContext) {
101+
const jsxDependencyContext = localContexts.getNextContext(ReactContextKeys.JSX_DEPENDENCIES) as [LCEJSXDependency[], number];
102+
if (jsxDependencyContext) {
109103
jsxDependencyContext[0].push(dep);
110104
}
111-
112105
}
113106
return new Map();
114107
}

0 commit comments

Comments
 (0)