Rule
A Rule which can be matched against a Context
Hierarchy
- Rule
Index
Constructors
Accessors
Methods
Constructors
constructor
+ new Rule(ruleDef
: RuleDefinition): Rule
Applies defaults, assigns some metadata.
Parameters:
Name | Type | Description |
---|---|---|
ruleDef | RuleDefinition |
Returns: Rule
Accessors
description
• get description(): any
Returns: any
id
• get id(): string
type
{string}
Returns: string
meta
• get meta(): any
type
{object}
todo
update with schema for ‘meta’ prop
Returns: any
schema
• get schema(): any
Returns: any
url
• get url(): any
Returns: any
validate
• get validate(): Function
Lazily-created function which validates the schema itself when first referenced, creates a config-validation function, caches it, then asserts any user-supplied config is valid using said function.
throws
If user-supplied config is invalid.
Returns: Function
Methods
handlers
▸ handlers(config?
: any): Promise‹any›
Calls the inspect()
function of a Rule impl, which will return one or
more “handler” functions. Note inspect()
might return a Promise
which
resolves to the “handler” functions.
Parameters:
Name | Type |
---|---|
config? | any |
Returns: Promise‹any›
inspect
▸ inspect(__namedParameters
: object): Observable‹Message›
Given a stream of Report objects and an optional configuration, execute the
inspect()
function of the rule definition, which should return a “next”
handler function, or an object having handler function props next
and
complete
.
1. Normalize the result of the inspect()
so we can make assumptions about
the shape of the returned value.
2. For each Report
(report
), run the next
handler as if it returned
a Promise
. This handler is passed the report
, and any Error
s
thrown are trapped. The handler may return a string (“message”), a
partial Message
object, Array
thereof, or a Promise
resolving to
any of that stuff, or just undefined
in the case of “nothing to
mention”
3. Return values are correlated with the filepath of the report. Note that
Report
objects may not have a filepath if they were not loaded from
file.
4. Once all Report
s have passed through the next
handler, call the
complete
handler. It receives no report
, and can be used in tandem
with next
to perform aggregation. Supports the same return values as
next
5. Finally, filter out empty/falsy partial Message
s (e.g., those without
actual string
message
props), and normalize the Message
by adding
relevant metadata (Rule
ID, user-supplied config used, default
severity, etc.)
Parameters:
▪ __namedParameters: object
Name | Type |
---|---|
config | any |
reports | Observable‹Report‹›› |
Returns: Observable‹Message›
toRuleConfig
▸ toRuleConfig(config
: any): RuleConfig
Given a Config, get associated rule config and create a RuleConfig
.
Parameters:
Name | Type |
---|---|
config | any |
Returns: RuleConfig
Static
applyDefaults
▸ applyDefaults(ruleDef
: Partial‹RuleDefinition›): RuleDefinition
Applies defaults to a rule definition during Rule
construction.
Parameters:
Name | Type | Description |
---|---|---|
ruleDef | Partial‹RuleDefinition› | Raw rule definition |
Returns: RuleDefinition
Static
create
▸ create(ruleDefinition
: RuleDefinition): Rule
Creates a Rule
from a user-defined (or builtin) RuleDefinition
, which
is the exports of a rule definition file.
Parameters:
Name | Type | Description |
---|---|---|
ruleDefinition | RuleDefinition | Rule definition |
Returns: Rule
New rule
Static
normalizeHandler
▸ normalizeHandler(): OperatorFunction‹function | RuleHandlerObject, RuleHandlerObject›
Operator. Given a “handler” (returned by the rule definition’s inspect
function), normalize it into an object (since it may be just a function)
Returns: OperatorFunction‹function | RuleHandlerObject, RuleHandlerObject›