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 fa4d33d1a1 JS: Improve handling of export declarations (references and
classes)
new 9c6309109e Merge pull request #5796 from
matthiasblaesing/js_improvements5
fa4d33d1a1 is described below
commit fa4d33d1a198d99b21205c22ba1e02bc3b66e47a
Author: Matthias Bläsing <[email protected]>
AuthorDate: Fri Apr 7 22:37:54 2023 +0200
JS: Improve handling of export declarations (references and classes)
At least two issues are fixed by this:
- "export class XY" construct is not parsed correctly resulting in
a JS model for that class, that his missing methods
- "class XY {}; export {XY}" is not correctly parsed resulting missing
declaration handling for that member
Example that was not correctly parsed:
```
export class test {
method(param){
param;
}
};
// this shows in navigator / highlighting works
class test2{
method(param){
param;
}
}
// no highlighting, Go to declaration does not work
export {test2};
```
Closes: #5184
---
.../testfiles/markoccurences/issueGH5184_01.js | 23 +++++++++++++++++++++
...ssueGH5184_01.js.testIssueGH5184_01.occurrences | 2 ++
.../editor/navigation/GoToDeclarationTest.java | 5 ++++-
.../editor/navigation/MarkOccurrenceTest.java | 6 +++++-
.../modules/javascript2/model/ModelVisitor.java | 24 ++++++++++++++++++++--
.../data/testfiles/structure/issueGH5184_02.js | 23 +++++++++++++++++++++
.../testfiles/structure/issueGH5184_02.js.model | 11 ++++++++++
.../modules/javascript2/model/ModelTest.java | 4 ++++
.../src/com/oracle/js/parser/DumpingVisitor.java | 4 +++-
9 files changed, 97 insertions(+), 5 deletions(-)
diff --git
a/webcommon/javascript2.editor/test/unit/data/testfiles/markoccurences/issueGH5184_01.js
b/webcommon/javascript2.editor/test/unit/data/testfiles/markoccurences/issueGH5184_01.js
new file mode 100644
index 0000000000..aca8114b1f
--- /dev/null
+++
b/webcommon/javascript2.editor/test/unit/data/testfiles/markoccurences/issueGH5184_01.js
@@ -0,0 +1,23 @@
+/*
+ * 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 test2 {
+}
+
+export {test2};
diff --git
a/webcommon/javascript2.editor/test/unit/data/testfiles/markoccurences/issueGH5184_01.js.testIssueGH5184_01.occurrences
b/webcommon/javascript2.editor/test/unit/data/testfiles/markoccurences/issueGH5184_01.js.testIssueGH5184_01.occurrences
new file mode 100644
index 0000000000..25bdaf9d3b
--- /dev/null
+++
b/webcommon/javascript2.editor/test/unit/data/testfiles/markoccurences/issueGH5184_01.js.testIssueGH5184_01.occurrences
@@ -0,0 +1,2 @@
+class |>MARK_OCCURRENCES:test2<| {
+export {|>MARK_OCCURRENCES:te^st2<|};
diff --git
a/webcommon/javascript2.editor/test/unit/src/org/netbeans/modules/javascript2/editor/navigation/GoToDeclarationTest.java
b/webcommon/javascript2.editor/test/unit/src/org/netbeans/modules/javascript2/editor/navigation/GoToDeclarationTest.java
index c196fceeb1..60557d7629 100644
---
a/webcommon/javascript2.editor/test/unit/src/org/netbeans/modules/javascript2/editor/navigation/GoToDeclarationTest.java
+++
b/webcommon/javascript2.editor/test/unit/src/org/netbeans/modules/javascript2/editor/navigation/GoToDeclarationTest.java
@@ -137,5 +137,8 @@ public class GoToDeclarationTest extends JsTestBase {
public void testImportedFile_03() throws Exception {
checkDeclaration("testfiles/ecmascript6/importExport/importFindDeclaration01.js",
"import { text as text3 } from \"l^ib/export02\";", "export02.js", 0);
}
-
+
+ public void testIssueGH5184_01() throws Exception {
+ checkDeclaration("testfiles/markoccurences/issueGH5184_01.js", "export
{te^st2};", "class ^test2 {");
+ }
}
diff --git
a/webcommon/javascript2.editor/test/unit/src/org/netbeans/modules/javascript2/editor/navigation/MarkOccurrenceTest.java
b/webcommon/javascript2.editor/test/unit/src/org/netbeans/modules/javascript2/editor/navigation/MarkOccurrenceTest.java
index 8ed951f1d8..b6ddaabf8f 100644
---
a/webcommon/javascript2.editor/test/unit/src/org/netbeans/modules/javascript2/editor/navigation/MarkOccurrenceTest.java
+++
b/webcommon/javascript2.editor/test/unit/src/org/netbeans/modules/javascript2/editor/navigation/MarkOccurrenceTest.java
@@ -2243,7 +2243,11 @@ public class MarkOccurrenceTest extends JsTestBase {
public void testIssue258724_02() throws Exception {
checkOccurrences("testfiles/markoccurences/issue258724.js", "return
this.ag^e;", true);
}
-
+
+ public void testIssueGH5184_01() throws Exception {
+ checkOccurrences("testfiles/markoccurences/issueGH5184_01.js", "export
{te^st2};", true);
+ }
+
private String getTestName() {
String name = getName();
int indexOf = name.indexOf("_");
diff --git
a/webcommon/javascript2.model/src/org/netbeans/modules/javascript2/model/ModelVisitor.java
b/webcommon/javascript2.model/src/org/netbeans/modules/javascript2/model/ModelVisitor.java
index 816ce0a534..7ca7de8280 100644
---
a/webcommon/javascript2.model/src/org/netbeans/modules/javascript2/model/ModelVisitor.java
+++
b/webcommon/javascript2.model/src/org/netbeans/modules/javascript2/model/ModelVisitor.java
@@ -65,6 +65,7 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Set;
import java.util.Stack;
import java.util.logging.Level;
@@ -696,7 +697,25 @@ public class ModelVisitor extends PathNodeVisitor
implements ModelResolver {
}
if (className != null) {
- classObject = new JsObjectImpl(parent, className, new
OffsetRange(node.getStart(), node.getFinish()), true, parent.getMimeType(),
parent.getSourceLabel());
+ // At least for exported classes multiple JsObjectImpls are created
+ // and that latest created one is missing the properties. To fix
+ // this, an existing object is checked and if it exists its
+ // properties are moved to the latest instance, creating a superset
+ // of all properties.
+ classObject = new JsObjectImpl(
+ parent,
+ className,
+ new OffsetRange(node.getStart(), node.getFinish()),
+ true,
+ parent.getMimeType(),
+ parent.getSourceLabel()
+ );
+ JsObject origClassObject = parent.getProperty(className.getName());
+ if (origClassObject != null) {
+ for (Entry<String, ? extends JsObject> e :
origClassObject.getProperties().entrySet()) {
+ ModelUtils.moveProperty(classObject, e.getValue());
+ }
+ }
parent.addProperty(className.getName(), classObject);
classObject.setJsKind(JsElement.Kind.CLASS);
if (refName != null) {
@@ -837,6 +856,7 @@ public class ModelVisitor extends PathNodeVisitor
implements ModelResolver {
@Override
public boolean enterExportNode(ExportNode exportNode) {
+ boolean result = super.enterExportNode(exportNode);
final ExportClauseNode exportClause = exportNode.getExportClause();
final FromNode from = exportNode.getFrom();
final Expression expression = exportNode.getExpression();
@@ -866,7 +886,7 @@ public class ModelVisitor extends PathNodeVisitor
implements ModelResolver {
expression.accept(this);
removeFromPathTheLast();
}
- return false;
+ return result;
}
@Override
diff --git
a/webcommon/javascript2.model/test/unit/data/testfiles/structure/issueGH5184_02.js
b/webcommon/javascript2.model/test/unit/data/testfiles/structure/issueGH5184_02.js
new file mode 100644
index 0000000000..891efb6bf8
--- /dev/null
+++
b/webcommon/javascript2.model/test/unit/data/testfiles/structure/issueGH5184_02.js
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+
+export class test {
+ method1() {
+ }
+}
\ No newline at end of file
diff --git
a/webcommon/javascript2.model/test/unit/data/testfiles/structure/issueGH5184_02.js.model
b/webcommon/javascript2.model/test/unit/data/testfiles/structure/issueGH5184_02.js.model
new file mode 100644
index 0000000000..92cccbba45
--- /dev/null
+++
b/webcommon/javascript2.model/test/unit/data/testfiles/structure/issueGH5184_02.js.model
@@ -0,0 +1,11 @@
+FUNCTION issueGH5184_02 [ANONYMOUS: false, DECLARED: true - issueGH5184_02,
MODIFIERS: PUBLIC, FILE]
+# RETURN TYPES
+undefined, RESOLVED: true
+# PROPERTIES
+test : OBJECT test [ANONYMOUS: false, DECLARED: true - test, MODIFIERS:
PUBLIC, CLASS]
+ # PROPERTIES
+ method1 : FUNCTION method1 [ANONYMOUS: false, DECLARED: true - method1,
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 39fa7d24d3..efbffbb198 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
@@ -230,6 +230,10 @@ public class ModelTest extends ModelTestBase {
checkModel("testfiles/model/issue251911.js");
}
+ public void testIssueGH5184_02() throws Exception {
+ checkModel("testfiles/structure/issueGH5184_02.js");
+ }
+
public void testPersonRevert() throws Exception {
FileObject fo = getTestFile("testfiles/model/person.js.model");
try (BufferedReader reader = new BufferedReader(new
InputStreamReader(fo.getInputStream()))) {
diff --git
a/webcommon/libs.nashorn/test/unit/src/com/oracle/js/parser/DumpingVisitor.java
b/webcommon/libs.nashorn/test/unit/src/com/oracle/js/parser/DumpingVisitor.java
index 36b82a12a8..76c55dda6d 100644
---
a/webcommon/libs.nashorn/test/unit/src/com/oracle/js/parser/DumpingVisitor.java
+++
b/webcommon/libs.nashorn/test/unit/src/com/oracle/js/parser/DumpingVisitor.java
@@ -59,9 +59,11 @@ class DumpingVisitor extends NodeVisitor {
System.out.println(indent() + node.getClass().getName() + " [" +
((LiteralNode) node).getValue() + "]");
} else if (node instanceof FunctionNode) {
FunctionNode fn = (FunctionNode) node;
- System.out.printf("%s%s [%s, kind=%s, isAsync=%b, isMethod=%b]%n",
+ System.out.printf("%s%s [%s-%d, %s, kind=%s, isAsync=%b,
isMethod=%b]%n",
indent(),
node.getClass().getName(),
+ fn.getStart(),
+ fn.getFinish(),
fn.getName(),
fn.getKind().name(),
fn.isAsync(),
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists