Skip to content
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

Reduce null safety issues in Statement and Expression subclasses #1033

Merged
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
3 changes: 1 addition & 2 deletions src/bscPlugin/hover/HoverProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SourceNode } from 'source-map';
import { isBrsFile, isFunctionType, isXmlFile } from '../../astUtils/reflection';
import type { BrsFile } from '../../files/BrsFile';
import type { XmlFile } from '../../files/XmlFile';
Expand Down Expand Up @@ -67,7 +66,7 @@ export class HoverProcessor {
//find a constant with this name
const constant = scope?.getConstFileLink(fullName, containingNamespace);
if (constant) {
const constantValue = new SourceNode(null, null, null, constant.item.value.transpile(new BrsTranspileState(file))).toString();
const constantValue = util.sourceNodeFromTranspileResult(null, null, null, constant.item.value.transpile(new BrsTranspileState(file))).toString();
return {
contents: this.buildContentsWithDocs(fence(`const ${constant.item.fullName} = ${constantValue}`), constant.item.tokens.const),
range: token.range
Expand Down
8 changes: 4 additions & 4 deletions src/files/BrsFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1238,15 +1238,15 @@ export class BrsFile {
/**
* Determine if the callee (i.e. function name) is a known function declared on the given namespace.
*/
public calleeIsKnownNamespaceFunction(callee: Expression, namespaceName: string) {
public calleeIsKnownNamespaceFunction(callee: Expression, namespaceName: string | undefined) {
//if we have a variable and a namespace
if (isVariableExpression(callee) && namespaceName) {
let lowerCalleeName = callee?.name?.text?.toLowerCase();
if (lowerCalleeName) {
let scopes = this.program.getScopesForFile(this);
for (let scope of scopes) {
let namespace = scope.namespaceLookup.get(namespaceName.toLowerCase());
if (namespace.functionStatements[lowerCalleeName]) {
if (namespace?.functionStatements[lowerCalleeName]) {
return true;
}
}
Expand Down Expand Up @@ -1477,7 +1477,7 @@ export class BrsFile {
let transpileResult: SourceNode | undefined;

if (this.needsTranspiled) {
transpileResult = new SourceNode(null, null, state.srcPath, this.ast.transpile(state));
transpileResult = util.sourceNodeFromTranspileResult(null, null, state.srcPath, this.ast.transpile(state));
} else if (this.program.options.sourceMap) {
//emit code as-is with a simple map to the original file location
transpileResult = util.simpleMap(state.srcPath, this.fileContents);
Expand Down Expand Up @@ -1506,7 +1506,7 @@ export class BrsFile {
public getTypedef() {
const state = new BrsTranspileState(this);
const typedef = this.ast.getTypedef(state);
const programNode = new SourceNode(null, null, this.srcPath, typedef);
const programNode = util.sourceNodeFromTranspileResult(null, null, this.srcPath, typedef);
return programNode.toString();
}

Expand Down
3 changes: 2 additions & 1 deletion src/files/XmlFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,8 @@ export class XmlFile {
//temporarily add the missing imports as script tags
this.ast.component.scripts = publishableScripts;

transpileResult = new SourceNode(null, null, state.srcPath, this.parser.ast.transpile(state));

transpileResult = util.sourceNodeFromTranspileResult(null, null, state.srcPath, this.parser.ast.transpile(state));

//restore the original scripts array
this.ast.component.scripts = originalScripts;
Expand Down
11 changes: 9 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,17 @@ export interface SemanticToken {
}

export interface TypedefProvider {
getTypedef(state: TranspileState): Array<SourceNode | string>;
getTypedef(state: TranspileState): TranspileResult;
}

export type TranspileResult = Array<(string | SourceNode)>;
export type TranspileResult = Array<(string | SourceNode | TranspileResult)>;

/**
* This is the type that the SourceNode class is declared as taking in its constructor.
* The actual type that SourceNode accepts is the more permissive TranspileResult, but
* we need to use this declared type for some type casts.
*/
export type FlattenedTranspileResult = Array<string | SourceNode>;

export type FileResolver = (srcPath: string) => string | undefined | Thenable<string | undefined> | void;

Expand Down
2 changes: 1 addition & 1 deletion src/parser/AstNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export abstract class AstNode {
/**
* The starting and ending location of the node.
*/
public abstract range: Range;
public abstract range: Range | undefined;

public abstract transpile(state: BrsTranspileState): TranspileResult;

Expand Down
2 changes: 1 addition & 1 deletion src/parser/BrsTranspileState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class BrsTranspileState extends TranspileState {
* Used to assist blocks in knowing when to add a comment statement to the same line as the first line of the parent
*/
lineage = [] as Array<{
range: Range;
range?: Range;
}>;

/**
Expand Down
Loading