Search in sources :

Example 1 with TypableElement

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

the class TypeXpectMethod method getTypeString.

private String getTypeString(IEObjectCoveringRegion offset, boolean expectedType) {
    final String calculatedString;
    EObject eobject = offset.getEObject();
    if (eobject instanceof LiteralOrComputedPropertyName) {
        eobject = eobject.eContainer();
    }
    RuleEnvironment G = newRuleEnvironment(eobject);
    Result<org.eclipse.n4js.ts.typeRefs.TypeRef> result;
    if (expectedType) {
        if (!(eobject instanceof Expression && eobject.eContainer() != null))
            return "Not an Expression at given region (required to obtain expected type); got instead: " + eobject.eClass().getName();
        result = ts.expectedTypeIn(G, eobject.eContainer(), (Expression) eobject);
    } else {
        if (eobject instanceof BindingProperty) {
            /*-
				 * Small tweak to allow testing the inferred type of variable declarations within binding patterns. For
				 * example, without this tweak, the following test would fail with a "Not a TypableElement at given
				 * region" exception:
				 *
				 * // Xpect type of 'len' --> number
				 * var {length:len} = "hello";
				 */
            if (((BindingProperty) eobject).getValue() != null && ((BindingProperty) eobject).getValue().getVarDecl() != null) {
                eobject = ((BindingProperty) eobject).getValue().getVarDecl();
            }
        }
        if (!(eobject instanceof TypableElement))
            return "Not a TypableElement at given region; got instead: " + eobject.eClass().getName();
        result = ts.type(G, (TypableElement) eobject);
    }
    if (result.getRuleFailedException() != null) {
        calculatedString = result.getRuleFailedException().getMessage();
    } else {
        calculatedString = result.getValue().getTypeRefAsString();
    }
    return calculatedString;
}
Also used : Expression(org.eclipse.n4js.n4JS.Expression) ParameterizedCallExpression(org.eclipse.n4js.n4JS.ParameterizedCallExpression) TypableElement(org.eclipse.n4js.ts.types.TypableElement) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) EObject(org.eclipse.emf.ecore.EObject) LiteralOrComputedPropertyName(org.eclipse.n4js.n4JS.LiteralOrComputedPropertyName) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) RuleEnvironmentExtensions.newRuleEnvironment(org.eclipse.n4js.typesystem.RuleEnvironmentExtensions.newRuleEnvironment) BindingProperty(org.eclipse.n4js.n4JS.BindingProperty)

Example 2 with TypableElement

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

the class N4JSNameValidator method holdsNoTypeNameOrNameEqualsType.

private boolean holdsNoTypeNameOrNameEqualsType(NamedElement namedElement) {
    if (namedElement instanceof TypableElement) {
        String name = namedElement.getName();
        if (BASE_TYPES.contains(name)) {
            TypeRef typeRef = ts.tau((TypableElement) namedElement);
            if (typeRef != null && typeRef.getDeclaredType() != null) {
                String typeName = typeRef.getDeclaredType().getName();
                if (!Strings.isEmpty(typeName) && !name.equals(typeName)) {
                    final String message = getMessageForCLF_NAME_DIFFERS_TYPE(validatorMessageHelper.description(namedElement), name, typeName);
                    addIssue(Strings.toFirstUpper(message), namedElement, N4JSFeatureUtils.attributeOfNameFeature(namedElement), CLF_NAME_DIFFERS_TYPE);
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : TypableElement(org.eclipse.n4js.ts.types.TypableElement) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef)

Aggregations

TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)2 TypableElement (org.eclipse.n4js.ts.types.TypableElement)2 EObject (org.eclipse.emf.ecore.EObject)1 BindingProperty (org.eclipse.n4js.n4JS.BindingProperty)1 Expression (org.eclipse.n4js.n4JS.Expression)1 LiteralOrComputedPropertyName (org.eclipse.n4js.n4JS.LiteralOrComputedPropertyName)1 ParameterizedCallExpression (org.eclipse.n4js.n4JS.ParameterizedCallExpression)1 RuleEnvironmentExtensions.newRuleEnvironment (org.eclipse.n4js.typesystem.RuleEnvironmentExtensions.newRuleEnvironment)1 RuleEnvironment (org.eclipse.xsemantics.runtime.RuleEnvironment)1