Search in sources :

Example 1 with SYN_KW_EXTENDS_IMPLEMENTS_MIXED_UP

use of org.eclipse.n4js.validation.IssueCodes.SYN_KW_EXTENDS_IMPLEMENTS_MIXED_UP 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)

Aggregations

Joiner (com.google.common.base.Joiner)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 EObject (org.eclipse.emf.ecore.EObject)1 EXTENDS_KEYWORD (org.eclipse.n4js.N4JSLanguageConstants.EXTENDS_KEYWORD)1 IMPLEMENTS_KEYWORD (org.eclipse.n4js.N4JSLanguageConstants.IMPLEMENTS_KEYWORD)1 CatchVariable (org.eclipse.n4js.n4JS.CatchVariable)1 ModifiableElement (org.eclipse.n4js.n4JS.ModifiableElement)1 ModifierUtils (org.eclipse.n4js.n4JS.ModifierUtils)1 N4ClassDefinition (org.eclipse.n4js.n4JS.N4ClassDefinition)1 N4InterfaceDeclaration (org.eclipse.n4js.n4JS.N4InterfaceDeclaration)1 N4JSPackage (org.eclipse.n4js.n4JS.N4JSPackage)1 N4Modifier (org.eclipse.n4js.n4JS.N4Modifier)1 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)1 IdentifiableElement (org.eclipse.n4js.ts.types.IdentifiableElement)1 TClass (org.eclipse.n4js.ts.types.TClass)1