-
Notifications
You must be signed in to change notification settings - Fork 874
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JS: Provide model data for meta properties new.target and import.meta
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
1 parent
86be3c4
commit bb2d43f
Showing
39 changed files
with
439 additions
and
14 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
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
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
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
Oops, something went wrong.