@markuplint/ml-config 3.4.0 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -49,7 +49,7 @@ function mergeRule(a, b) {
49
49
  }
50
50
  return oB;
51
51
  }
52
- const value = Array.isArray(oA.value) && Array.isArray(b) ? [...oA.value, oB] : oB;
52
+ const value = Array.isArray(oA.value) && Array.isArray(oB) ? [...oA.value, ...oB] : oB;
53
53
  const res = (0, utils_1.cleanOptions)({ ...oA, value });
54
54
  (0, utils_1.deleteUndefProp)(res);
55
55
  return res;
@@ -145,7 +145,7 @@ function mergeRules(a, b) {
145
145
  }
146
146
  }
147
147
  (0, utils_1.deleteUndefProp)(res);
148
- return res;
148
+ return Object.freeze(res);
149
149
  }
150
150
  function optimizeRules(rules) {
151
151
  const res = {};
package/lib/types.d.ts CHANGED
@@ -1,183 +1,187 @@
1
1
  import type { ParserOptions } from '@markuplint/ml-ast';
2
2
  import type { RegexSelector } from '@markuplint/selector';
3
3
  export type { RegexSelector } from '@markuplint/selector';
4
- export interface Config {
5
- $schema?: string;
6
- extends?: string | string[];
7
- plugins?: (PluginConfig | string)[];
8
- parser?: ParserConfig;
9
- parserOptions?: ParserOptions;
10
- specs?: SpecConfig;
11
- excludeFiles?: string[];
12
- pretenders?: Pretender[];
13
- rules?: Rules;
14
- nodeRules?: NodeRule[];
15
- childNodeRules?: ChildNodeRule[];
16
- overrides?: Record<string, Omit<Config, '$schema' | 'extends' | 'overrides'>>;
17
- }
4
+ export type Config = {
5
+ readonly $schema?: string;
6
+ readonly extends?: string | readonly string[];
7
+ readonly plugins?: readonly (PluginConfig | string)[];
8
+ readonly parser?: ParserConfig;
9
+ readonly parserOptions?: ParserOptions;
10
+ readonly specs?: SpecConfig;
11
+ readonly excludeFiles?: readonly string[];
12
+ readonly pretenders?: readonly Pretender[];
13
+ readonly rules?: Rules;
14
+ readonly nodeRules?: readonly NodeRule[];
15
+ readonly childNodeRules?: readonly ChildNodeRule[];
16
+ readonly overrides?: Readonly<Record<string, OverrideConfig>>;
17
+ };
18
+ export type PrimitiveScalar = string | number | boolean;
19
+ export type PlainData = Nullable<PrimitiveScalar> | readonly PlainData[] | {
20
+ readonly [key: string]: PlainData | any;
21
+ };
22
+ export type NonNullablePlainData = PrimitiveScalar | readonly NonNullablePlainData[] | {
23
+ readonly [key: string]: NonNullablePlainData;
24
+ };
25
+ export type OverrideConfig = Omit<Config, '$schema' | 'extends' | 'overrides'>;
18
26
  export type PluginConfig = {
19
- name: string;
20
- settings: Record<string, any>;
27
+ readonly name: string;
28
+ readonly settings: Readonly<Record<string, NonNullablePlainData>>;
29
+ };
30
+ export type ParserConfig = {
31
+ readonly [extensionPattern: string]: string;
21
32
  };
22
- export interface ParserConfig {
23
- [extensionPattern: string]: string;
24
- }
25
33
  export type SpecConfig = {
26
- [extensionPattern: string]: string;
34
+ readonly [extensionPattern: string]: string;
27
35
  };
28
36
  export type Pretender = {
29
- /**
30
- * Target node selectors
31
- */
32
- selector: string;
33
- /**
34
- * If it is a string, it is resolved as an element name.
35
- * An element has the same attributes as the pretended custom element
36
- * because attributes are just inherited.
37
- *
38
- * If it is an Object, It creates the element by that.
39
- */
40
- as: string | OriginalNode;
37
+ /**
38
+ * Target node selectors
39
+ */
40
+ readonly selector: string;
41
+ /**
42
+ * If it is a string, it is resolved as an element name.
43
+ * An element has the same attributes as the pretended custom element
44
+ * because attributes are just inherited.
45
+ *
46
+ * If it is an Object, It creates the element by that.
47
+ */
48
+ readonly as: string | OriginalNode;
41
49
  };
42
50
  export type OriginalNode = {
43
- /**
44
- * Element name
45
- */
46
- element: string;
47
- /**
48
- * Namespace
49
- *
50
- * Supports `"svg"` and `undefined` only.
51
- * If it is `undefined`, the namespace is HTML.
52
- */
53
- namespace?: 'svg';
54
- /**
55
- * Attributes
56
- */
57
- attrs?: {
58
- /**
59
- * Attribute name
60
- */
61
- name: string;
62
- /**
63
- * If it omits this property, the attribute is resolved as a boolean.
64
- */
65
- value?:
66
- | string
67
- | {
68
- fromAttr: string;
69
- };
70
- }[];
71
- /**
72
- * To have attributes the defined element has.
73
- */
74
- inheritAttrs?: boolean;
75
- /**
76
- * ARIA properties
77
- */
78
- aria?: PretenderARIA;
51
+ /**
52
+ * Element name
53
+ */
54
+ readonly element: string;
55
+ /**
56
+ * Namespace
57
+ *
58
+ * Supports `"svg"` and `undefined` only.
59
+ * If it is `undefined`, the namespace is HTML.
60
+ */
61
+ readonly namespace?: 'svg';
62
+ /**
63
+ * Attributes
64
+ */
65
+ readonly attrs?: readonly {
66
+ /**
67
+ * Attribute name
68
+ */
69
+ readonly name: string;
70
+ /**
71
+ * If it omits this property, the attribute is resolved as a boolean.
72
+ */
73
+ readonly value?: string | {
74
+ readonly fromAttr: string;
75
+ };
76
+ }[];
77
+ /**
78
+ * To have attributes the defined element has.
79
+ */
80
+ readonly inheritAttrs?: boolean;
81
+ /**
82
+ * ARIA properties
83
+ */
84
+ readonly aria?: PretenderARIA;
79
85
  };
80
86
  /**
81
87
  * Pretender Node ARIA properties
82
88
  */
83
89
  export type PretenderARIA = {
84
- /**
85
- * Accessible name
86
- *
87
- * - If it is `true`, it assumes the element has any text on its accessible name.
88
- * - If it specifies `fromAttr` property, it assumes the accessible name refers to the value of the attribute.
89
- */
90
- name?:
91
- | boolean
92
- | {
93
- fromAttr: string;
94
- };
95
- };
96
- export type Rule<T extends RuleConfigValue, O = void> = RuleConfig<T, O> | T | boolean;
90
+ /**
91
+ * Accessible name
92
+ *
93
+ * - If it is `true`, it assumes the element has any text on its accessible name.
94
+ * - If it specifies `fromAttr` property, it assumes the accessible name refers to the value of the attribute.
95
+ */
96
+ readonly name?: boolean | {
97
+ readonly fromAttr: string;
98
+ };
99
+ };
100
+ export type Rule<T extends RuleConfigValue, O extends PlainData = undefined> = RuleConfig<T, O> | Readonly<T> | boolean;
97
101
  /**
98
102
  * @deprecated
99
103
  */
