-
-
Notifications
You must be signed in to change notification settings - Fork 63
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: allow adding additional members to production parse methods #745
Merged
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
074d05d
feat: allow adding additional members to production parse methods
pyoor bb9f3b4
feat: add 'extensions' option for extending existing productions
pyoor 0a2b860
fix: remove unnecessary spread operator
pyoor 491c5cf
refactor: rename extension 'callback-interface' to callbackInterface
pyoor 883ca3c
test: improve extension parsing tests
pyoor aabbb9e
docs: fix up jsdoc definition for ParserOptions
pyoor e20190e
test: remove use strict
pyoor 48c4dc2
test: merge extension test into custom-production
pyoor ca97d20
test: replace customProduction with top-level CustomAttribute
pyoor 66e3862
test: remove extension argument from collection utility
pyoor 38c949e
docs: normalize use of Token import
pyoor 12e19d0
test: fix import of expect function
pyoor 364de96
docs: mark args as any
pyoor 59b4172
docs: fix path to container.js
pyoor 38ffe97
refactor: remove unnecessary spread operator
pyoor 688adf2
docs: fix jsdoc types
pyoor bc54d0e
docs: fix jsdoc types
pyoor bdf5ee4
fix: remove iheritance attribute from CallbackInterface
pyoor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,87 @@ | ||
"use strict"; | ||
|
||
import { expect } from "expect"; | ||
import { parse, write } from "webidl2"; | ||
|
||
describe("Writer template functions", () => { | ||
const customIdl = ` | ||
interface X {}; | ||
custom Y; | ||
`; | ||
import { parse, write } from "webidl2"; | ||
import { Base } from "../lib/productions/base.js"; | ||
import { | ||
autoParenter, | ||
type_with_extended_attributes, | ||
} from "../lib/productions/helpers.js"; | ||
|
||
/** | ||
* @param {import("../lib/tokeniser").Tokeniser} tokeniser | ||
*/ | ||
const customProduction = (tokeniser) => { | ||
const { position } = tokeniser; | ||
const base = tokeniser.consumeIdentifier("custom"); | ||
if (!base) { | ||
return; | ||
} | ||
const tokens = { base }; | ||
tokens.name = tokeniser.consumeKind("identifier"); | ||
tokens.termination = tokeniser.consume(";"); | ||
if (!tokens.name || !tokens.termination) { | ||
tokeniser.unconsume(position); | ||
class CustomAttribute extends Base { | ||
static parse(tokeniser) { | ||
const start_position = tokeniser.position; | ||
const tokens = {}; | ||
const ret = autoParenter( | ||
new CustomAttribute({ source: tokeniser.source, tokens }) | ||
); | ||
tokens.base = tokeniser.consumeIdentifier("custom"); | ||
if (!tokens.base) { | ||
tokeniser.unconsume(start_position); | ||
return; | ||
} | ||
return { | ||
type: "custom", | ||
tokens, | ||
/** @param {import("../lib/writer.js").Writer} w */ | ||
write(w) { | ||
return w.ts.wrap([ | ||
w.token(this.tokens.base), | ||
w.token(this.tokens.name), | ||
w.token(this.tokens.termination), | ||
]); | ||
}, | ||
}; | ||
}; | ||
ret.idlType = | ||
type_with_extended_attributes(tokeniser, "attribute-type") || | ||
tokeniser.error("Attribute lacks a type"); | ||
tokens.name = | ||
tokeniser.consumeKind("identifier") || | ||
tokeniser.error("Attribute lacks a name"); | ||
tokens.termination = | ||
tokeniser.consume(";") || | ||
tokeniser.error("Unterminated attribute, expected `;`"); | ||
return ret.this; | ||
} | ||
|
||
const result = parse(customIdl, { | ||
productions: [customProduction], | ||
concrete: true, | ||
get type() { | ||
return "custom attribute"; | ||
} | ||
|
||
write(w) { | ||
const { parent } = this; | ||
return w.ts.definition( | ||
w.ts.wrap([ | ||
this.extAttrs.write(w), | ||
w.token(this.tokens.base), | ||
w.ts.type(this.idlType.write(w)), | ||
w.name_token(this.tokens.name, { data: this, parent }), | ||
w.token(this.tokens.termination), | ||
]), | ||
{ data: this, parent } | ||
); | ||
} | ||
} | ||
|
||
describe("Parse IDLs using custom productions", () => { | ||
it("Parse and rewrite top-level custom attribute", () => { | ||
const customIdl = "custom long bar;"; | ||
const result = parse(customIdl, { | ||
productions: [CustomAttribute.parse], | ||
concrete: true, | ||
}); | ||
expect(result[0].type).toBe("custom attribute"); | ||
|
||
const rewritten = write(result); | ||
expect(rewritten).toBe(customIdl); | ||
}); | ||
expect(result[0].type).toBe("interface"); | ||
expect(result[1].type).toBe("custom"); | ||
}); | ||
|
||
const rewritten = write(result); | ||
expect(rewritten).toBe(customIdl); | ||
describe("Parse IDLs using custom extensions", () => { | ||
[ | ||
["callback interface", "callbackInterface"], | ||
["dictionary", "dictionary"], | ||
["interface", "interface"], | ||
["interface mixin", "mixin"], | ||
["namespace", "namespace"], | ||
].forEach(([type, key]) => { | ||
it(`Attribute on ${type}`, () => { | ||
const customIdl = `${type} Foo { | ||
custom long bar; | ||
};`; | ||
const result = parse(customIdl, { | ||
concrete: true, | ||
extensions: { [key]: { extMembers: [[CustomAttribute.parse]] } }, | ||
}); | ||
expect(result[0].type).toBe(type); | ||
expect(result[0].members[0].type).toBe("custom attribute"); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this change separately? We should either change all
partial
toinheritable
everywhere or keep it everywhere, and making that change here sounds like out-of scope.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't the issue here that callback interface doesn't support
partial
at all?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh hmm, I think we talked about it somewhere but can't find it anywhere, do you remember? I now wonder why we need it at all, neither the spec nor the mozilla-central uses inheritance for callback interfaces, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I found it, and I think we should just remove
inheritable
s here.