This is an automated email from the ASF dual-hosted git repository. matthiasblaesing pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/master by this push: new 2bce924a59 JS: Don't handle this as global value new cc5516f28d Merge pull request #5800 from matthiasblaesing/js_improvements6 2bce924a59 is described below commit 2bce924a59eb4bf79cd7a54fdcd613e9876b0f05 Author: Matthias Bläsing <mblaes...@doppel-helix.eu> AuthorDate: Sat Apr 8 13:13:16 2023 +0200 JS: Don't handle this as global value JS allows method calls for all functions, which in turn makes this available in nearly all contexts. It is thus not sensible to report this as an undeclared global variable. Closes: #4568 Closes: #4213 --- .../editor/hints/GlobalIsNotDefined.java | 2 +- .../test/unit/data/testfiles/hints/issueGH4213.js | 28 ++++++++++++++++++++++ .../hints/issueGH4213.js.testIssueGH4213.hints | 0 .../test/unit/data/testfiles/hints/issueGH4568.js | 24 +++++++++++++++++++ .../hints/issueGH4568.js.testIssueGH4568.hints | 0 .../editor/hint/JsGlobalIsNotDeclaredTest.java | 8 +++++++ .../modules/javascript2/model/api/ModelUtils.java | 10 +++++++- .../data/testfiles/structure/bogusGlobalThis_01.js | 25 +++++++++++++++++++ .../structure/bogusGlobalThis_01.js.model | 14 +++++++++++ .../data/testfiles/structure/bogusGlobalThis_02.js | 25 +++++++++++++++++++ .../structure/bogusGlobalThis_02.js.model | 14 +++++++++++ .../modules/javascript2/model/ModelTest.java | 5 ++++ 12 files changed, 153 insertions(+), 2 deletions(-) diff --git a/webcommon/javascript2.editor/src/org/netbeans/modules/javascript2/editor/hints/GlobalIsNotDefined.java b/webcommon/javascript2.editor/src/org/netbeans/modules/javascript2/editor/hints/GlobalIsNotDefined.java index fef301474c..96a9e80d15 100644 --- a/webcommon/javascript2.editor/src/org/netbeans/modules/javascript2/editor/hints/GlobalIsNotDefined.java +++ b/webcommon/javascript2.editor/src/org/netbeans/modules/javascript2/editor/hints/GlobalIsNotDefined.java @@ -60,7 +60,7 @@ import org.openide.util.NbBundle; public class GlobalIsNotDefined extends JsAstRule { private static final List<String> KNOWN_GLOBAL_OBJECTS = Arrays.asList( - "super", "$", "jQuery", //NOI18N + "super", "$", "jQuery", "this", //NOI18N Type.ARRAY, Type.OBJECT, Type.BOOLEAN, Type.NULL, Type.NUMBER, Type.REGEXP, Type.STRING, Type.UNDEFINED, Type.UNRESOLVED); diff --git a/webcommon/javascript2.editor/test/unit/data/testfiles/hints/issueGH4213.js b/webcommon/javascript2.editor/test/unit/data/testfiles/hints/issueGH4213.js new file mode 100644 index 0000000000..7fc5a1f1eb --- /dev/null +++ b/webcommon/javascript2.editor/test/unit/data/testfiles/hints/issueGH4213.js @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +class Test { + state = { + test: null + }; + + setCategory(url) { + this.state.test = {url}; + } +} diff --git a/webcommon/javascript2.editor/test/unit/data/testfiles/hints/issueGH4213.js.testIssueGH4213.hints b/webcommon/javascript2.editor/test/unit/data/testfiles/hints/issueGH4213.js.testIssueGH4213.hints new file mode 100644 index 0000000000..e69de29bb2 diff --git a/webcommon/javascript2.editor/test/unit/data/testfiles/hints/issueGH4568.js b/webcommon/javascript2.editor/test/unit/data/testfiles/hints/issueGH4568.js new file mode 100644 index 0000000000..08db25dad6 --- /dev/null +++ b/webcommon/javascript2.editor/test/unit/data/testfiles/hints/issueGH4568.js @@ -0,0 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +var p = { + c: function() { + this.a.b = {}; + } +}; diff --git a/webcommon/javascript2.editor/test/unit/data/testfiles/hints/issueGH4568.js.testIssueGH4568.hints b/webcommon/javascript2.editor/test/unit/data/testfiles/hints/issueGH4568.js.testIssueGH4568.hints new file mode 100644 index 0000000000..e69de29bb2 diff --git a/webcommon/javascript2.editor/test/unit/src/org/netbeans/modules/javascript2/editor/hint/JsGlobalIsNotDeclaredTest.java b/webcommon/javascript2.editor/test/unit/src/org/netbeans/modules/javascript2/editor/hint/JsGlobalIsNotDeclaredTest.java index 3524dca066..141a8cc73e 100644 --- a/webcommon/javascript2.editor/test/unit/src/org/netbeans/modules/javascript2/editor/hint/JsGlobalIsNotDeclaredTest.java +++ b/webcommon/javascript2.editor/test/unit/src/org/netbeans/modules/javascript2/editor/hint/JsGlobalIsNotDeclaredTest.java @@ -119,6 +119,14 @@ public class JsGlobalIsNotDeclaredTest extends HintTestBase { checkHints(this, createRule(), "testfiles/hints/issueGH4246.js", null); } + public void testIssueGH4213() throws Exception { + checkHints(this, createRule(), "testfiles/hints/issueGH4213.js", null); + } + + public void testIssueGH4568() throws Exception { + checkHints(this, createRule(), "testfiles/hints/issueGH4568.js", null); + } + @Override protected boolean cleanCacheDir() { // The cache dir also holds the index cache - if the cache is cleared, diff --git a/webcommon/javascript2.model/src/org/netbeans/modules/javascript2/model/api/ModelUtils.java b/webcommon/javascript2.model/src/org/netbeans/modules/javascript2/model/api/ModelUtils.java index 8f810b6a77..37fa86aac3 100644 --- a/webcommon/javascript2.model/src/org/netbeans/modules/javascript2/model/api/ModelUtils.java +++ b/webcommon/javascript2.model/src/org/netbeans/modules/javascript2/model/api/ModelUtils.java @@ -88,13 +88,21 @@ public class ModelUtils { private static final Logger LOG = Logger.getLogger(ModelUtils.class.getName()); + @SuppressWarnings("AssignmentToMethodParameter") public static JsObjectImpl getJsObject (ModelBuilder builder, List<Identifier> fqName, boolean isLHS) { if (fqName == null || fqName.isEmpty()) { return null; } JsObject result = builder.getCurrentObject(); - JsObject tmpObject = null; String firstName = fqName.get(0).getName(); + JsObject tmpObject; + + if (THIS.equals(firstName)) { + tmpObject = resolveThis(result); + fqName = fqName.subList(1, fqName.size()); + } else { + tmpObject = null; + } while (tmpObject == null && result != null && result.getParent() != null) { if (result instanceof JsFunctionImpl) { diff --git a/webcommon/javascript2.model/test/unit/data/testfiles/structure/bogusGlobalThis_01.js b/webcommon/javascript2.model/test/unit/data/testfiles/structure/bogusGlobalThis_01.js new file mode 100644 index 0000000000..68ca5f3ca4 --- /dev/null +++ b/webcommon/javascript2.model/test/unit/data/testfiles/structure/bogusGlobalThis_01.js @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +var p = { + x: function() { + this.b.a = {}; // This construct should not lead to the creation of a + // global variable this + } +}; \ No newline at end of file diff --git a/webcommon/javascript2.model/test/unit/data/testfiles/structure/bogusGlobalThis_01.js.model b/webcommon/javascript2.model/test/unit/data/testfiles/structure/bogusGlobalThis_01.js.model new file mode 100644 index 0000000000..88fb0b4caf --- /dev/null +++ b/webcommon/javascript2.model/test/unit/data/testfiles/structure/bogusGlobalThis_01.js.model @@ -0,0 +1,14 @@ +FUNCTION bogusGlobalThis_01 [ANONYMOUS: false, DECLARED: true - bogusGlobalThis_01, MODIFIERS: PUBLIC, FILE] +# RETURN TYPES +undefined, RESOLVED: true +# PROPERTIES +p : OBJECT p [ANONYMOUS: false, DECLARED: true - p, MODIFIERS: PUBLIC, OBJECT_LITERAL] + # PROPERTIES + b : OBJECT b [ANONYMOUS: false, DECLARED: false - b, MODIFIERS: PUBLIC, OBJECT] + # PROPERTIES + a : OBJECT a [ANONYMOUS: false, DECLARED: true - a, MODIFIERS: PUBLIC, OBJECT_LITERAL] + x : FUNCTION x [ANONYMOUS: false, DECLARED: true - x, MODIFIERS: PUBLIC, METHOD] + # RETURN TYPES + undefined, RESOLVED: true + # PROPERTIES + arguments : OBJECT arguments [ANONYMOUS: false, DECLARED: false - arguments, MODIFIERS: PRIVATE, VARIABLE] diff --git a/webcommon/javascript2.model/test/unit/data/testfiles/structure/bogusGlobalThis_02.js b/webcommon/javascript2.model/test/unit/data/testfiles/structure/bogusGlobalThis_02.js new file mode 100644 index 0000000000..eab774440c --- /dev/null +++ b/webcommon/javascript2.model/test/unit/data/testfiles/structure/bogusGlobalThis_02.js @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +class q { + x() { + this.a.b = {}; // This construct should not lead to the creation of a + // global variable this + } +} \ No newline at end of file diff --git a/webcommon/javascript2.model/test/unit/data/testfiles/structure/bogusGlobalThis_02.js.model b/webcommon/javascript2.model/test/unit/data/testfiles/structure/bogusGlobalThis_02.js.model new file mode 100644 index 0000000000..7e3d3ca97f --- /dev/null +++ b/webcommon/javascript2.model/test/unit/data/testfiles/structure/bogusGlobalThis_02.js.model @@ -0,0 +1,14 @@ +FUNCTION bogusGlobalThis_02 [ANONYMOUS: false, DECLARED: true - bogusGlobalThis_02, MODIFIERS: PUBLIC, FILE] +# RETURN TYPES +undefined, RESOLVED: true +# PROPERTIES +q : OBJECT q [ANONYMOUS: false, DECLARED: true - q, MODIFIERS: PUBLIC, CLASS] + # PROPERTIES + a : OBJECT a [ANONYMOUS: false, DECLARED: false - a, MODIFIERS: PUBLIC, OBJECT] + # PROPERTIES + b : OBJECT b [ANONYMOUS: false, DECLARED: true - b, MODIFIERS: PUBLIC, OBJECT_LITERAL] + x : FUNCTION x [ANONYMOUS: false, DECLARED: true - x, MODIFIERS: PUBLIC, METHOD] + # RETURN TYPES + undefined, RESOLVED: true + # PROPERTIES + arguments : OBJECT arguments [ANONYMOUS: false, DECLARED: false - arguments, MODIFIERS: PRIVATE, VARIABLE] diff --git a/webcommon/javascript2.model/test/unit/src/org/netbeans/modules/javascript2/model/ModelTest.java b/webcommon/javascript2.model/test/unit/src/org/netbeans/modules/javascript2/model/ModelTest.java index efbffbb198..494b8adcab 100644 --- a/webcommon/javascript2.model/test/unit/src/org/netbeans/modules/javascript2/model/ModelTest.java +++ b/webcommon/javascript2.model/test/unit/src/org/netbeans/modules/javascript2/model/ModelTest.java @@ -234,6 +234,11 @@ public class ModelTest extends ModelTestBase { checkModel("testfiles/structure/issueGH5184_02.js"); } + public void testBogusGlobalThis() throws Exception { + checkModel("testfiles/structure/bogusGlobalThis_01.js"); + checkModel("testfiles/structure/bogusGlobalThis_02.js"); + } + public void testPersonRevert() throws Exception { FileObject fo = getTestFile("testfiles/model/person.js.model"); try (BufferedReader reader = new BufferedReader(new InputStreamReader(fo.getInputStream()))) { --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists