Search in sources :

Example 1 with IdentifiableElement

use of org.eclipse.n4js.ts.types.IdentifiableElement in project n4js by eclipse.

the class N4JSEnumValidator method checkUsageOfStringBasedEnum.

/**
 * See N4JS Specification, Req. IDE-41, Nr. 6.
 */
@Check
public void checkUsageOfStringBasedEnum(IdentifierRef identRef) {
    final IdentifiableElement id = identRef.getId();
    if (id == null || id.eIsProxy()) {
        return;
    }
    if (!(id instanceof TEnum)) {
        return;
    }
    final TEnum tEnum = (TEnum) id;
    if (!AnnotationDefinition.STRING_BASED.hasAnnotation(tEnum)) {
        return;
    }
    // we now have an IdentifierRef pointing to a string-based enum ...
    final EObject parent = N4JSASTUtils.skipParenExpressionUpward(identRef.eContainer());
    final ParameterizedPropertyAccessExpression parentPAE = parent instanceof ParameterizedPropertyAccessExpression ? (ParameterizedPropertyAccessExpression) parent : null;
    final IdentifiableElement prop = parentPAE != null ? parentPAE.getProperty() : null;
    if (prop != null) {
        if (prop.eIsProxy()) {
            // unnecessary duplicate error
            return;
        }
        if (tEnum.getLiterals().contains(prop)) {
            // reference to one of tEnum's literals -> valid usage!
            return;
        }
        final RuleEnvironment G = RuleEnvironmentExtensions.newRuleEnvironment(identRef);
        final TMember getterLiterals = RuleEnvironmentExtensions.n4StringBasedEnumType(G).findOwnedMember("literals", false, true);
        if (prop == getterLiterals) {
            // reference to static getter 'literals' in N4StringBasedEnum -> valid usage!
            return;
        }
    }
    // invalid usage!
    addIssue(getMessageForENM_INVALID_USE_OF_STRINGBASED_ENUM(), identRef, ENM_INVALID_USE_OF_STRINGBASED_ENUM);
}
Also used : ParameterizedPropertyAccessExpression(org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression) TEnum(org.eclipse.n4js.ts.types.TEnum) EObject(org.eclipse.emf.ecore.EObject) IdentifiableElement(org.eclipse.n4js.ts.types.IdentifiableElement) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) TMember(org.eclipse.n4js.ts.types.TMember) Check(org.eclipse.xtext.validation.Check)

Example 2 with IdentifiableElement

use of org.eclipse.n4js.ts.types.IdentifiableElement in project n4js by eclipse.

the class N4JSSyntaxValidator method checkClassDefinition.

/**
 * Checks that no "with" is used and that list of implemented interfaces is separated with commas and not with
 * keywords. These checks (with some warnings created instead of errors) should help the transition from roles to
 * interfaces. However, they may be useful later on as well, e.g., if an interface is manually refactored into a
 * class or vice versa.
 * <p>
 * Note that "with" is used in Dart for roles, so maybe it is useful to have a user-friendly message instead of a
 * parser error.
 */
