Search in sources :

Example 1 with N4ClassDefinition

use of org.eclipse.n4js.n4JS.N4ClassDefinition in project n4js by eclipse.

the class N4JSMemberRedefinitionValidator method checkUnpairedAccessorFilling.

private void checkUnpairedAccessorFilling(MemberMatrix mm, N4ClassifierDefinition definition) {
    if (definition.getDefinedType().isStaticPolyfill() && mm.hasMixedAccessorPair()) {
        FieldAccessor ownedAccessor = (FieldAccessor) Iterables.getFirst(mm.owned(), null);
        if (null == ownedAccessor) {
            // Should not happen, a mixed accessor pair implies at least one owned member
            return;
        }
        if (!(definition instanceof N4ClassDefinition)) {
            // Non-class static polyfills aren't allowed. Validated somewhere else.
            return;
        }
        TClass filledClass = MemberRedefinitionUtils.getFilledClass((N4ClassDefinition) definition);
        if (null == filledClass) {
            // Invalid static polyfill class. Validated somewhere else.
            return;
        }
        // Iterate over all inherited members
        SourceAwareIterator memberIterator = mm.actuallyInheritedAndMixedMembers();
        while (memberIterator.hasNext()) {
            TMember next = memberIterator.next();
            ContainerType<?> containingType = next.getContainingType();
            // Issue an error if the member isn't owned by the filled class
            if (containingType != filledClass) {
                messageMissingOwnedAccessor(ownedAccessor);
            }
        }
    }
}
Also used : SourceAwareIterator(org.eclipse.n4js.validation.validators.utils.MemberMatrix.SourceAwareIterator) TMember(org.eclipse.n4js.ts.types.TMember) FieldAccessor(org.eclipse.n4js.ts.types.FieldAccessor) TClass(org.eclipse.n4js.ts.types.TClass) N4ClassDefinition(org.eclipse.n4js.n4JS.N4ClassDefinition)

Example 2 with N4ClassDefinition

use of org.eclipse.n4js.n4JS.N4ClassDefinition 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 N4ClassDefinition

use of org.eclipse.n4js.n4JS.N4ClassDefinition in project n4js by eclipse.

the class FGUtils method isCFContainer.

/**
 * @return true iff the given {@link ControlFlowElement} is a container such as a function's body.
 */
public static boolean isCFContainer(EObject cfe) {
    boolean isScript = cfe instanceof Script;
    boolean isBlock = cfe instanceof Block;
    boolean isExpression = cfe instanceof Expression;
    boolean isBindingPattern = cfe instanceof BindingPattern;
    if (!isScript && !isBlock && !isExpression && !isBindingPattern) {
        return false;
    }
    EObject cfeContainer = cfe.eContainer();
    EObject cfeContainer2 = (cfeContainer == null) ? null : cfeContainer.eContainer();
    boolean containerIsFunctionDeclaration = cfeContainer instanceof FunctionDeclaration;
    boolean containerIsFunctionDefinition = cfeContainer instanceof FunctionDefinition;
    boolean containerIsFieldAccessor = cfeContainer instanceof FieldAccessor;
    boolean containerIsFormalParameter = cfeContainer instanceof FormalParameter;
    boolean containerIsFieldDeclaration = cfeContainer instanceof N4FieldDeclaration;
    boolean containerIsAnnotationArgument = cfeContainer instanceof AnnotationArgument;
    boolean containerIsLiteralOrComputedPropertyName = cfeContainer instanceof LiteralOrComputedPropertyName;
    boolean containerIsExportSpecifier = cfeContainer instanceof ExportSpecifier;
    boolean containerIsExportDeclaration = cfeContainer instanceof ExportDeclaration;
    boolean containerIsN4ClassDefinition = cfeContainer instanceof N4ClassDefinition;
    boolean container2IsN4FieldDeclaration = cfeContainer2 instanceof N4FieldDeclaration;
    boolean isCFContainer = false;
    isCFContainer |= isScript;
    isCFContainer |= isBlock && containerIsFunctionDeclaration;
    isCFContainer |= isBlock && containerIsFunctionDefinition;
    isCFContainer |= isBlock && containerIsFieldAccessor;
    isCFContainer |= isBindingPattern && containerIsFormalParameter;
    isCFContainer |= isExpression && containerIsFormalParameter;
    isCFContainer |= isExpression && containerIsFieldDeclaration;
    isCFContainer |= isExpression && containerIsAnnotationArgument;
    isCFContainer |= isExpression && containerIsLiteralOrComputedPropertyName && container2IsN4FieldDeclaration;
    isCFContainer |= isExpression && containerIsN4ClassDefinition;
    isCFContainer |= isExpression && containerIsExportSpecifier;
    isCFContainer |= isExpression && containerIsExportDeclaration;
    return isCFContainer;
}
Also used : FormalParameter(org.eclipse.n4js.n4JS.FormalParameter) Script(org.eclipse.n4js.n4JS.Script) BindingPattern(org.eclipse.n4js.n4JS.BindingPattern) N4FieldDeclaration(org.eclipse.n4js.n4JS.N4FieldDeclaration) ExportSpecifier(org.eclipse.n4js.n4JS.ExportSpecifier) ExportDeclaration(org.eclipse.n4js.n4JS.ExportDeclaration) FieldAccessor(org.eclipse.n4js.n4JS.FieldAccessor) AnnotationArgument(org.eclipse.n4js.n4JS.AnnotationArgument) N4ClassDefinition(org.eclipse.n4js.n4JS.N4ClassDefinition) FunctionDeclaration(org.eclipse.n4js.n4JS.FunctionDeclaration) Expression(org.eclipse.n4js.n4JS.Expression) EObject(org.eclipse.emf.ecore.EObject) CatchBlock(org.eclipse.n4js.n4JS.CatchBlock) Block(org.eclipse.n4js.n4JS.Block) FunctionDefinition(org.eclipse.n4js.n4JS.FunctionDefinition) LiteralOrComputedPropertyName(org.eclipse.n4js.n4JS.LiteralOrComputedPropertyName)

Aggregations

N4ClassDefinition (org.eclipse.n4js.n4JS.N4ClassDefinition)3 EObject (org.eclipse.emf.ecore.EObject)2 TClass (org.eclipse.n4js.ts.types.TClass)2 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 EXTENDS_KEYWORD (org.eclipse.n4js.N4JSLanguageConstants.EXTENDS_KEYWORD)1 IMPLEMENTS_KEYWORD (org.eclipse.n4js.N4JSLanguageConstants.IMPLEMENTS_KEYWORD)1 AnnotationArgument (org.eclipse.n4js.n4JS.AnnotationArgument)1 BindingPattern (org.eclipse.n4js.n4JS.BindingPattern)1 Block (org.eclipse.n4js.n4JS.Block)1 CatchBlock (org.eclipse.n4js.n4JS.CatchBlock)1 CatchVariable (org.eclipse.n4js.n4JS.CatchVariable)1 ExportDeclaration (org.eclipse.n4js.n4JS.ExportDeclaration)1 ExportSpecifier (org.eclipse.n4js.n4JS.ExportSpecifier)1 Expression (org.eclipse.n4js.n4JS.Expression)1