100
- export type RuleV2<T extends RuleConfigValue, O = void> = RuleConfigV2<T, O> | T | boolean;
101
- export type AnyRule = Rule<RuleConfigValue, unknown>;
104
+ export type RuleV2<T extends RuleConfigValue, O extends PlainData = undefined> = RuleConfigV2<T, O> | Readonly<T> | boolean;
105
+ export type AnyRule = Rule<RuleConfigValue, PlainData>;
102
106
  /**
103
107
  * @deprecated
104
108
  */
105
- export type AnyRuleV2 = RuleV2<RuleConfigValue, unknown>;
106
- export interface Rules {
107
- [ruleName: string]: AnyRule;
108
- }
109
- export type RuleConfig<T extends RuleConfigValue, O = void> = {
110
- severity?: Severity;
111
- value?: T;
112
- options?: O;
113
- reason?: string;
109
+ export type AnyRuleV2 = RuleV2<RuleConfigValue, PlainData>;
110
+ export type Rules = {
111
+ readonly [ruleName: string]: AnyRule;
112
+ };
113
+ export type RuleConfig<T extends RuleConfigValue, O extends PlainData = undefined> = {
114
+ readonly severity?: Severity;
115
+ readonly value?: Readonly<T>;
116
+ readonly options?: Readonly<O>;
117
+ readonly reason?: string;
114
118
  };
115
119
  /**
116
120
  * @deprecated
117
121
  */
118
- export type RuleConfigV2<T extends RuleConfigValue, O = void> = {
119
- severity?: Severity;
120
- value?: T;
121
- reason?: string;
122
- /**
123
- * Old property
124
- *
125
- * @deprecated
126
- * @see {this.options}
127
- */
128
- option?: O;
122
+ export type RuleConfigV2<T extends RuleConfigValue, O extends PlainData = undefined> = {
123
+ readonly severity?: Severity;
124
+ readonly value?: Readonly<T>;
125
+ readonly reason?: string;
126
+ /**
127
+ * Old property
128
+ *
129
+ * @deprecated
130
+ * @see {this.options}
131
+ */
132
+ readonly option?: Readonly<O>;
129
133
  };
130
134
  export type Severity = 'error' | 'warning' | 'info';
131
- export type RuleConfigValue = string | number | boolean | any[] | null;
132
- export interface NodeRule {
133
- selector?: string;
134
- regexSelector?: RegexSelector;
135
- categories?: string[];
136
- roles?: string[];
137
- obsolete?: boolean;
138
- rules?: Rules;
139
- }
140
- export interface ChildNodeRule {
141
- selector?: string;
142
- regexSelector?: RegexSelector;
143
- inheritance?: boolean;
144
- rules?: Rules;
145
- }
146
- export type Report<T extends RuleConfigValue, O = null> = Report1<T, O> | Report2 | (Report1<T, O> & Report2);
147
- export type Report1<T extends RuleConfigValue, O = null> = {
148
- message: string;
149
- scope: Scope<T, O>;
135
+ export type RuleConfigValue = PrimitiveScalar | readonly (PrimitiveScalar | Readonly<Record<string, any>>)[] | null;
136
+ export type NodeRule = {
137
+ readonly selector?: string;
138
+ readonly regexSelector?: RegexSelector;
139
+ readonly categories?: readonly string[];
140
+ readonly roles?: readonly string[];
141
+ readonly obsolete?: boolean;
142
+ readonly rules?: Rules;
143
+ };
144
+ export type ChildNodeRule = {
145
+ readonly selector?: string;
146
+ readonly regexSelector?: RegexSelector;
147
+ readonly inheritance?: boolean;
148
+ readonly rules?: Rules;
149
+ };
150
+ export type Report<T extends RuleConfigValue, O extends PlainData = undefined> = Report1<T, O> | Report2 | (Report1<T, O> & Report2);
151
+ export type Report1<T extends RuleConfigValue, O extends PlainData = undefined> = {
152
+ readonly message: string;
153
+ readonly scope: Scope<T, O>;
150
154
  };
151
155
  export type Report2 = {
152
- message: string;
153
- line: number;
154
- col: number;
155
- raw: string;
156
- };
157
- export type Scope<T extends RuleConfigValue, O = null> = {
158
- rule: RuleInfo<T, O>;
159
- startLine: number;
160
- startCol: number;
161
- raw: string;
162
- };
163
- export interface Violation {
164
- ruleId: string;
165
- severity: Severity;
166
- message: string;
167
- reason?: string;
168
- line: number;
169
- col: number;
170
- raw: string;
171
- }
172
- export interface RuleInfo<T extends RuleConfigValue, O = null> {
173
- disabled: boolean;
174
- severity: Severity;
175
- value: T;
176
- options: O;
177
- reason?: string;
178
- }
179
- export type GlobalRuleInfo<T extends RuleConfigValue, O = null> = RuleInfo<T, O> & {
180
- nodeRules: RuleInfo<T, O>[];
181
- childNodeRules: RuleInfo<T, O>[];
156
+ readonly message: string;
157
+ readonly line: number;
158
+ readonly col: number;
159
+ readonly raw: string;
160
+ };
161
+ export type Scope<T extends RuleConfigValue, O extends PlainData = undefined> = {
162
+ readonly rule: RuleInfo<T, O>;
163
+ readonly startLine: number;
164
+ readonly startCol: number;
165
+ readonly raw: string;
166
+ };
167
+ export type Violation = {
168
+ readonly ruleId: string;
169
+ readonly severity: Severity;
170
+ readonly message: string;
171
+ readonly reason?: string;
172
+ readonly line: number;
173
+ readonly col: number;
174
+ readonly raw: string;
175
+ };
176
+ export type RuleInfo<T extends RuleConfigValue, O extends PlainData = undefined> = {
177
+ readonly disabled: boolean;
178
+ readonly severity: Severity;
179
+ readonly value: Readonly<T>;
180
+ readonly options: Readonly<O>;
181
+ readonly reason?: string;
182
+ };
183
+ export type GlobalRuleInfo<T extends RuleConfigValue, O extends PlainData = undefined> = RuleInfo<T, O> & {
184
+ nodeRules: RuleInfo<T, O>[];
185
+ childNodeRules: RuleInfo<T, O>[];
182
186
  };
183
187
  export type Nullable<T> = T | null | undefined;
package/lib/utils.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { AnyRule, AnyRuleV2, RuleConfig, RuleConfigV2, RuleConfigValue } from './types';
1
+ import type { AnyRule, AnyRuleV2, PlainData, RuleConfig, RuleConfigV2, RuleConfigValue } from './types';
2
2
  /**
3
3
  * Return undefined if the template doesn't include the variable that is set as a property in data.
4
4
  * But return template string without changes if it doesn't have a variable.
@@ -6,14 +6,9 @@ import type { AnyRule, AnyRuleV2, RuleConfig, RuleConfigV2, RuleConfigValue } fr
6
6
  * @param template Mustache template string
7
7
  * @param data Captured string for replacement
8
8
  */
9
- export declare function provideValue(template: string, data: Record<string, string>): string | undefined;
10
- export declare function exchangeValueOnRule(
11
- rule: AnyRule | AnyRuleV2,
12
- data: Record<string, string>,
13
- ): AnyRule | undefined;
14
- export declare function cleanOptions(
15
- rule: RuleConfig<RuleConfigValue, unknown> | RuleConfigV2<RuleConfigValue, unknown>,
16
- ): RuleConfig<RuleConfigValue, unknown>;
9
+ export declare function provideValue(template: string, data: Readonly<Record<string, string>>): string | undefined;
10
+ export declare function exchangeValueOnRule(rule: AnyRule | AnyRuleV2, data: Readonly<Record<string, string>>): AnyRule | undefined;
11
+ export declare function cleanOptions(rule: RuleConfig<RuleConfigValue, PlainData> | RuleConfigV2<RuleConfigValue, PlainData>): RuleConfig<RuleConfigValue, PlainData>;
17
12
  export declare function isRuleConfigValue(v: any): v is RuleConfigValue;
18
13
  /**
19
14
  *
package/lib/utils.js CHANGED
@@ -40,9 +40,14 @@ function exchangeValueOnRule(rule, data) {
40
40
  }
41
41
  const options = extractOptions(result);
42
42
  if (options) {
43
+ const newOptions = exchangeOption(options, data);
43
44
  result = {
44
45
  ...result,
45
- options: exchangeOption(options, data),
46
+ ...(newOptions == null
47
+ ? undefined
48
+ : {
49
+ options: newOptions,
50
+ }),
46
51
  };
47
52
  }
48
53
  if (result.reason != null) {
@@ -126,7 +131,7 @@ function exchangeValue(rule, data) {
126
131
  }
127
132
  return val;
128
133
  })
129
- .filter(item => item !== undefined);
134
+ .filter((item) => item !== undefined);
130
135
  return ruleArray.length ? ruleArray : undefined;
131
136
  }
132
137
  return rule;
@@ -141,7 +146,7 @@ function exchangeOption(optionValue, data) {
141
146
  if (typeof optionValue === 'string') {
142
147
  return provideValue(optionValue, data);
143
148
  }
144
- if (Array.isArray(optionValue)) {
149
+ if (isArray(optionValue)) {
145
150
  return optionValue.map(v => exchangeOption(v, data));
146
151
  }
147
152
  const result = {};
@@ -150,3 +155,16 @@ function exchangeOption(optionValue, data) {
150
155
  });
151
156
  return result;
152
157
  }
158
+ /**
159
+ * Array.isArray for ReadonlyArray
160
+ *
161
+ * > Array.isArray type narrows to any[] for ReadonlyArray<T>
162
+ *
163
+ * @see https://github.com/microsoft/TypeScript/issues/17002
164
+ *
165
+ * @param value
166
+ * @returns
167
+ */
168
+ function isArray(value) {
169
+ return Array.isArray(value);
170
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markuplint/ml-config",
3
- "version": "3.4.0",
3
+ "version": "3.5.0",
4
4
  "description": "JSON Schema and TypeScript types of markuplint configure JSON",
5
5
  "repository": "git@github.com:markuplint/markuplint.git",
6
6
  "author": "Yusuke Hirao <yusukehirao@me.com>",
@@ -19,14 +19,15 @@
19
19
  "clean": "tsc --build --clean"
20
20
  },
21
21
  "devDependencies": {
22
- "@markuplint/ml-ast": "3.0.0",
22
+ "@markuplint/ml-ast": "3.1.0",
23
23
  "@types/mustache": "^4.2.2"
24
24
  },
25
25
  "dependencies": {
26
- "@markuplint/selector": "3.4.0",
26
+ "@markuplint/selector": "3.5.0",
27
27
  "deepmerge": "^4.2.2",
28
28
  "is-plain-object": "^5.0.0",
29
- "mustache": "^4.2.0"
29
+ "mustache": "^4.2.0",
30
+ "type-fest": "^3.6.1"
30
31
  },
31
- "gitHead": "a83e0f5f214a9bbcc0286b9e269074ddca6189e7"
32
+ "gitHead": "0c47b2c2722f6823a17f36edbab98486275f8ab4"
32
33
  }