@Check
public void checkClassDefinition(N4ClassDefinition n4ClassDefinition) {
    holdsCorrectOrderOfExtendsImplements(n4ClassDefinition);
    ICompositeNode node = NodeModelUtils.findActualNodeFor(n4ClassDefinition);
    ILeafNode keywordNode = findSecondLeafWithKeyword(n4ClassDefinition, "{", node, EXTENDS_KEYWORD, false);
    if (keywordNode != null) {
        TClass tclass = n4ClassDefinition.getDefinedTypeAsClass();
        if (tclass == null) {
            // avoid consequential errors
            return;
        }
        if (StreamSupport.stream(tclass.getImplementedInterfaceRefs().spliterator(), false).allMatch(superTypeRef -> superTypeRef.getDeclaredType() instanceof TInterface)) {
            List<? extends IdentifiableElement> interfaces = StreamSupport.stream(tclass.getImplementedInterfaceRefs().spliterator(), false).map(ref -> (TInterface) (ref.getDeclaredType())).collect(Collectors.toList());
            String message = getMessageForSYN_KW_EXTENDS_IMPLEMENTS_MIXED_UP(validatorMessageHelper.description(tclass), "extend", "interface" + (interfaces.size() > 1 ? "s " : " ") + validatorMessageHelper.names(interfaces), IMPLEMENTS_KEYWORD);
            addIssue(message, n4ClassDefinition, keywordNode.getTotalOffset(), keywordNode.getLength(), SYN_KW_EXTENDS_IMPLEMENTS_MIXED_UP);
        }
    }
}
Also used : IssueCodes(org.eclipse.n4js.validation.IssueCodes) AbstractElement(org.eclipse.xtext.AbstractElement) IMPLEMENTS_KEYWORD(org.eclipse.n4js.N4JSLanguageConstants.IMPLEMENTS_KEYWORD) N4JSPackage(org.eclipse.n4js.n4JS.N4JSPackage) SYN_KW_EXTENDS_IMPLEMENTS_MIXED_UP(org.eclipse.n4js.validation.IssueCodes.SYN_KW_EXTENDS_IMPLEMENTS_MIXED_UP) TClass(org.eclipse.n4js.ts.types.TClass) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) NodeModelUtils(org.eclipse.xtext.nodemodel.util.NodeModelUtils) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) HashSet(java.util.HashSet) ModifiableElement(org.eclipse.n4js.n4JS.ModifiableElement) TInterface(org.eclipse.n4js.ts.types.TInterface) Type(org.eclipse.n4js.ts.types.Type) IdentifiableElement(org.eclipse.n4js.ts.types.IdentifiableElement) N4Modifier(org.eclipse.n4js.n4JS.N4Modifier) AbstractN4JSDeclarativeValidator(org.eclipse.n4js.validation.AbstractN4JSDeclarativeValidator) AST_CATCH_VAR_TYPED(org.eclipse.n4js.validation.IssueCodes.AST_CATCH_VAR_TYPED) IssueCodes.getMessageForSYN_KW_EXTENDS_IMPLEMENTS_MIXED_UP(org.eclipse.n4js.validation.IssueCodes.getMessageForSYN_KW_EXTENDS_IMPLEMENTS_MIXED_UP) StreamSupport(java.util.stream.StreamSupport) N4InterfaceDeclaration(org.eclipse.n4js.n4JS.N4InterfaceDeclaration) INode(org.eclipse.xtext.nodemodel.INode) Check(org.eclipse.xtext.validation.Check) N4ClassDefinition(org.eclipse.n4js.n4JS.N4ClassDefinition) BidiTreeIterator(org.eclipse.xtext.nodemodel.BidiTreeIterator) IssueCodes.getMessageForSYN_KW_EXTENDS_IMPLEMENTS_WRONG_ORDER(org.eclipse.n4js.validation.IssueCodes.getMessageForSYN_KW_EXTENDS_IMPLEMENTS_WRONG_ORDER) Set(java.util.Set) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Collectors(java.util.stream.Collectors) Alternatives(org.eclipse.xtext.Alternatives) Keyword(org.eclipse.xtext.Keyword) EXTENDS_KEYWORD(org.eclipse.n4js.N4JSLanguageConstants.EXTENDS_KEYWORD) List(java.util.List) Stream(java.util.stream.Stream) IssueCodes.getMessageForAST_CATCH_VAR_TYPED(org.eclipse.n4js.validation.IssueCodes.getMessageForAST_CATCH_VAR_TYPED) ModifierUtils(org.eclipse.n4js.n4JS.ModifierUtils) CatchVariable(org.eclipse.n4js.n4JS.CatchVariable) EValidatorRegistrar(org.eclipse.xtext.validation.EValidatorRegistrar) Joiner(com.google.common.base.Joiner) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) TInterface(org.eclipse.n4js.ts.types.TInterface) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) TClass(org.eclipse.n4js.ts.types.TClass) Check(org.eclipse.xtext.validation.Check)

Example 3 with IdentifiableElement

use of org.eclipse.n4js.ts.types.IdentifiableElement in project n4js by eclipse.

the class AmbiguousImportDescription method getMessage.

