-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7211 from matthiasblaesing/js-import-meta
JS: Handle meta-properties new.target and import.meta and improve code completion with newlines and comments
- Loading branch information
Showing
82 changed files
with
1,119 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...src/org/netbeans/modules/javascript2/editor/classpath/MetaPropertiesModelContributor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...avascript2.editor/src/org/netbeans/modules/javascript2/editor/classpath/metaproperties.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
webcommon/javascript2.editor/test/unit/data/testfiles/completion/gh5919/gh5919_0.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
6 changes: 6 additions & 0 deletions
6
...2.editor/test/unit/data/testfiles/completion/gh5919/gh5919_0.js.testGH5919_0_1.completion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
23 changes: 23 additions & 0 deletions
23
webcommon/javascript2.editor/test/unit/data/testfiles/completion/gh5919/gh5919_1.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
5 changes: 5 additions & 0 deletions
5
...2.editor/test/unit/data/testfiles/completion/gh5919/gh5919_1.js.testGH5919_1_1.completion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
5 changes: 5 additions & 0 deletions
5
...2.editor/test/unit/data/testfiles/completion/gh5919/gh5919_1.js.testGH5919_1_2.completion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
6 changes: 6 additions & 0 deletions
6
...2.editor/test/unit/data/testfiles/completion/gh5919/gh5919_1.js.testGH5919_1_3.completion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
5 changes: 5 additions & 0 deletions
5
...2.editor/test/unit/data/testfiles/completion/gh5919/gh5919_1.js.testGH5919_1_4.completion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
6 changes: 6 additions & 0 deletions
6
...2.editor/test/unit/data/testfiles/completion/gh5919/gh5919_1.js.testGH5919_1_5.completion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
5 changes: 5 additions & 0 deletions
5
...2.editor/test/unit/data/testfiles/completion/gh5919/gh5919_1.js.testGH5919_1_6.completion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
5 changes: 5 additions & 0 deletions
5
...2.editor/test/unit/data/testfiles/completion/gh5919/gh5919_1.js.testGH5919_1_7.completion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
23 changes: 23 additions & 0 deletions
23
webcommon/javascript2.editor/test/unit/data/testfiles/completion/gh5919/gh5919_2.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
5 changes: 5 additions & 0 deletions
5
...2.editor/test/unit/data/testfiles/completion/gh5919/gh5919_2.js.testGH5919_2_1.completion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
5 changes: 5 additions & 0 deletions
5
...2.editor/test/unit/data/testfiles/completion/gh5919/gh5919_2.js.testGH5919_2_2.completion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.