Skip to content

Commit

Permalink
Support for 'super'
Browse files Browse the repository at this point in the history
  • Loading branch information
sashi0034 committed Jan 4, 2025
1 parent 2535c63 commit 4dcb064
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions server/src/compile/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import {
ResolvedType,
SymbolFunction,
SymbolKind,
SymbolObject,
SymbolScope,
SymbolType,
SymbolVariable
Expand Down Expand Up @@ -106,8 +107,8 @@ import {
} from "./symbolUtils";
import {Mutable} from "../utils/utilities";
import {getGlobalSettings} from "../code/settings";
import assert = require("node:assert");
import {createVirtualToken} from "./tokenUtils";
import assert = require("node:assert");

type HoistingQueue = (() => void)[];

Expand Down Expand Up @@ -218,12 +219,30 @@ function hoistClass(parentScope: SymbolScope, nodeClass: NodeClass, analyzing: A
const templateTypes = hoistClassTemplateTypes(scope, nodeClass.typeTemplates);
if (templateTypes.length > 0) symbol.templateTypes = templateTypes;

const baseList = hoistBaseList(scope, nodeClass);
if (baseList !== undefined) symbol.baseList = baseList;
symbol.baseList = hoistBaseList(scope, nodeClass);

hoisting.push(() => {
hoistClassMembers(scope, nodeClass, analyzing, hoisting);
if (baseList !== undefined) copyBaseMembers(scope, baseList);

hoisting.push(() => {
if (symbol.baseList === undefined) return;

// Copy the members of the base class
copyBaseMembers(scope, symbol.baseList);

// Check to insert the super constructor
const primeBase = symbol.baseList.length >= 1 ? symbol.baseList[0] : undefined;
const superConstructor = findConstructorForResolvedType(primeBase);
if (superConstructor?.symbolKind === SymbolKind.Function) {
const superSymbol: Mutable<SymbolFunction> = {...superConstructor};

const declaredPlace: Mutable<ParsedToken> = createVirtualToken(TokenKind.Identifier, 'super');
declaredPlace.location = {...superSymbol.declaredPlace.location};

superSymbol.declaredPlace = declaredPlace;
insertSymbolObject(scope.symbolMap, superSymbol);
}
});
});

pushHintOfCompletionScopeToParent(parentScope, scope, nodeClass.nodeRange);
Expand Down Expand Up @@ -1095,11 +1114,7 @@ function analyzeConstructorCaller(
callerArgList: NodeArgList,
constructorType: ResolvedType
): ResolvedType | undefined {
const constructorIdentifier = constructorType.symbolType.declaredPlace.text;
if (constructorType.sourceScope === undefined) return undefined;

const classScope = findScopeShallowly(constructorType.sourceScope, constructorIdentifier);
const constructor = classScope !== undefined ? findSymbolShallowly(classScope, constructorIdentifier) : undefined;
const constructor = findConstructorForResolvedType(constructorType);
if (constructor === undefined || constructor.symbolKind !== SymbolKind.Function) {
return analyzeBuiltinConstructorCaller(scope, callerIdentifier, callerArgList, constructorType);
}
Expand All @@ -1108,6 +1123,14 @@ function analyzeConstructorCaller(
return constructorType;
}

function findConstructorForResolvedType(resolvedType: ResolvedType | undefined): SymbolObject | undefined {
if (resolvedType?.sourceScope === undefined) return undefined;

const constructorIdentifier = resolvedType.symbolType.declaredPlace.text;
const classScope = findScopeShallowly(resolvedType.sourceScope, constructorIdentifier);
return classScope !== undefined ? findSymbolShallowly(classScope, constructorIdentifier) : undefined;
}

function analyzeBuiltinConstructorCaller(
scope: SymbolScope,
callerIdentifier: ParsedToken,
Expand Down

0 comments on commit 4dcb064

Please sign in to comment.