From 2b9bdaa2dc8ef9d5c22f5035bd34b467854db0f3 Mon Sep 17 00:00:00 2001 From: mmews-n4 Date: Mon, 18 Oct 2021 15:15:32 +0200 Subject: [PATCH] GH-2230: Incorrect scoping in modules containing (non-static) polyfills (#2231) * fix bug * add tests * adjust test expectation --- .../LocallyKnownTypesScopingHelper.xtend | 2 +- ...inheriting_from_filled_type__IdeTest.xtend | 1 + .../ref_to_filled_type_scoping.n4js.xt | 41 ++++++++++++++++ .../ref_to_filled_type_scoping.package.json | 15 ++++++ ...ef_to_static_filled_type_scoping.n4js.src2 | 21 ++++++++ .../ref_to_static_filled_type_scoping.n4js.xt | 49 +++++++++++++++++++ ...to_static_filled_type_scoping.package.json | 16 ++++++ 7 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill/ref_to_filled_type_scoping.n4js.xt create mode 100644 tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill/ref_to_filled_type_scoping.package.json create mode 100644 tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill_static/ref_to_static_filled_type_scoping.n4js.src2 create mode 100644 tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill_static/ref_to_static_filled_type_scoping.n4js.xt create mode 100644 tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill_static/ref_to_static_filled_type_scoping.package.json diff --git a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/scoping/utils/LocallyKnownTypesScopingHelper.xtend b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/scoping/utils/LocallyKnownTypesScopingHelper.xtend index 621059c505..bd6aab62e8 100644 --- a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/scoping/utils/LocallyKnownTypesScopingHelper.xtend +++ b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/scoping/utils/LocallyKnownTypesScopingHelper.xtend @@ -117,7 +117,7 @@ class LocallyKnownTypesScopingHelper { if (local === null || local.eIsProxy) { return parent; } - val eoDescrs = local.topLevelTypes.map[ topLevelType | EObjectDescription.create(topLevelType.name, topLevelType) ]; + val eoDescrs = local.topLevelTypes.filter[t | !t.polyfill ].map[ topLevelType | EObjectDescription.create(topLevelType.name, topLevelType) ]; return scopeSnapshotHelper.scopeFor("scopeWithLocallyDeclaredTypes", script, parent, eoDescrs); } diff --git a/tests/org.eclipse.n4js.ui.tests/src/org/eclipse/n4js/tests/staticpolyfill/StaticPolyfill_inheriting_from_filled_type__IdeTest.xtend b/tests/org.eclipse.n4js.ui.tests/src/org/eclipse/n4js/tests/staticpolyfill/StaticPolyfill_inheriting_from_filled_type__IdeTest.xtend index adfbd8f8cc..69b683a43a 100644 --- a/tests/org.eclipse.n4js.ui.tests/src/org/eclipse/n4js/tests/staticpolyfill/StaticPolyfill_inheriting_from_filled_type__IdeTest.xtend +++ b/tests/org.eclipse.n4js.ui.tests/src/org/eclipse/n4js/tests/staticpolyfill/StaticPolyfill_inheriting_from_filled_type__IdeTest.xtend @@ -62,6 +62,7 @@ class StaticPolyfill_inheriting_from_filled_type__IdeTest extends AbstractStatic "(Error, [17:14 - 17:18], Couldn't resolve reference to IdentifiableElement 'test'.)", "(Error, [21:11 - 21:15], Couldn't resolve reference to IdentifiableElement 'test'.)", "(Error, [27:34 - 27:39], Couldn't resolve reference to Type 'Buddy'.)", + "(Error, [29:20 - 29:24], Couldn't resolve reference to Type 'Poly'.)", "(Error, [36:8 - 36:12], Couldn't resolve reference to IdentifiableElement 'test'.)" ], "src2/A2.n4js" -> #[ diff --git a/tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill/ref_to_filled_type_scoping.n4js.xt b/tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill/ref_to_filled_type_scoping.n4js.xt new file mode 100644 index 0000000000..791a95c3cd --- /dev/null +++ b/tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill/ref_to_filled_type_scoping.n4js.xt @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2021 NumberFour AG. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * NumberFour AG - Initial API and implementation + */ + +/* XPECT_SETUP org.eclipse.n4js.xpect.tests.N4jsXtTest + + Workspace { + Project "ref-to-filled-type-scoping" { + Folder "src" { + ThisFile { } + } + File "package.json" { from="ref_to_filled_type_scoping.package.json" } + } + } + + END_SETUP + */ + + +@@Global @@ProvidedByRuntime + +@Polyfill +export external public class Array extends Array { + // XPECT linkedFragment at 'Array' --> n4scheme:/builtin_js.n4jsd#/1/@topLevelTypes.2 + member: Array; // correctly scoped to Array in builtin_js.n4jsd +} + +// XPECT linkedFragment at 'Array' --> n4scheme:/builtin_js.n4jsd#/1/@topLevelTypes.2 +const K : Array = null; + +class SomeClass { + // XPECT linkedFragment at 'Array' --> n4scheme:/builtin_js.n4jsd#/1/@topLevelTypes.2 + someMethod(): Array; // make sure: not incorrectly scoped to Array in this module +} diff --git a/tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill/ref_to_filled_type_scoping.package.json b/tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill/ref_to_filled_type_scoping.package.json new file mode 100644 index 0000000000..2978e22813 --- /dev/null +++ b/tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill/ref_to_filled_type_scoping.package.json @@ -0,0 +1,15 @@ +{ + "name": "ref-to-filled-type-scoping", + "version": "0.0.1", + "n4js": { + "projectType": "runtimelibrary", + "vendorId": "org.eclipse.n4js", + "vendorName": "Eclipse N4JS Project", + "output": "src-gen", + "sources": { + "source": [ + "src" + ] + } + } +} \ No newline at end of file diff --git a/tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill_static/ref_to_static_filled_type_scoping.n4js.src2 b/tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill_static/ref_to_static_filled_type_scoping.n4js.src2 new file mode 100644 index 0000000000..e044f6d15c --- /dev/null +++ b/tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill_static/ref_to_static_filled_type_scoping.n4js.src2 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2021 NumberFour AG. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * NumberFour AG - Initial API and implementation + */ + +@@StaticPolyfillAware + +export public class A { + public methodA() : void {} +} + + +export public class B { + public methodB() : void {} +} diff --git a/tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill_static/ref_to_static_filled_type_scoping.n4js.xt b/tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill_static/ref_to_static_filled_type_scoping.n4js.xt new file mode 100644 index 0000000000..095c2c5343 --- /dev/null +++ b/tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill_static/ref_to_static_filled_type_scoping.n4js.xt @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2021 NumberFour AG. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * NumberFour AG - Initial API and implementation + */ + +/* XPECT_SETUP org.eclipse.n4js.xpect.tests.N4jsXtTest + + Workspace { + Project "ref-to-static-filled-type-scoping" { + Folder "src1" { + ThisFile { } + } + Folder "src2" { + File "ref_to_static_filled_type_scoping.n4js" { from="ref_to_static_filled_type_scoping.n4js.src2" } + } + File "package.json" { from="ref_to_static_filled_type_scoping.package.json" } + } + } + + END_SETUP + */ + + + + +@@StaticPolyfillModule + +@StaticPolyfill +export public class A extends A { + + // XPECT linkedFragment at 'A' --> ../src2/ref_to_static_filled_type_scoping.n4js#/1/@topLevelTypes.0 + public member : A = null; +} + + +@StaticPolyfill +export public class B extends B { + + // XPECT linkedFragment at 'A' --> ../src2/ref_to_static_filled_type_scoping.n4js#/1/@topLevelTypes.0 + public member : A = null; +} + + \ No newline at end of file diff --git a/tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill_static/ref_to_static_filled_type_scoping.package.json b/tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill_static/ref_to_static_filled_type_scoping.package.json new file mode 100644 index 0000000000..b90ff9d46a --- /dev/null +++ b/tests/org.eclipse.n4js.xpect.tests/xt-tests/polyfill_static/ref_to_static_filled_type_scoping.package.json @@ -0,0 +1,16 @@ +{ + "name": "ref-to-static-filled-type-scoping", + "version": "0.0.1", + "n4js": { + "projectType": "library", + "vendorId": "org.eclipse.n4js", + "vendorName": "Eclipse N4JS Project", + "output": "src-gen", + "sources": { + "source": [ + "src1", + "src2" + ] + } + } +} \ No newline at end of file