@markuplint/ml-config 3.3.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,35 +1,43 @@
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
37
  /**
30
38
  * Target node selectors
31
39
  */
32
- selector: string;
40
+ readonly selector: string;
33
41
  /**
34
42
  * If it is a string, it is resolved as an element name.
35
43
  * An element has the same attributes as the pretended custom element
@@ -37,43 +45,43 @@ export type Pretender = {
37
45
  *
38
46
  * If it is an Object, It creates the element by that.
39
47
  */
40
- as: string | OriginalNode;
48
+ readonly as: string | OriginalNode;
41
49
  };
42
50
  export type OriginalNode = {
43
51
  /**
44
52
  * Element name
45
53
  */
46
- element: string;
54
+ readonly element: string;
47
55
  /**
48
56
  * Namespace
49
57
  *
50
58
  * Supports `"svg"` and `undefined` only.
51
59
  * If it is `undefined`, the namespace is HTML.
52
60
  */
53
- namespace?: 'svg';
61
+ readonly namespace?: 'svg';
54
62
  /**
55
63
  * Attributes
56
64
  */
57
- attrs?: {
65
+ readonly attrs?: readonly {
58
66
  /**
59
67
  * Attribute name
60
68
  */
61
- name: string;
69
+ readonly name: string;
62
70
  /**
63
71
  * If it omits this property, the attribute is resolved as a boolean.
64
72
  */
65
- value?: string | {
66
- fromAttr: string;
73
+ readonly value?: string | {
74
+ readonly fromAttr: string;
67
75
  };
68
76
  }[];
69
77
  /**
70
78
  * To have attributes the defined element has.
71
79
  */
72
- inheritAttrs?: boolean;
80
+ readonly inheritAttrs?: boolean;
73
81
  /**
74
82
  * ARIA properties
75
83
  */
76
- aria?: PretenderARIA;
84
+ readonly aria?: PretenderARIA;
77
85
  };
78
86
  /**
79
87
  * Pretender Node ARIA properties
@@ -85,94 +93,94 @@ export type PretenderARIA = {
85
93
  * - If it is `true`, it assumes the element has any text on its accessible name.
86
94
  * - If it specifies `fromAttr` property, it assumes the accessible name refers to the value of the attribute.
87
95
  */
88
- name?: boolean | {
89
- fromAttr: string;
96
+ readonly name?: boolean | {
97
+ readonly fromAttr: string;
90
98
  };
91
99
  };
92
- export type Rule<T extends RuleConfigValue, O = void> = RuleConfig<T, O> | T | boolean;
100
+ export type Rule<T extends RuleConfigValue, O extends PlainData = undefined> = RuleConfig<T, O> | Readonly<T> | boolean;
93
101
  /**
94
102
  * @deprecated
95
103
  */
96
- export type RuleV2<T extends RuleConfigValue, O = void> = RuleConfigV2<T, O> | T | boolean;
97
- 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>;
98
106
  /**
99
107
  * @deprecated
100
108
  */
101
- export type AnyRuleV2 = RuleV2<RuleConfigValue, unknown>;
102
- export interface Rules {
103
- [ruleName: string]: AnyRule;
104
- }
105
- export type RuleConfig<T extends RuleConfigValue, O = void> = {
106
- severity?: Severity;
107
- value?: T;
108
- options?: O;
109
- 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;
110
118
  };
111
119
  /**
112
120
  * @deprecated
113
121
  */
114
- export type RuleConfigV2<T extends RuleConfigValue, O = void> = {
115
- severity?: Severity;
116
- value?: T;
117
- reason?: string;
122
+ export type RuleConfigV2<T extends RuleConfigValue, O extends PlainData = undefined> = {
123
+ readonly severity?: Severity;
124
+ readonly value?: Readonly<T>;
125
+ readonly reason?: string;
118
126
  /**
119
127
  * Old property
120
128
  *
121
129
  * @deprecated
122
130
  * @see {this.options}
123
131
  */
124
- option?: O;
132
+ readonly option?: Readonly<O>;
125
133
  };
126
134
  export type Severity = 'error' | 'warning' | 'info';
127
- export type RuleConfigValue = string | number | boolean | any[] | null;
128
- export interface NodeRule {
129
- selector?: string;
130
- regexSelector?: RegexSelector;
131
- categories?: string[];
132
- roles?: string[];
133
- obsolete?: boolean;
134
- rules?: Rules;
135
- }
136
- export interface ChildNodeRule {
137
- selector?: string;
138
- regexSelector?: RegexSelector;
139
- inheritance?: boolean;
140
- rules?: Rules;
141
- }
142
- export type Report<T extends RuleConfigValue, O = null> = Report1<T, O> | Report2 | (Report1<T, O> & Report2);
143
- export type Report1<T extends RuleConfigValue, O = null> = {
144
- message: string;
145
- 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>;
146
154
  };
147
155
  export type Report2 = {
148
- message: string;
149
- line: number;
150
- col: number;
151
- raw: string;
152
- };
153
- export type Scope<T extends RuleConfigValue, O = null> = {
154
- rule: RuleInfo<T, O>;
155
- startLine: number;
156
- startCol: number;
157
- raw: string;
158
- };
159
- export interface Violation {
160
- ruleId: string;
161
- severity: Severity;
162
- message: string;
163
- reason?: string;
164
- line: number;
165
- col: number;
166
- raw: string;
167
- }
168
- export interface RuleInfo<T extends RuleConfigValue, O = null> {
169
- disabled: boolean;
170
- severity: Severity;
171
- value: T;
172
- options: O;
173
- reason?: string;
174
- }
175
- export type GlobalRuleInfo<T extends RuleConfigValue, O = null> = 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> & {
176
184
  nodeRules: RuleInfo<T, O>[];
177
185
  childNodeRules: RuleInfo<T, O>[];
178
186
  };
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,9 +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(rule: AnyRule | AnyRuleV2, data: Record<string, string>): AnyRule | undefined;
11
- export declare function cleanOptions(rule: RuleConfig<RuleConfigValue, unknown> | RuleConfigV2<RuleConfigValue, unknown>): 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>;
12
12
  export declare function isRuleConfigValue(v: any): v is RuleConfigValue;
13
13
  /**
14
14
  *
package/lib/utils.js CHANGED
@@ -12,8 +12,9 @@ const mustache_1 = tslib_1.__importDefault(require("mustache"));
12
12
  * @param data Captured string for replacement
13
13
  */
14
14
  function provideValue(template, data) {
15
+ var _a;
15
16
  const ast = mustache_1.default.parse(template);
16
- if (ast.length === 1 && ast[0][0] === 'text') {
17
+ if (ast.length === 1 && ((_a = ast[0]) === null || _a === void 0 ? void 0 : _a[0]) === 'text') {
17
18
  // It doesn't have a variable
18
19
  return template;
19
20
  }
@@ -39,9 +40,14 @@ function exchangeValueOnRule(rule, data) {
39
40
  }
40
41
  const options = extractOptions(result);
41
42
  if (options) {
43
+ const newOptions = exchangeOption(options, data);
42
44
  result = {
43
45
  ...result,
44
- options: exchangeOption(options, data),
46
+ ...(newOptions == null
47
+ ? undefined
48
+ : {
49
+ options: newOptions,
50
+ }),
45
51
  };
46
52
  }
47
53
  if (result.reason != null) {
@@ -125,7 +131,7 @@ function exchangeValue(rule, data) {
125
131
  }
126
132
  return val;
127
133
  })
128
- .filter(item => item !== undefined);
134
+ .filter((item) => item !== undefined);
129
135
  return ruleArray.length ? ruleArray : undefined;
130
136
  }
131
137
  return rule;
@@ -140,7 +146,7 @@ function exchangeOption(optionValue, data) {
140
146
  if (typeof optionValue === 'string') {
141
147
  return provideValue(optionValue, data);
142
148
  }
143
- if (Array.isArray(optionValue)) {
149
+ if (isArray(optionValue)) {
144
150
  return optionValue.map(v => exchangeOption(v, data));
145
151
  }
146
152
  const result = {};
@@ -149,3 +155,16 @@ function exchangeOption(optionValue, data) {
149
155
  });
150
156
  return result;
151
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.3.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.3.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": "791fb22a4df7acb985ced3808923fba0cd95c28a"
32
+ "gitHead": "0c47b2c2722f6823a17f36edbab98486275f8ab4"
32
33
  }