@Override
public String getMessage() {
    StringBuilder typeListStr = new StringBuilder();
    IdentifiableElement first = (IdentifiableElement) EcoreUtil.resolve(getEObjectOrProxy(), context);
    String typeIdent = first instanceof Type ? "type" : "variable";
    TModule module = (TModule) first.eContainer();
    typeListStr.append(module.getQualifiedName());
    Set<IdentifiableElement> uniqueTypes = Sets.newLinkedHashSet(elements);
    uniqueTypes.remove(first);
    Iterator<IdentifiableElement> iter = uniqueTypes.iterator();
    while (iter.hasNext()) {
        IdentifiableElement type = iter.next();
        if (iter.hasNext()) {
            typeListStr.append(", ");
        } else {
            typeListStr.append(" and ");
        }
        typeListStr.append(((TModule) type.eContainer()).getQualifiedName());
    }
    if (this.issueCode == IssueCodes.IMP_AMBIGUOUS_WILDCARD) {
        return IssueCodes.getMessageForIMP_AMBIGUOUS_WILDCARD(typeIdent, getName(), typeListStr.toString());
    } else if (this.issueCode == IssueCodes.IMP_AMBIGUOUS) {
        return IssueCodes.getMessageForIMP_AMBIGUOUS(typeIdent, getName(), typeListStr.toString());
    } else if (this.issueCode == IssueCodes.IMP_DUPLICATE_NAMESPACE) {
        return IssueCodes.getMessageForIMP_DUPLICATE_NAMESPACE(getName(), "stub");
    }
    return "Unknown ambiguous import issue: " + this.issueCode + " for " + context + ".";
}
Also used : Type(org.eclipse.n4js.ts.types.Type) IdentifiableElement(org.eclipse.n4js.ts.types.IdentifiableElement) TModule(org.eclipse.n4js.ts.types.TModule)

Example 4 with IdentifiableElement

use of org.eclipse.n4js.ts.types.IdentifiableElement in project n4js by eclipse.

the class InternalTypeSystem method applyRuleTypeIdentifierRef.

protected Result<TypeRef> applyRuleTypeIdentifierRef(final RuleEnvironment G, final RuleApplicationTrace _trace_, final IdentifierRef idref) throws RuleFailedException {
    // output parameter
    TypeRef T = null;
    /* G |- idref.id : T */
    IdentifiableElement _id = idref.getId();
    Result<TypeRef> result = typeInternal(G, _trace_, _id);
    checkAssignableTo(result.getFirst(), TypeRef.class);
    T = (TypeRef) result.getFirst();
    T = this.versionResolver.<TypeRef, IdentifierRef>resolveVersion(T, idref);
    if (((idref.eContainer() instanceof ParameterizedCallExpression) && (idref.eContainmentFeature() == N4JSPackage.eINSTANCE.getParameterizedCallExpression_Target()))) {
        final TMethod callableCtorFunction = this.typeSystemHelper.getCallableClassConstructorFunction(G, T);
        if ((callableCtorFunction != null)) {
            T = TypeExtensions.ref(callableCtorFunction);
        }
    }
    return new Result<TypeRef>(T);
}
Also used : TMethod(org.eclipse.n4js.ts.types.TMethod) ThisTypeRef(org.eclipse.n4js.ts.typeRefs.ThisTypeRef) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) BaseTypeRef(org.eclipse.n4js.ts.typeRefs.BaseTypeRef) FunctionTypeRef(org.eclipse.n4js.ts.typeRefs.FunctionTypeRef) ExistentialTypeRef(org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef) BoundThisTypeRef(org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef) StructuralTypeRef(org.eclipse.n4js.ts.typeRefs.StructuralTypeRef) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) StaticBaseTypeRef(org.eclipse.n4js.ts.typeRefs.StaticBaseTypeRef) ComposedTypeRef(org.eclipse.n4js.ts.typeRefs.ComposedTypeRef) UnknownTypeRef(org.eclipse.n4js.ts.typeRefs.UnknownTypeRef) ParameterizedCallExpression(org.eclipse.n4js.n4JS.ParameterizedCallExpression) IdentifiableElement(org.eclipse.n4js.ts.types.IdentifiableElement) Result(org.eclipse.xsemantics.runtime.Result) StructuralTypingResult(org.eclipse.n4js.typesystem.StructuralTypingResult)

Example 5 with IdentifiableElement

use of org.eclipse.n4js.ts.types.IdentifiableElement in project n4js by eclipse.

the class InternalTypeSystem method applyRuleExpectedTypeInJSXPropertyAttribute.

protected Result<TypeRef> applyRuleExpectedTypeInJSXPropertyAttribute(final RuleEnvironment G, final RuleApplicationTrace _trace_, final JSXPropertyAttribute container, final Expression expr) throws RuleFailedException {
    // output parameter
    TypeRef T = null;
    T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
    final EObject jsxElem = container.eContainer();
    if ((jsxElem instanceof JSXElement)) {
        final TypeRef propsTypeRef = this.reactHelper.getPropsType(((JSXElement) jsxElem));
        if ((propsTypeRef != null)) {
            final RuleEnvironment G2 = RuleEnvironmentExtensions.wrap(G);
            this.typeSystemHelper.addSubstitutions(G2, propsTypeRef);
            RuleEnvironmentExtensions.addThisType(G2, propsTypeRef);
            /* G2 |- container.getProperty() : var TypeRef propertyTypeRef */
            IdentifiableElement _property = container.getProperty();
            TypeRef propertyTypeRef = null;
            Result<TypeRef> result = typeInternal(G2, _trace_, _property);
            checkAssignableTo(result.getFirst(), TypeRef.class);
            propertyTypeRef = (TypeRef) result.getFirst();
            /* G2 |- propertyTypeRef ~> T */
            Result<TypeArgument> result_1 = substTypeVariablesInternal(G2, _trace_, propertyTypeRef);
            checkAssignableTo(result_1.getFirst(), TypeRef.class);
            T = (TypeRef) result_1.getFirst();
        }
    }
    return new Result<TypeRef>(T);
}
Also used : JSXElement(org.eclipse.n4js.n4JS.JSXElement) ThisTypeRef(org.eclipse.n4js.ts.typeRefs.ThisTypeRef) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) BaseTypeRef(org.eclipse.n4js.ts.typeRefs.BaseTypeRef) FunctionTypeRef(org.eclipse.n4js.ts.typeRefs.FunctionTypeRef) ExistentialTypeRef(org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef) BoundThisTypeRef(org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef) StructuralTypeRef(org.eclipse.n4js.ts.typeRefs.StructuralTypeRef) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) StaticBaseTypeRef(org.eclipse.n4js.ts.typeRefs.StaticBaseTypeRef) ComposedTypeRef(org.eclipse.n4js.ts.typeRefs.ComposedTypeRef) UnknownTypeRef(org.eclipse.n4js.ts.typeRefs.UnknownTypeRef) EObject(org.eclipse.emf.ecore.EObject) IdentifiableElement(org.eclipse.n4js.ts.types.IdentifiableElement) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) TypeArgument(org.eclipse.n4js.ts.typeRefs.TypeArgument) Result(org.eclipse.xsemantics.runtime.Result) StructuralTypingResult(org.eclipse.n4js.typesystem.StructuralTypingResult)

Aggregations

IdentifiableElement (org.eclipse.n4js.ts.types.IdentifiableElement)27 EObject (org.eclipse.emf.ecore.EObject)12 ExportDeclaration (org.eclipse.n4js.n4JS.ExportDeclaration)7 ExportableElement (org.eclipse.n4js.n4JS.ExportableElement)6 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)6 NamedElement (org.eclipse.n4js.n4JS.NamedElement)5 ParameterizedPropertyAccessExpression (org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression)5 BaseTypeRef (org.eclipse.n4js.ts.typeRefs.BaseTypeRef)5 BoundThisTypeRef (org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef)5 ComposedTypeRef (org.eclipse.n4js.ts.typeRefs.ComposedTypeRef)5 ExistentialTypeRef (org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef)5 FunctionTypeRef (org.eclipse.n4js.ts.typeRefs.FunctionTypeRef)5 StaticBaseTypeRef (org.eclipse.n4js.ts.typeRefs.StaticBaseTypeRef)5 StructuralTypeRef (org.eclipse.n4js.ts.typeRefs.StructuralTypeRef)5 ThisTypeRef (org.eclipse.n4js.ts.typeRefs.ThisTypeRef)5 TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)5 TypeTypeRef (org.eclipse.n4js.ts.typeRefs.TypeTypeRef)5 UnknownTypeRef (org.eclipse.n4js.ts.typeRefs.UnknownTypeRef)5 StructuralTypingResult (org.eclipse.n4js.typesystem.StructuralTypingResult)5 Result (org.eclipse.xsemantics.runtime.Result)5