Search in sources :

Example 6 with ImportDeclaration

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

the class StaticWriteAccessFilterScope method isAccepted.

@Override
protected boolean isAccepted(IEObjectDescription description) {
    EObject proxyOrInstance = description.getEObjectOrProxy();
    if (proxyOrInstance instanceof TMember && !proxyOrInstance.eIsProxy()) {
        TMember member = (TMember) proxyOrInstance;
        // this particular message to hide the better one.
        if (member.isStatic() && member.isWriteable() && /* i.e. (member.isField(), not const || member.isSetter()) */
        isWriteAccess()) {
            ContainerType<?> memberType = member.getContainingType();
            memberTypeName = memberType.getName();
            // Access only allowed for Direct access, so AST must be IdentifierRef.
            final boolean isTargetGivenByIdentifier = getTarget() instanceof IdentifierRef;
            if (!isTargetGivenByIdentifier) {
                // Not an IdentifierRef --> disallowed for write access.
                return false;
            }
            IdentifierRef idref = (IdentifierRef) getTarget();
            // this also covers aliased imports:
            if (idref.getId().getName().equals(memberTypeName)) {
                // correct name.
                return true;
            } else {
                // wrong name, disallowed
                // search for alias, for better error reporting.
                Script sc = EcoreUtil2.getContainerOfType(context, Script.class);
                Optional<NamedImportSpecifier> namedImport = sc.getScriptElements().stream().filter(se -> se instanceof ImportDeclaration).map(se -> (ImportDeclaration) se).flatMap(idecl -> {
                    return idecl.getImportSpecifiers().stream().filter(is -> is instanceof NamedImportSpecifier).map(is -> (NamedImportSpecifier) is);
                }).filter(s -> s.getImportedElement() == memberType).findFirst();
                if (namedImport.isPresent()) {
                    // if alias is present assign, otherwise null will be passed through
                    memberTypeAlias = namedImport.get().getAlias();
                }
                return false;
            }
        }
    }
    return true;
}
Also used : IndexedAccessExpression(org.eclipse.n4js.n4JS.IndexedAccessExpression) FilterWithErrorMarkerScope(org.eclipse.n4js.xtext.scoping.FilterWithErrorMarkerScope) IScope(org.eclipse.xtext.scoping.IScope) TMember(org.eclipse.n4js.ts.types.TMember) EObject(org.eclipse.emf.ecore.EObject) Script(org.eclipse.n4js.n4JS.Script) IEObjectDescriptionWithError(org.eclipse.n4js.xtext.scoping.IEObjectDescriptionWithError) NamedImportSpecifier(org.eclipse.n4js.n4JS.NamedImportSpecifier) ContainerType(org.eclipse.n4js.ts.types.ContainerType) ExpressionExtensions(org.eclipse.n4js.n4JS.extensions.ExpressionExtensions) ImportDeclaration(org.eclipse.n4js.n4JS.ImportDeclaration) ParameterizedPropertyAccessExpression(org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression) EcoreUtil2(org.eclipse.xtext.EcoreUtil2) IdentifierRef(org.eclipse.n4js.n4JS.IdentifierRef) Optional(java.util.Optional) Expression(org.eclipse.n4js.n4JS.Expression) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) Script(org.eclipse.n4js.n4JS.Script) NamedImportSpecifier(org.eclipse.n4js.n4JS.NamedImportSpecifier) EObject(org.eclipse.emf.ecore.EObject) ImportDeclaration(org.eclipse.n4js.n4JS.ImportDeclaration) TMember(org.eclipse.n4js.ts.types.TMember) IdentifierRef(org.eclipse.n4js.n4JS.IdentifierRef)

Aggregations

ImportDeclaration (org.eclipse.n4js.n4JS.ImportDeclaration)6 EObject (org.eclipse.emf.ecore.EObject)2 IdentifierRef (org.eclipse.n4js.n4JS.IdentifierRef)2 IndexedAccessExpression (org.eclipse.n4js.n4JS.IndexedAccessExpression)2 NamedImportSpecifier (org.eclipse.n4js.n4JS.NamedImportSpecifier)2 ParameterizedPropertyAccessExpression (org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression)2 Script (org.eclipse.n4js.n4JS.Script)2 TModule (org.eclipse.n4js.ts.types.TModule)2 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 EPackage (org.eclipse.emf.ecore.EPackage)1 AdditiveExpression (org.eclipse.n4js.n4JS.AdditiveExpression)1 Annotation (org.eclipse.n4js.n4JS.Annotation)1 AnnotationList (org.eclipse.n4js.n4JS.AnnotationList)1 Argument (org.eclipse.n4js.n4JS.Argument)1 ArrayBindingPattern (org.eclipse.n4js.n4JS.ArrayBindingPattern)1 ArrayElement (org.eclipse.n4js.n4JS.ArrayElement)1 ArrayLiteral (org.eclipse.n4js.n4JS.ArrayLiteral)1 ArrayPadding (org.eclipse.n4js.n4JS.ArrayPadding)1 ArrowFunction (org.eclipse.n4js.n4JS.ArrowFunction)1