1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//! Javascript's role rows. The Halstead keyword roles (#1693), the
//! paying-construct roles — `Comparison`, `ShortCircuit`, `EagerLogical`,
//! `Ternary`, `CaseArm`, `Guard`, `NullSafe` (#1694, #1695) — the space
//! tree (#1696) and the control flow — `Branch`, `Switch`, `Loop`,
//! `Catch`, `ElseIf`, `Exit` (#1697) — and
//! the documentation rows (#1699) are declared.
use crate::Javascript;
use crate::role_table;
use crate::roles::control_flow::else_if_parent_clause_gate;
use crate::roles::js_family::{
js_doc_gates, js_family_binding_gates, js_family_closures, js_family_comparison_payers,
js_family_containers, js_family_decl_keywords, js_family_doc_gates, js_family_doc_targets,
js_family_exits, js_family_func_openers, js_family_loops, js_family_modifiers,
js_family_short_circuit_payers, js_family_strings,
};
// The chained `if` sits directly inside an `else_clause`; a nested one
// under `else { … }` sits inside the statement block (#1697).
else_if_parent_clause_gate!(Javascript, ElseClause);
js_family_binding_gates!(Javascript, JavascriptCode, FieldDefinition);
// The documentation gates, per dialect (#1699): JSDoc ignores `/***`; plain JavaScript follows it.
js_family_doc_gates!(Javascript, javadoc_block);
js_doc_gates!(Javascript);
role_table! {
lang: Javascript;
// Two `call_expression` ids at the pin; the second is the alias a
// decorator call takes, never emitted (probed with `bca dump` over
// `@dec(1)`), and listed defensively (grammar-dispatch §1).
Call { owns [CallExpression, CallExpression2] }
// `String2` (kind id 221) is JS's anonymous string-*literal* alias
// (e.g. the module string in `import "m"`), a string alongside
// `String` (#283).
StringLit { owns [..js_family_strings!(), String2] wraps [TemplateSubstitution] }
// The one `binary_expression` kind carries every binary operator;
// its `operator` field says which, so each row's owner holds its
// payer (`roles/js_family.rs` has the lists). `??` is `NullSafe`'s
// and `&&=` / `||=` are assignments.
Comparison {
owns [BinaryExpression(holds_payer)]
pays [..js_family_comparison_payers!()]
project "operator"
}
ShortCircuit {
owns [BinaryExpression(holds_payer)]
pays [..js_family_short_circuit_payers!()]
project "operator"
}
EagerLogical { absent "JavaScript's `&` / `|` / `^` are bitwise" }
// The `ternary_expression` owns it and its `?` pays, ungated: the
// JavaScript grammars spell the bare token nowhere else, and a `?`
// error recovery strands under an `ERROR` (`let c = a ?` at end of
// input) has always paid (the TypeScript grammars gate theirs, for
// the ten type productions `roles/js_family.rs` names).
Ternary {
owns [TernaryExpression]
pays [QMARK]
project "operator"
}
// Two structural forms, each one decision per token (#281, #248):
// the null coalescing `a ?? b`, a `binary_expression` owned through
// its `operator` field, and the optional chain `a?.b` / `a?.[0]` /
// `a?.()`, whose `?.` the JavaScript grammars emit as the bare
// `optional_chain` token (no wrapper) — the TypeScript grammars wrap
// it, and their rows say so. The compound `??=` is an
// `augmented_assignment_expression`, not a `binary_expression`: it
// stays the consumers' own, recorded here as considered.
NullSafe {
owns [BinaryExpression(holds_payer)]
pays [QMARKQMARK, OptionalChain]
project "operator"
excludes [QMARKQMARKEQ "the compound `??=`, an assignment ABC pays as one and a decision cyclomatic pays as its own (#236, #248)"]
}
// A `switch_case` owns a decision and its `case` keyword pays; the
// `switch_default` and its `default` keyword are the fallback and
// pay nothing (lesson 11, #469).
CaseArm {
owns [SwitchCase(holds_payer)]
pays [Case]
excludes [
SwitchDefault "the fallback arm, no decision (lesson 11, #469)",
Default "the fallback label's keyword",
]
}
Guard { absent "JavaScript has no guard clause" }
// The JS family's control flow (#1697): the one-kind rows are spelled
// inline, the loop and exit lists are fragments of
// `roles/js_family.rs`, and no dialect has an extra — the four
// grammars share the shape and differ only in kind ids. The `ElseIf`
// gate is emitted beside this table.
Branch { owns [IfStatement] pays [If] }
Switch { owns [SwitchStatement] }
Loop { owns [..js_family_loops!()] pays [For, While] }
Catch { owns [CatchClause] pays [Catch] }
ElseIf { owns [IfStatement(gated_by continues_else_clause)] pays [] }
Exit { owns [..js_family_exits!()] }
// The shared JS-family `static` (`roles/js_family.rs`) plus
// `Staticget`, one token spelling `static get` followed by a
// newline — the static getter whose name starts the next line —
// which these two grammars alone have. It is billed beside `static`
// (#1563) so that form bills a modifier rather than nothing; being
// one node it is one operator.
Modifier { owns [..js_family_modifiers!(), Staticget] }
DeclKeyword { owns [..js_family_decl_keywords!()] }
Primitive { absent "JavaScript has no type names" }
// The shared JS-family openers, closures and containers
// (`roles/js_family.rs`); the binding gates are emitted above, with
// this grammar's class-field kind.
FuncOpener { owns [..js_family_func_openers!()] }
Closure { owns [..js_family_closures!()] }
Container { owns [..js_family_containers!()] }
// A class field (`x = v;`); one whose initialiser is a function is
// a method written as a field, which the consumers tell apart
// themselves — the row recognises the declaration, the consumer
// reads its value.
Member { owns [FieldDefinition] }
// The two comment kinds — a `/* */` or `//` `comment` and the Annex-B
// `<!-- -->` `html_comment` (#697); only the first can open with the
// tool's block marker (#1699).
Comment { owns [Comment, HtmlComment] }
DocComment { owns [Comment(text_gated_by opens_with_doc_block_marker no_chain)] }
Docstring { absent "JavaScript documents with JSDoc comments, not string literals" }
// What JSDoc's `astnode.getInfo` names: the family fragment, plus an
// export other than a bare `export { … }` list and an expression
// statement assigning to a name or a member. Every target but the
// class, method and field is outside the space-tree rows — the
// parity exceptions `roles/doc_tests/parity.rs` records.
DocTarget {
owns [
..js_family_doc_targets!(),
UsingDeclaration(gated_by first_declarator_binds_a_name no_chain),
FieldDefinition,
ExportStatement(gated_by export_declares no_chain),
ExpressionStatement(gated_by assigns_to_a_name_or_member no_chain),
]
}
DocRunOpener { absent "JSDoc attaches every doc comment to a declaration or a named symbol; none documents by its own content" }
DocRunDirective { absent "JSDoc reads no directive comment" }
// Nothing beyond the callables and the closures: JSDoc documents
// inner symbols (`outer~inner`), so a doc comment in a body stays
// documentation (`DOC_COMMENTS_IN_FUNCTION_BODIES`, #1597), and which
// child is the body — a statement block, an arrow's expression — is
// the Checker's body-child test, not a kind.
DocBodyResidual { absent "only the callables and closures hold a doc comment in a body; the body-child test is the Checker's" }
}