Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Jan 30, 2023
1 parent b11d0ae commit fe421c7
Show file tree
Hide file tree
Showing 12 changed files with 437 additions and 969 deletions.
2 changes: 1 addition & 1 deletion packages/langium/src/grammar/generated/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function isCondition(item: unknown): item is Condition {
return reflection.isInstance(item, Condition);
}

export type FeatureName = string;
export type FeatureName = 'current' | 'entry' | 'extends' | 'false' | 'fragment' | 'grammar' | 'hidden' | 'import' | 'infer' | 'infers' | 'interface' | 'returns' | 'terminal' | 'true' | 'type' | 'with' | PrimitiveType | string;

export type PrimitiveType = 'Date' | 'bigint' | 'boolean' | 'number' | 'string';

Expand Down
6 changes: 3 additions & 3 deletions packages/langium/src/grammar/type-system/ast-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export function collectValidationAst(grammars: Grammar | Grammar[], documents?:
};
}

function createAstTypes(first: PlainAstTypes, second: PlainAstTypes): AstTypes {
export function createAstTypes(first: PlainAstTypes, second?: PlainAstTypes): AstTypes {
const astTypes: PlainAstTypes = {
interfaces: mergeAndRemoveDuplicates<PlainInterface>(...first.interfaces, ...second.interfaces),
unions: mergeAndRemoveDuplicates<PlainUnion>(...first.unions, ...second.unions),
interfaces: mergeAndRemoveDuplicates<PlainInterface>(...first.interfaces, ...second?.interfaces ?? []),
unions: mergeAndRemoveDuplicates<PlainUnion>(...first.unions, ...second?.unions ?? []),
};

sortInterfacesTopologically(astTypes.interfaces);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import { isNamed } from '../../../references/name-provider';
import { Interface, Type, TypeDefinition, isArrayType, isReferenceType, isUnionType, isSimpleType } from '../../generated/ast';
import { getTypeNameWithoutError, isPrimitiveType } from '../../internal-grammar-util';
import { getTypeName, getTypeNameWithoutError, isPrimitiveType } from '../../internal-grammar-util';
import { PlainAstTypes, PlainInterface, PlainProperty, PlainPropertyType, PlainUnion } from './plain-types';

export function collectDeclaredTypes(interfaces: Interface[], unions: Type[]): PlainAstTypes {
Expand All @@ -25,8 +24,8 @@ export function collectDeclaredTypes(interfaces: Interface[], unions: Type[]): P
}
const superTypes = new Set<string>();
for (const superType of type.superTypes) {
if (superType.ref && isNamed(superType.ref) && superType.ref.name) {
superTypes.add(superType.ref.name);
if (superType.ref) {
superTypes.add(getTypeName(superType.ref));
}
}
const interfaceType: PlainInterface = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { isNamed } from '../../../references/name-provider';
import { MultiMap } from '../../../utils/collections';
import { ParserRule, isAlternatives, isKeyword, Action, isParserRule, isAction, AbstractElement, isGroup, isUnorderedGroup, isAssignment, isRuleCall, Assignment, isCrossReference, RuleCall } from '../../generated/ast';
import { ParserRule, isAlternatives, isKeyword, Action, isParserRule, isAction, AbstractElement, isGroup, isUnorderedGroup, isAssignment, isRuleCall, Assignment, isCrossReference, RuleCall, isTerminalRule } from '../../generated/ast';
import { getTypeNameWithoutError, isOptionalCardinality, getRuleType, isPrimitiveType } from '../../internal-grammar-util';
import { mergePropertyTypes, PlainAstTypes, PlainInterface, PlainProperty, PlainPropertyType, PlainUnion } from './plain-types';

Expand Down Expand Up @@ -254,9 +254,15 @@ function buildDataRuleType(element: AbstractElement, cancel: () => PlainProperty
} else if (isRuleCall(element)) {
const ref = element.rule?.ref;
if (ref) {
return {
value: ref.name
};
if (isTerminalRule(ref)) {
return {
primitive: ref.type?.name ?? 'string'
};
} else {
return {
value: ref.name
};
}
} else {
return cancel();
}
Expand Down Expand Up @@ -511,12 +517,6 @@ function flattenTypes(alternatives: TypePath[]): TypePath[] {
return types;
}

// function isNotInTypeAlternatives(typeAlternatives: PropertyType[]): (type: PropertyType) => boolean {
// return (type: PropertyType) => {
// return !typeAlternatives.some(e => comparePropertyType(e, type));
// };
// }

function buildSuperUnions(interfaces: PlainInterface[]): PlainUnion[] {
const interfaceMap = new Map(interfaces.map(e => [e.name, e]));
const unions: PlainUnion[] = [];
Expand Down
13 changes: 7 additions & 6 deletions packages/langium/src/grammar/validation/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export function registerValidationChecks(services: LangiumGrammarServices): void
validator.checkCrossRefTerminalType,
validator.checkCrossRefType
],
SimpleType: validator.checkFragmentsInTypes
// AtomType: [
// validator.checkAtomTypeRefType,
// validator.checkFragmentsInTypes
Expand Down Expand Up @@ -719,12 +720,12 @@ export class LangiumGrammarValidator {
// }
// }
// }
//
// checkFragmentsInTypes(atomType: ast.AtomType, accept: ValidationAcceptor): void {
// if (ast.isParserRule(atomType.refType?.ref) && atomType.refType?.ref.fragment) {
// accept('error', 'Cannot use rule fragments in types.', { node: atomType, property: 'refType' });
// }
// }

checkFragmentsInTypes(type: ast.SimpleType, accept: ValidationAcceptor): void {
if (ast.isParserRule(type.typeRef?.ref) && type.typeRef?.ref.fragment) {
accept('error', 'Cannot use rule fragments in types.', { node: type, property: 'typeRef' });
}
}

protected checkReferenceToRuleButNotType(type: Reference<ast.AbstractType>): string | undefined {
if (type && ast.isParserRule(type.ref) && !isDataTypeRule(type.ref) && (type.ref.returnType || type.ref.inferredType)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/langium/src/test/langium-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,9 @@ export function expectWarning<T extends AstNode = AstNode, N extends AstNode = A
});
}

export function clearDocuments(services: LangiumServices): void {
export function clearDocuments(services: LangiumServices): Promise<void> {
const allDocs = services.shared.workspace.LangiumDocuments.all.map(x => x.uri).toArray();
services.shared.workspace.DocumentBuilder.update([], allDocs);
return services.shared.workspace.DocumentBuilder.update([], allDocs);
}

export interface DecodedSemanticTokensWithRanges {
Expand Down
67 changes: 33 additions & 34 deletions packages/langium/test/grammar/ast-reflection-interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,40 @@ describe('AST reflection interpreter', () => {

describe('Inheritance with sub- and super-types', () => {

const superType = new InterfaceType('Super', [], [
{
name: 'A',
optional: false,
typeAlternatives: [{
array: true,
reference: false,
types: ['string']
}],
astNodes: new Set()
},
{
name: 'Ref',
optional: true,
typeAlternatives: [{
array: false,
reference: true,
types: ['RefTarget']
}],
astNodes: new Set()
const superType = new InterfaceType('Super', false, false);

superType.properties.push({
name: 'A',
astNodes: new Set(),
optional: false,
type: {
elementType: {
primitive: 'string'
}
}
}, {
name: 'Ref',
astNodes: new Set(),
optional: true,
type: {
referenceType: {
value: superType
}
}
]);
});

const subType = new InterfaceType('Sub', ['Super'], [
{
name: 'B',
optional: false,
typeAlternatives: [{
array: true,
reference: false,
types: ['string']
}],
astNodes: new Set()
const subType = new InterfaceType('Sub', false, false);
subType.properties.push({
name: 'B',
astNodes: new Set(),
optional: false,
type: {
elementType: {
primitive: 'string'
}
}
]);
});
subType.superTypes.add(superType);

const reflectionForInheritance = interpretAstReflection({
interfaces: [superType, subType],
Expand All @@ -70,14 +69,14 @@ describe('AST reflection interpreter', () => {
},
property: 'Ref',
reference: undefined!
})).toBe('RefTarget');
})).toBe('Super');
expect(reflectionForInheritance.getReferenceType({
container: {
$type: 'Sub'
},
property: 'Ref',
reference: undefined!
})).toBe('RefTarget');
})).toBe('Super');
});

test('Creates metadata with super types', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { DiagnosticSeverity } from 'vscode-languageserver';
import { AstNode, createLangiumGrammarServices, EmptyFileSystem, GrammarAST, Properties, streamAllContents, streamContents } from '../../src';
import { Assignment, isAssignment } from '../../src/grammar/generated/ast';
import { Assignment, isAssignment, UnionType } from '../../src/grammar/generated/ast';
import { IssueCodes } from '../../src/grammar/validation/validator';
import { clearDocuments, expectError, expectIssue, expectNoIssues, expectWarning, parseHelper, validationHelper, ValidationResult } from '../../src/test';

Expand Down Expand Up @@ -176,10 +176,10 @@ describe('checkReferenceToRuleButNotType', () => {
});

test('AtomType validation', () => {
const type = validationResult.document.parseResult.value.types[0];
const unionType = validationResult.document.parseResult.value.types[0].type as UnionType;
const missingType = unionType.types[0];
expectError(validationResult, "Could not resolve reference to AbstractType named 'Reference'.", {
node: type,
property: { name: 'typeAlternatives' }
node: missingType
});
});

Expand Down
Loading

0 comments on commit fe421c7

Please sign in to comment.