Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support [Exposed=*] #634

Merged
merged 3 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ Extended attributes look like this:
* `"decimal-list"`
* `"integer"`
* `"integer-list"`
* `"*"`
* `parent`: The container of this type as an Object.

### Default and Const Values
Expand Down
29 changes: 20 additions & 9 deletions lib/productions/extended-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ class ExtendedAttributeParameters extends Base {
const ret = autoParenter(
new ExtendedAttributeParameters({ source: tokeniser.source, tokens })
);
ret.list = [];
if (tokens.assign) {
tokens.asterisk = tokeniser.consume("*");
if (tokens.asterisk) {
return ret.this;
}
tokens.secondaryName = tokeniser.consumeKind(...extAttrValueSyntax);
}
tokens.open = tokeniser.consume("(");
Expand All @@ -77,13 +82,18 @@ class ExtendedAttributeParameters extends Base {
}

get rhsIsList() {
return this.tokens.assign && !this.tokens.secondaryName;
return (
this.tokens.assign && !this.tokens.asterisk && !this.tokens.secondaryName
);
}

get rhsType() {
if (this.rhsIsList) {
return this.list[0].tokens.value.type + "-list";
}
if (this.tokens.asterisk) {
return "*";
}
if (this.tokens.secondaryName) {
return this.tokens.secondaryName.type;
}
Expand All @@ -95,15 +105,14 @@ class ExtendedAttributeParameters extends Base {
const { rhsType } = this;
return w.ts.wrap([
w.token(this.tokens.assign),
w.token(this.tokens.asterisk),
w.reference_token(this.tokens.secondaryName, this.parent),
w.token(this.tokens.open),
...(!this.list
? []
: this.list.map((p) => {
return rhsType === "identifier-list"
? w.identifier(p, this.parent)
: p.write(w);
})),
...this.list.map((p) => {
return rhsType === "identifier-list"
? w.identifier(p, this.parent)
: p.write(w);
}),
w.token(this.tokens.close),
]);
}
Expand Down Expand Up @@ -143,7 +152,9 @@ export class SimpleExtendedAttribute extends Base {
}
const value = this.params.rhsIsList
? list
: unescape(tokens.secondaryName.value);
: this.params.tokens.secondaryName
? unescape(tokens.secondaryName.value)
: null;
return { type, value };
}
get arguments() {
Expand Down
1 change: 1 addition & 0 deletions lib/tokeniser.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const punctuations = [
"=",
">",
"?",
"*",
"[",
"]",
"{",
Expand Down
24 changes: 24 additions & 0 deletions test/syntax/baseline/exposed-asterisk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"type": "interface",
"name": "X",
"inheritance": null,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm? Is null deprecated in JSON?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don’t think so? Weird.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is null deprecated in JSON?

Almost certainly not, because as far as I understand it, JSON syntax is forever set in stone, which unfortunately means that comments will almost certainly never be supported, even though they existed in the initial drafts.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's my understanding. Not sure why the red highlight 🤔. Also not sure where to report.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it just got fixed in Nixinova/NovaGrammars#7, which is the project @github uses for highlighting JSON syntax: https://github.com/github/linguist/tree/master/vendor.

"members": [],
"extAttrs": [
{
"type": "extended-attribute",
"name": "Exposed",
"rhs": {
"type": "*",
"value": null
},
"arguments": []
}
],
"partial": false
},
{
"type": "eof",
"value": ""
}
]
2 changes: 2 additions & 0 deletions test/syntax/idl/exposed-asterisk.webidl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Exposed=*]
interface X {};
14 changes: 7 additions & 7 deletions webpack.config.cjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const path = require("path");
const TerserPlugin = require('terser-webpack-plugin');
const TerserPlugin = require("terser-webpack-plugin");
module.exports = {
entry: "./index.js",
output: {
filename: "webidl2.js",
path: path.resolve(__dirname, "dist"),
library: "WebIDL2",
libraryTarget: "umd",
globalObject: "globalThis"
globalObject: "globalThis",
},
mode: "production",
devtool: "source-map",
Expand All @@ -16,9 +16,9 @@ module.exports = {
new TerserPlugin({
terserOptions: {
keep_classnames: true,
sourceMap: true
}
})
]
}
sourceMap: true,
},
}),
],
},
};