Skip to content

Commit a4430ff

Browse files
authored
Extract production rules dfns from ECMAScript spec (#1881)
Via mdn/browser-compat-data#27246 This completes preprocessing of the ECMAScript spec to also extract production rules that are needed for documentation purpose. These production rules are defined in `<emu-production>` tags wrapped in `<emu-grammar type="definition">` tags. Annex B re-defines some of the production rules for Web browser hosts. To avoid duplication, the code adds a `"for": ["Web browsers"]` to disambiguate. All definitions are created with a "grammar" definition type, and flagged as "private" (at least until it becomes clear that the terms are needed for xref purpose). Use of "grammar" is not fantastic but is already done by the CSP spec and it avoids introducing a new definition type that isn't explicitly supported by spec authoring tools.
1 parent 468b57b commit a4430ff

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/browserlib/extract-dfns.mjs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,21 @@ function preProcessEcmascript() {
705705
}
706706
});
707707

708+
// Extract production rules
709+
[...document.querySelectorAll("emu-grammar[type=definition] emu-production")]
710+
.forEach(el => {
711+
const dfn = wrapWithDfn(el);
712+
dfn.id = el.id;
713+
dfn.dataset.lt = el.getAttribute("name");
714+
dfn.dataset.dfnType = "grammar";
715+
dfn.dataset.noexport = "";
716+
if (el.closest('[data-reffy-page$="additional-ecmascript-features-for-web-browsers.html"]')) {
717+
// Production rules in Annex B replace some of the production rules
718+
// defined in other sections for web browser hosts.
719+
dfn.dataset.dfnFor = "Web browsers";
720+
}
721+
});
722+
708723
[...document.querySelectorAll("dfn")]
709724
.filter(legacySectionFilter)
710725
.forEach(el => {

test/extract-dfns.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ const tests = [
214214
spec: "ecmascript"
215215
},
216216
{
217-
title: "extracts properties of the globalThis object in ecmascript spec",
217+
title: "extracts properties of the globalThis object in ecmascript spec",
218218
html: '<emu-clause id="sec-value-properties-of-the-global-object"><h1><span class="secnum">25.1.5.1</span> Value Properties of the Global Object</h1><emu-clause id="foo"> <h1>Foo</h1></emu-clause></emu-clause>',
219219
changesToBaseDfn: [ {type: "attribute", "for": ["globalThis"], access: "public", definedIn: "heading", heading: { id: "foo", href: "about:blank#foo", title: "Foo"}}],
220220
spec: "ecmascript"
@@ -292,6 +292,40 @@ const tests = [
292292
changesToBaseDfn: [{linkingText: [ "Function"], type: "dfn", "for": ["ECMAScript Execution Contexts"], access: "public", definedIn: "table", heading: { id: "foo", href: "about:blank#foo", title: "Heading"}}],
293293
spec: "ecmascript"
294294
},
295+
{
296+
title: 'extracts production rules from ecmascript spec',
297+
html: `<emu-grammar type="definition">
298+
<emu-production name="SpreadElement" id="foo">
299+
...
300+
</emu-production>
301+
</emu-grammar>`,
302+
changesToBaseDfn: [{
303+
linkingText: ["SpreadElement"],
304+
type: "grammar"
305+
}],
306+
spec: "ecmascript"
307+
},
308+
{
309+
title: 'scopes Annex B production rules from ecmascript spec',
310+
html: `<section data-reffy-page="https://example.org/additional-ecmascript-features-for-web-browsers.html">
311+
<emu-grammar type="definition">
312+
<emu-production name="SpreadElement" id="foo">
313+
...
314+
</emu-production>
315+
</emu-grammar>
316+
</section>`,
317+
changesToBaseDfn: [{
318+
href: "https://example.org/additional-ecmascript-features-for-web-browsers.html#foo",
319+
linkingText: ["SpreadElement"],
320+
type: "grammar",
321+
for: ["Web browsers"],
322+
heading: {
323+
href: 'https://example.org/additional-ecmascript-features-for-web-browsers.html',
324+
title: ''
325+
}
326+
}],
327+
spec: "ecmascript"
328+
},
295329
{
296330
title: 'assign ids to un-id’d definitions of the ecmascript spec',
297331
html: '<emu-clause id="foo"><h1><span class="secnum">9.4</span> Execution Contexts</h1><p>An <dfn variants="execution contexts">execution context</dfn> is a specification device that is used to track the runtime evaluation of code by an ECMAScript implementation.</p></emu-clause>',

0 commit comments

Comments
 (0)