Skip to content

Commit

Permalink
Merge pull request #7211 from matthiasblaesing/js-import-meta
Browse files Browse the repository at this point in the history
JS: Handle meta-properties new.target and import.meta and improve code completion with newlines and comments
  • Loading branch information
matthiasblaesing committed Apr 10, 2024
2 parents 4b3eb69 + 9766a62 commit 1768511
Show file tree
Hide file tree
Showing 82 changed files with 1,119 additions and 50 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
Expand Up @@ -318,6 +318,12 @@ private static boolean acceptTokenChain(TokenSequence tokenSequence, Object[] to
int orgTokenSequencePos = tokenSequence.offset();
boolean accept = true;
boolean moreTokens = movePrevious ? tokenSequence.movePrevious() : true;
while((tokenSequence.token().id() == JsTokenId.BLOCK_COMMENT || tokenSequence.token().id() == JsTokenId.LINE_COMMENT)) {
moreTokens = tokenSequence.movePrevious();
if(! moreTokens) {
break;
}
}

for (int i = tokenIdChain.length - 1; i >= 0; i --){
Object tokenID = tokenIdChain[i];
Expand All @@ -328,8 +334,14 @@ private static boolean acceptTokenChain(TokenSequence tokenSequence, Object[] to
}

if (tokenID instanceof JsTokenId) {
if (tokenSequence.token().id() == tokenID){
if (tokenSequence.token().id() == tokenID) {
moreTokens = tokenSequence.movePrevious();
while ((tokenSequence.token().id() == JsTokenId.BLOCK_COMMENT || tokenSequence.token().id() == JsTokenId.LINE_COMMENT)) {
moreTokens = tokenSequence.movePrevious();
if (!moreTokens) {
break;
}
}
} else {
// NO MATCH
accept = false;
Expand All @@ -342,7 +354,7 @@ private static boolean acceptTokenChain(TokenSequence tokenSequence, Object[] to

tokenSequence.move(orgTokenSequencePos);
tokenSequence.moveNext();
return accept;
return accept;
}

private static void balanceBracketBack(TokenSequence<JsTokenId> ts) {
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
@@ -0,0 +1,20 @@
/*
* 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.
*/

impo
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Code completion result for source line:
impo|
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
CLASS import [PUBLIC] metaproperties.js
KEYWORD import ECMAScript 6 - 2015
Original file line number Diff line number Diff line change
@@ -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.
*/

import.meta.url;
function dummy() {
new.target;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Code completion result for source line:
import.|meta.url;
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
CLASS meta [PUBLIC] metaproperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Code completion result for source line:
import.me|ta.url;
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
CLASS meta [PUBLIC] metaproperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Code completion result for source line:
import.meta.|url;
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
METHOD resolve(moduleName: String): S [PUBLIC] <font color=#999999>metaproperties.js</font>
FIELD url: null|String [PUBLIC] <font color=#999999>metaproperties.js</font>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Code completion result for source line:
import.meta.ur|l;
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
FIELD url: null|String [PUBLIC] <font color=#999999>metaproperties.js</font>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Code completion result for source line:
ne|w.target;
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
CLASS new [PUBLIC] metaproperties.js
KEYWORD new null
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Code completion result for source line:
new.|target;
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
METHOD target(): undefined [PUBLIC] metaproperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Code completion result for source line:
new.targ|et;
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
METHOD target(): undefined [PUBLIC] metaproperties.js
Original file line number Diff line number Diff line change
@@ -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.
*/

import/*dummy*/.meta.url;
function dummy() {
new/*dummy*/.target;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Code completion result for source line:
import/*dummy*/.|meta.url;
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
CLASS meta [PUBLIC] metaproperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Code completion result for source line:
import/*dummy*/.me|ta.url;
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
CLASS meta [PUBLIC] metaproperties.js
Loading

0 comments on commit 1768511

Please sign in to comment.