Skip to content

Commit

Permalink
JS: Provide model data for meta properties new.target and import.meta
Browse files Browse the repository at this point in the history
Both new.target and import.meta are declared as special properties with
special semantic as they are properties on keywords. The approach taken
here is to declare a helper object "metaproperties", declare the "new"
and "import" properties and their children there. After parsing the
properties are relocated from metaproperties into the global object,
where they will be picked up by the JsCompletion.
  • Loading branch information
matthiasblaesing committed Mar 31, 2024
1 parent 86be3c4 commit bb2d43f
Show file tree
Hide file tree
Showing 39 changed files with 439 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ CONSTRUCTO Bulici [PUBLIC] index.html
CONSTRUCTO Clobrda [PUBLIC] index.html
CLASS $context [PRIVATE] <font color=#999999>index.html</font>
CLASS $parentContext [PRIVATE] <font color=#999999>index.html</font>
CLASS import [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS ko [PUBLIC] <font color=#999999>Knockout</font>
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
METHOD init(): undefined [PUBLIC] index.html
FIELD age: Number [PUBLIC] index.html
FIELD jmeno [PUBLIC] index.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Code completion result for source line:
------------------------------------
CLASS $context [PRIVATE] <font color=#999999>index.html</font>
CLASS $parentContext [PRIVATE] <font color=#999999>index.html</font>
CLASS import [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS ko [PUBLIC] <font color=#999999>Knockout</font>
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS viewModel [PUBLIC] index.html
VARIABLE $data [PRIVATE] <font color=#999999>index.html</font>
VARIABLE $element [PRIVATE] <font color=#999999>index.html</font>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Code completion result for source line:
CONSTRUCTO TestModel [PUBLIC] index.html
CLASS $context [PRIVATE] <font color=#999999>index.html</font>
CLASS $parentContext [PRIVATE] <font color=#999999>index.html</font>
CLASS import [PUBLIC] metaproperties.js
CLASS ko [PUBLIC] <font color=#999999>Knockout</font>
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
FIELD savedLists: ko.observableArray [PUBLIC] <font color=#999999>index.html</font>
FIELD userNameToAddIsValid [PUBLIC] <font color=#999999>index.html</font>
VARIABLE $data: ko.$bindings [PRIVATE] <font color=#999999>index.html</font>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Code completion result for source line:
CONSTRUCTO MyViewModel [PUBLIC] index.html
CLASS $context [PRIVATE] <font color=#999999>index.html</font>
CLASS $parentContext [PRIVATE] <font color=#999999>index.html</font>
CLASS import [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS ko [PUBLIC] <font color=#999999>Knockout</font>
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
FIELD credits: Number [PUBLIC] index.html
FIELD name: String [PUBLIC] index.html
VARIABLE $data [PRIVATE] <font color=#999999>index.html</font>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Code completion result for source line:
CONSTRUCTO MyViewModel [PUBLIC] index.html
CLASS $context [PRIVATE] <font color=#999999>index.html</font>
CLASS $parentContext [PRIVATE] <font color=#999999>index.html</font>
CLASS import [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS ko [PUBLIC] <font color=#999999>Knockout</font>
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
FIELD credits: Number [PUBLIC] index.html
FIELD name: String [PUBLIC] index.html
VARIABLE $data [PRIVATE] <font color=#999999>index.html</font>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Code completion result for source line:
------------------------------------
CLASS $context [PRIVATE] <font color=#999999>index.html</font>
CLASS $parentContext [PRIVATE] <font color=#999999>index.html</font>
CLASS import [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS ko [PUBLIC] <font color=#999999>Knockout</font>
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS viewModel [PUBLIC] index.html
FIELD months: Array [PUBLIC] index.html
FIELD name: String [PUBLIC] index.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ CONSTRUCTO Bulici [PUBLIC] index.html
CONSTRUCTO Clobrda [PUBLIC] index.html
CLASS $context [PRIVATE] <font color=#999999>index.html</font>
CLASS $parentContext [PRIVATE] <font color=#999999>index.html</font>
CLASS import [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS ko [PUBLIC] <font color=#999999>Knockout</font>
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
METHOD init(): undefined [PUBLIC] index.html
FIELD age: Number [PUBLIC] index.html
FIELD jmeno [PUBLIC] index.html
Expand Down
3 changes: 2 additions & 1 deletion webcommon/javascript2.editor/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
# specific language governing permissions and limitations
# under the License.

javac.source=1.8
javac.source=11
javac.target=11
javac.compilerargs=-Xlint -Xlint:-serial
javadoc.arch=${basedir}/arch.xml
nbm.needs.restart=true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.
*/
package org.netbeans.modules.javascript2.editor.classpath;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.netbeans.modules.javascript2.editor.parser.JsParserResult;
import org.netbeans.modules.javascript2.model.api.JsObject;
import org.netbeans.modules.javascript2.model.api.Model;
import org.netbeans.modules.javascript2.model.spi.ModelElementFactory;
import org.netbeans.modules.javascript2.model.spi.ModelInterceptor;
import org.netbeans.modules.javascript2.model.spi.ModelInterceptor.Registration;
import org.netbeans.modules.parsing.api.ParserManager;
import org.netbeans.modules.parsing.api.Source;
import org.netbeans.modules.parsing.spi.ParseException;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.URLMapper;

@Registration
public class MetaPropertiesModelContributor implements ModelInterceptor {

private static final Logger LOG = Logger.getLogger(MetaPropertiesModelContributor.class.getName());

private static Model GLOBALS = null;

@Override
public Collection<JsObject> interceptGlobal(ModelElementFactory factory, FileObject fo) {
if(GLOBALS == null) {
try {
FileObject globalsJS = URLMapper.findFileObject(MetaPropertiesModelContributor.class.getResource("metaproperties.js")); //NOI18N
Source source = Source.create(globalsJS);
ParserManager.parse(Set.of(source), (resultIterator) -> {
Model model = Model.getModel((JsParserResult) resultIterator.getParserResult(), true);
JsObject globalsVariables = model.getGlobalObject().getProperty("metaproperties"); //NOI18N
for(String propertyName: new ArrayList<>(globalsVariables.getProperties().keySet())) {
globalsVariables.moveProperty(propertyName, model.getGlobalObject());
}
model.getGlobalObject().getProperties().remove("metaproperties"); //NOI18N
GLOBALS = model;
});
} catch (ParseException ex) {
LOG.log(Level.WARNING, "Failed to initialize metaproperties.js to supply i.e. new.target and import.meta");
}
}
JsObject globals = GLOBALS.getGlobalObject();
return List.of(globals);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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 metaproperties = {};

metaproperties.new = {};
/**
* The <strong><code>new.target</code></strong> meta-property lets you detect
* whether a function or constructor was called using the <code>new</code>
* operator. In constructors and functions invoked using the <code>new</code>
* operator, <strong><code>new.target</code></strong> returns a reference to the
* constructor or function that new was called upon. In normal function calls,
* <strong><code>new.target</code></strong> is undefined.
*/
metaproperties.new.target = function(){};

metaproperties.import = {};
metaproperties.import.meta = {};
/**
* The full URL to the module, includes query parameters and/or hash (following
* the ? or #).
*
* In browsers, this is either the URL from which the script was obtained (for
* external scripts), or the URL of the containing document (for inline
* scripts).
*
* In Node.js, this is the file path (including the file:// protocol).
*
* @type String|null
*/
metaproperties.import.meta.url = "";
/**
* import.meta.resolve() is a built-in function defined on the import.meta
* object of a JavaScript module that resolves a module specifier to a URL using
* the current module's URL as base.
*
* @param {String} moduleName
* @returns {String}
*/
metaproperties.import.meta.resolve = function(moduleName) {};
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ CONSTRUCTO Uint8ClampedArray [PUBLIC] JS Platform
CONSTRUCTO WeakMap [PUBLIC] JS Platform
CONSTRUCTO WeakRef [PUBLIC] JS Platform
CONSTRUCTO WeakSet [PUBLIC] JS Platform
CLASS import [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
METHOD ImportAssertions [PUBLIC] JS Platform
METHOD Intl(): undefined [PUBLIC] JS Platform
METHOD Math [PUBLIC] JS Platform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var hisObject = new |MyObject();
------------------------------------
CONSTRUCTO sheObject(): sheObject [PRIVATE] constructors.js
CLASS Context [PUBLIC] constructors.js
CLASS import [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
METHOD MyObject(): undefined [PRIVATE] constructors.js
METHOD test() [PUBLIC] constructors.js
METHOD yourObject(): undefined [PRIVATE] constructors.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Code completion result for source line:
<script>|
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
CLASS import [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
KEYWORD async ECMAScript 7 - 2016 (Experimental)
KEYWORD await ECMAScript 7 - 2016 (Experimental)
KEYWORD break null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Code completion result for source line:
------------------------------------
CONSTRUCTO Test [PUBLIC] issue232178.js
CLASS document [PUBLIC] issue232178.js
CLASS import [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS synergy [PUBLIC] issue232178.js
CLASS util [PUBLIC] issue232178.js
METHOD Date [PUBLIC] issue232178.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Code completion result for source line:
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
CLASS console [PUBLIC] issue240914.js
CLASS import [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS issue290914 [PUBLIC] issue240914.js
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
FIELD age: Number [PUBLIC] issue240914.js
FIELD name: String [PUBLIC] issue240914.js
KEYWORD async ECMAScript 7 - 2016 (Experimental)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Code completion result for source line:
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
CLASS console [PUBLIC] issue240914.js
CLASS import [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS issue290914 [PUBLIC] issue240914.js
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS prop3 [PUBLIC] issue240914.js
METHOD create(): issue290914 [PUBLIC] issue240914.js
FIELD prop1: Number [PUBLIC] issue240914.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Code completion result for source line:
n|;//test1
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
CLASS new [PUBLIC] metaproperties.js
FIELD name: String [PUBLIC] controllers.js
KEYWORD new null
KEYWORD null null
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ CONSTRUCTO Address [PUBLIC] typeInferenceNew.js
CONSTRUCTO Car [PUBLIC] typeInferenceNew.js
CLASS Context [PUBLIC] typeInferenceNew.js
CLASS formatter [PUBLIC] typeInferenceNew.js
CLASS import [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
VARIABLE object: Address|Car [PUBLIC] typeInferenceNew.js
KEYWORD async ECMAScript 7 - 2016 (Experimental)
KEYWORD await ECMAScript 7 - 2016 (Experimental)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ CONSTRUCTO WeakMap [PUBLIC] JS Platform
CONSTRUCTO WeakRef [PUBLIC] JS Platform
CONSTRUCTO WeakSet [PUBLIC] JS Platform
CLASS b [PUBLIC] with1.js
CLASS import [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS prototype [PUBLIC] JS Platform
CLASS z [PUBLIC] with1.js
METHOD ImportAssertions [PUBLIC] JS Platform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ CONSTRUCTO WeakMap [PUBLIC] JS Platform
CONSTRUCTO WeakRef [PUBLIC] JS Platform
CONSTRUCTO WeakSet [PUBLIC] JS Platform
CLASS b [PUBLIC] with3.js
CLASS import [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS z [PUBLIC] with3.js
METHOD ImportAssertions [PUBLIC] JS Platform
METHOD Intl(): undefined [PUBLIC] JS Platform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ CONSTRUCTO Uint8ClampedArray [PUBLIC] JS Platform
CONSTRUCTO WeakMap [PUBLIC] JS Platform
CONSTRUCTO WeakRef [PUBLIC] JS Platform
CONSTRUCTO WeakSet [PUBLIC] JS Platform
CLASS import [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS ko [PUBLIC] with5.js
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS z [PUBLIC] declaration.js
METHOD ImportAssertions [PUBLIC] JS Platform
METHOD Intl(): undefined [PUBLIC] JS Platform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ CONSTRUCTO WeakMap [PUBLIC] JS Platform
CONSTRUCTO WeakRef [PUBLIC] JS Platform
CONSTRUCTO WeakSet [PUBLIC] JS Platform
CLASS b [PUBLIC] with4.js
CLASS import [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS prototype [PUBLIC] JS Platform
CLASS z [PUBLIC] with4.js
METHOD ImportAssertions [PUBLIC] JS Platform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ CONSTRUCTO WeakMap [PUBLIC] JS Platform
CONSTRUCTO WeakRef [PUBLIC] JS Platform
CONSTRUCTO WeakSet [PUBLIC] JS Platform
CLASS b [PUBLIC] with4.js
CLASS import [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS new [PUBLIC] <font color=#999999>metaproperties.js</font>
CLASS prototype [PUBLIC] JS Platform
CLASS z [PUBLIC] with4.js
METHOD ImportAssertions [PUBLIC] JS Platform
Expand Down
Loading

0 comments on commit bb2d43f

Please sign in to comment.