Search in sources :

Example 16 with TInterface

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

the class N4JSSyntaxValidator method checkInterfaceDeclaration.

/**
 * Checks that no "with" or "role" 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.
 * <p>
 * "role" will be removed in grammar.
 */
@Check
public void checkInterfaceDeclaration(N4InterfaceDeclaration n4InterfaceDecl) {
    ICompositeNode node = NodeModelUtils.findActualNodeFor(n4InterfaceDecl);
    ILeafNode keywordNode;
    keywordNode = findLeafWithKeyword(n4InterfaceDecl, "{", node, IMPLEMENTS_KEYWORD, false);
    if (keywordNode != null) {
        TInterface tinterface = n4InterfaceDecl.getDefinedTypeAsInterface();
        if (tinterface == null) {
            // avoid consequential errors
            return;
        }
        if (tinterface.getSuperInterfaceRefs().isEmpty()) {
            // ok
            return;
        }
        if (tinterface.getSuperInterfaceRefs().stream().allMatch(superTypeRef -> superTypeRef.getDeclaredType() instanceof TInterface)) {
            List<? extends IdentifiableElement> interfaces = tinterface.getSuperInterfaceRefs().stream().flatMap((ParameterizedTypeRef ref) -> {
                Type declaredType = ref.getDeclaredType();
                if (declaredType instanceof TInterface) {
                    return Stream.of((TInterface) declaredType);
                }
                return Stream.empty();
            }).collect(Collectors.toList());
            String message = getMessageForSYN_KW_EXTENDS_IMPLEMENTS_MIXED_UP(validatorMessageHelper.description(tinterface), "implement", "interface" + (interfaces.size() > 1 ? "s " : " ") + validatorMessageHelper.names(interfaces), EXTENDS_KEYWORD);
            addIssue(message, n4InterfaceDecl, keywordNode.getTotalOffset(), keywordNode.getLength(), SYN_KW_EXTENDS_IMPLEMENTS_MIXED_UP);
        }
    }
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) Type(org.eclipse.n4js.ts.types.Type) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) TInterface(org.eclipse.n4js.ts.types.TInterface) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Check(org.eclipse.xtext.validation.Check)

Aggregations

TInterface (org.eclipse.n4js.ts.types.TInterface)16 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)9 Type (org.eclipse.n4js.ts.types.Type)8 EObject (org.eclipse.emf.ecore.EObject)7 TClass (org.eclipse.n4js.ts.types.TClass)7 TClassifier (org.eclipse.n4js.ts.types.TClassifier)6 TField (org.eclipse.n4js.ts.types.TField)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 Stream (java.util.stream.Stream)4 PrimitiveType (org.eclipse.n4js.ts.types.PrimitiveType)4 TGetter (org.eclipse.n4js.ts.types.TGetter)4 TMember (org.eclipse.n4js.ts.types.TMember)4 TSetter (org.eclipse.n4js.ts.types.TSetter)4 AccessorTuple (org.eclipse.n4js.ts.types.util.AccessorTuple)4 MemberList (org.eclipse.n4js.ts.types.util.MemberList)4 HashSet (java.util.HashSet)3 Optional (java.util.Optional)3 N4InterfaceDeclaration (org.eclipse.n4js.n4JS.N4InterfaceDeclaration)3