Skip to content

Commit

Permalink
Merge branch 'release-1.0.0' into remove_fractionalHex_and_unnecessar…
Browse files Browse the repository at this point in the history
…y_import_diagnostics
  • Loading branch information
TwitchBronBron authored Dec 17, 2024
2 parents 54a37f5 + 351b9ff commit 3113236
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 12 deletions.
14 changes: 14 additions & 0 deletions src/bscPlugin/validation/ScopeValidator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3088,6 +3088,20 @@ describe('ScopeValidator', () => {
DiagnosticMessages.operatorTypeMismatch('++', 'string').message
]);
});

it('deals with adding int, bool and invalid', () => {
program.setFile('source/util.bs', `
sub doStuff()
print 1 + (true + invalid)
end sub
`);
program.validate();
//should have errors
expectDiagnostics(program, [
DiagnosticMessages.operatorTypeMismatch('+', 'boolean', 'invalid').message
]);
});
});

describe('memberAccessibilityMismatch', () => {
Expand Down
1 change: 1 addition & 0 deletions src/bscPlugin/validation/ScopeValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ export class ScopeValidator {
// Because you need to verify each combination of types
return;
}

const leftIsPrimitive = isPrimitiveType(leftTypeToTest);
const rightIsPrimitive = isPrimitiveType(rightTypeToTest);
const leftIsAny = isDynamicType(leftTypeToTest) || isObjectType(leftTypeToTest);
Expand Down
2 changes: 1 addition & 1 deletion src/lexer/TokenKind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ export const DeclarableTypes = [

/** List of TokenKind that will not break parsing a TypeExpression in Brighterscript*/
export const AllowedTypeIdentifiers = [
...AllowedProperties
...(AllowedProperties.filter(tokenKind => tokenKind !== TokenKind.Invalid))
];


Expand Down
7 changes: 5 additions & 2 deletions src/parser/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as fileUrl from 'file-url';
import type { WalkOptions, WalkVisitor } from '../astUtils/visitors';
import { WalkMode } from '../astUtils/visitors';
import { walk, InternalWalkMode, walkArray } from '../astUtils/visitors';
import { isAALiteralExpression, isAAMemberExpression, isArrayLiteralExpression, isArrayType, isCallExpression, isCallableType, isCallfuncExpression, isComponentType, isDottedGetExpression, isEscapedCharCodeLiteralExpression, isFunctionExpression, isFunctionStatement, isIntegerType, isInterfaceMethodStatement, isLiteralBoolean, isLiteralExpression, isLiteralNumber, isLiteralString, isLongIntegerType, isMethodStatement, isNamespaceStatement, isNewExpression, isPrimitiveType, isReferenceType, isStringType, isTemplateStringExpression, isTypecastExpression, isUnaryExpression, isVariableExpression } from '../astUtils/reflection';
import { isAALiteralExpression, isAAMemberExpression, isArrayLiteralExpression, isArrayType, isCallExpression, isCallableType, isCallfuncExpression, isComponentType, isDottedGetExpression, isEscapedCharCodeLiteralExpression, isFunctionExpression, isFunctionStatement, isIntegerType, isInterfaceMethodStatement, isInvalidType, isLiteralBoolean, isLiteralExpression, isLiteralNumber, isLiteralString, isLongIntegerType, isMethodStatement, isNamespaceStatement, isNewExpression, isPrimitiveType, isReferenceType, isStringType, isTemplateStringExpression, isTypecastExpression, isUnaryExpression, isVariableExpression, isVoidType } from '../astUtils/reflection';
import type { GetTypeOptions, TranspileResult, TypedefProvider } from '../interfaces';
import { TypeChainEntry } from '../interfaces';
import { VoidType } from '../types/VoidType';
Expand Down Expand Up @@ -496,8 +496,11 @@ export class FunctionParameterExpression extends Expression {
const docs = brsDocParser.parseNode(this.findAncestor(isFunctionStatement));
const paramName = this.tokens.name.text;

const paramTypeFromCode = this.typeExpression?.getType({ ...options, flags: SymbolTypeFlag.typetime, typeChain: undefined }) ??
let paramTypeFromCode = this.typeExpression?.getType({ ...options, flags: SymbolTypeFlag.typetime, typeChain: undefined }) ??
this.defaultValue?.getType({ ...options, flags: SymbolTypeFlag.runtime, typeChain: undefined });
if (isInvalidType(paramTypeFromCode) || isVoidType(paramTypeFromCode)) {
paramTypeFromCode = undefined;
}
const paramTypeFromDoc = docs.getParamBscType(paramName, { ...options, fullName: paramName, typeChain: undefined, tableProvider: () => this.getSymbolTable() });

let paramType = util.chooseTypeFromCodeOrDocComment(paramTypeFromCode, paramTypeFromDoc, options) ?? DynamicType.instance;
Expand Down
22 changes: 22 additions & 0 deletions src/parser/Parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,28 @@ describe('parser', () => {
expectZeroDiagnostics(parser);
});

it('does not allow return type as invalid', () => {
let parser = parse(`
function test(x) as invalid
return invalid
end function
`, ParseMode.BrighterScript);
expectDiagnosticsIncludes(parser, [
DiagnosticMessages.expectedIdentifier('as').message
]);
});

it('does not allow param type as invalid', () => {
let parser = parse(`
function test(x as invalid)
return invalid
end function
`, ParseMode.BrighterScript);
expectDiagnosticsIncludes(parser, [
DiagnosticMessages.expectedIdentifier('as').message
]);
});

describe('namespace', () => {
it('allows namespaces declared inside other namespaces', () => {
const parser = parse(`
Expand Down
8 changes: 7 additions & 1 deletion src/parser/Statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3340,8 +3340,14 @@ export class FieldStatement extends Statement implements TypedefProvider {
* Defaults to `DynamicType`
*/
getType(options: GetTypeOptions) {
let initialValueType = this.initialValue?.getType({ ...options, flags: SymbolTypeFlag.runtime });

if (isInvalidType(initialValueType) || isVoidType(initialValueType)) {
initialValueType = undefined;
}

return this.typeExpression?.getType({ ...options, flags: SymbolTypeFlag.typetime }) ??
this.initialValue?.getType({ ...options, flags: SymbolTypeFlag.runtime }) ?? DynamicType.instance;
initialValueType ?? DynamicType.instance;
}

public readonly location: Location | undefined;
Expand Down
6 changes: 4 additions & 2 deletions src/types/ClassType.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SymbolTable } from '../SymbolTable';
import { isClassType, isDynamicType, isObjectType } from '../astUtils/reflection';
import { isClassType, isDynamicType, isInvalidType, isObjectType } from '../astUtils/reflection';
import type { TypeCompatibilityData } from '../interfaces';
import type { BscType } from './BscType';
import { BscTypeKind } from './BscTypeKind';
Expand All @@ -19,7 +19,9 @@ export class ClassType extends InheritableType {
public isTypeCompatible(targetType: BscType, data?: TypeCompatibilityData) {
if (this.isEqual(targetType, data)) {
return true;
} else if (isDynamicType(targetType) ||
} else if (
isInvalidType(targetType) ||
isDynamicType(targetType) ||
isObjectType(targetType) ||
isUnionTypeCompatible(this, targetType, data)) {
return true;
Expand Down
5 changes: 3 additions & 2 deletions src/types/ComponentType.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { GetSymbolTypeOptions } from '../SymbolTable';
import type { SymbolTypeFlag } from '../SymbolTypeFlag';
import { SymbolTable } from '../SymbolTable';
import { isComponentType, isDynamicType, isObjectType } from '../astUtils/reflection';
import { isComponentType, isDynamicType, isInvalidType, isObjectType } from '../astUtils/reflection';
import type { ExtraSymbolData, TypeCompatibilityData } from '../interfaces';
import type { BaseFunctionType } from './BaseFunctionType';
import type { BscType } from './BscType';
Expand All @@ -27,7 +27,8 @@ export class ComponentType extends InheritableType {
public isTypeCompatible(targetType: BscType, data?: TypeCompatibilityData) {
if (this.isEqual(targetType)) {
return true;
} else if (isDynamicType(targetType) ||
} else if (isInvalidType(targetType) ||
isDynamicType(targetType) ||
isObjectType(targetType) ||
isUnionTypeCompatible(this, targetType, data)) {
return true;
Expand Down
7 changes: 5 additions & 2 deletions src/types/InterfaceType.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TypeCompatibilityData } from '../interfaces';
import { SymbolTypeFlag } from '../SymbolTypeFlag';
import { isDynamicType, isInterfaceType, isObjectType } from '../astUtils/reflection';
import { isDynamicType, isInterfaceType, isInvalidType, isObjectType } from '../astUtils/reflection';
import type { BscType } from './BscType';
import { BscTypeKind } from './BscTypeKind';
import { InheritableType } from './InheritableType';
Expand All @@ -18,7 +18,10 @@ export class InterfaceType extends InheritableType {
public readonly kind = BscTypeKind.InterfaceType;

public isTypeCompatible(targetType: BscType, data?: TypeCompatibilityData) {
if (isDynamicType(targetType) || isObjectType(targetType) || isUnionTypeCompatible(this, targetType, data)) {
if (isInvalidType(targetType) ||
isDynamicType(targetType) ||
isObjectType(targetType) ||
isUnionTypeCompatible(this, targetType, data)) {
return true;
}
if (isInterfaceType(targetType)) {
Expand Down
5 changes: 3 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { FunctionType } from './types/FunctionType';
import type { AssignmentStatement, NamespaceStatement } from './parser/Statement';
import type { BscFile } from './files/BscFile';
import type { NamespaceType } from './types/NamespaceType';
import { InvalidType } from './types/InvalidType';

export class Util {
public clearConsole() {
Expand Down Expand Up @@ -1359,7 +1360,7 @@ export class Util {
case TokenKind.IntegerLiteral:
return IntegerType.instance;
case TokenKind.Invalid:
return DynamicType.instance; // TODO: use InvalidType better new InvalidType(token.text);
return new InvalidType(token.text);
case TokenKind.LongInteger:
return new LongIntegerType(token.text);
case TokenKind.LongIntegerLiteral:
Expand Down Expand Up @@ -1390,7 +1391,7 @@ export class Util {
case 'integer':
return new IntegerType(token.text);
case 'invalid':
return DynamicType.instance; // TODO: use InvalidType better new InvalidType(token.text);
return new InvalidType(token.text);
case 'longinteger':
return new LongIntegerType(token.text);
case 'object':
Expand Down

0 comments on commit 3113236

Please sign in to comment.