Search in sources :

Example 6 with NamedImportSpecifier

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

NamedImportSpecifier (org.eclipse.n4js.n4JS.NamedImportSpecifier)6 EObject (org.eclipse.emf.ecore.EObject)3 IdentifierRef (org.eclipse.n4js.n4JS.IdentifierRef)3 ParameterizedPropertyAccessExpression (org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression)3 URI (org.eclipse.emf.common.util.URI)2 ImportDeclaration (org.eclipse.n4js.n4JS.ImportDeclaration)2 IndexedAccessExpression (org.eclipse.n4js.n4JS.IndexedAccessExpression)2 JSXPropertyAttribute (org.eclipse.n4js.n4JS.JSXPropertyAttribute)2 Script (org.eclipse.n4js.n4JS.Script)2 Optional (java.util.Optional)1 EClass (org.eclipse.emf.ecore.EClass)1 EPackage (org.eclipse.emf.ecore.EPackage)1 EReference (org.eclipse.emf.ecore.EReference)1 InternalEObject (org.eclipse.emf.ecore.InternalEObject)1 BadEscapementException (org.eclipse.n4js.conversion.AbstractN4JSStringValueConverter.BadEscapementException)1 N4JSValueConverterException (org.eclipse.n4js.conversion.N4JSValueConverterException)1 N4JSValueConverterWithValueException (org.eclipse.n4js.conversion.N4JSValueConverterWithValueException)1 AdditiveExpression (org.eclipse.n4js.n4JS.AdditiveExpression)1 Annotation (org.eclipse.n4js.n4JS.Annotation)1 AnnotationList (org.eclipse.n4js.n4JS.AnnotationList)1