fix and a test for using the "this" in a function belonging to an object instance. Same code as in assign() but we need it earlier.
diff --git a/plugins/org.eclipse.dltk.javascript.core/src/org/eclipse/dltk/internal/javascript/ti/TypeInferencerVisitor.java b/plugins/org.eclipse.dltk.javascript.core/src/org/eclipse/dltk/internal/javascript/ti/TypeInferencerVisitor.java index 692bd35..2f0482b 100644 --- a/plugins/org.eclipse.dltk.javascript.core/src/org/eclipse/dltk/internal/javascript/ti/TypeInferencerVisitor.java +++ b/plugins/org.eclipse.dltk.javascript.core/src/org/eclipse/dltk/internal/javascript/ti/TypeInferencerVisitor.java
@@ -874,6 +874,23 @@ } } + } else { + // if this is a "this.property" assignment then take over the this + // of the parent. + if (node.getParent() instanceof BinaryOperation) { + BinaryOperation bo = (BinaryOperation) node.getParent(); + if (bo.getLeftExpression() instanceof PropertyExpression + && ((PropertyExpression) bo.getLeftExpression()) + .getObject() instanceof ThisExpression) { + IValueCollection context = peekContext(); + if (context instanceof IFunctionValueCollection) { + String name = ((IFunctionValueCollection) context) + .getFunctionName(); + thisValue.setDeclaredType(RTypes.localType(name, + context.getParent().getChild(name))); + } + } + } } final IValueCollection function = new FunctionValueCollection( peekContext(), method.getName(), thisValue,
diff --git a/tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/validation/TypeInfoValidationTests.java b/tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/validation/TypeInfoValidationTests.java index fad2f88..238082e 100644 --- a/tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/validation/TypeInfoValidationTests.java +++ b/tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/validation/TypeInfoValidationTests.java
@@ -3364,6 +3364,25 @@ assertEquals(problems.toString(), 1, problems.size()); } + + public void testThisCallInObjectFunction() { + final StringList code = new StringList(); + code.add("/**"); + code.add(" * @constructor"); + code.add(" */"); + code.add("function Myconstructor(){"); + code.add(" this.getArray = function(){"); + code.add(" return []"); + code.add(" },"); + code.add(" this.test = function() {"); + code.add(" var shouldBeArray = this.getArray()"); + code.add(" for (var i = 0, len = shouldBeArray.length; i < len; i++) {}"); + code.add(" }"); + code.add("}"); + final List<IProblem> problems = validate(code.toString()); + assertEquals(problems.toString(), 0, problems.size()); + } + public void testProtectedProperty() { if (notYetImplemented(this)) { return;