@markuplint/types 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.
@@ -1,5 +1,5 @@
1
1
  import type { CustomSyntaxCheck, UnmatchedResult } from './types';
2
2
  export declare function checkMultiTypes(
3
3
  value: string,
4
- checks: CustomSyntaxCheck[],
5
- ): UnmatchedResult | import('./types').MatchedResult;
4
+ checks: readonly CustomSyntaxCheck[],
5
+ ): import('./types').MatchedResult | UnmatchedResult;
@@ -4,6 +4,7 @@ exports.checkMultiTypes = void 0;
4
4
  const debug_1 = require("./debug");
5
5
  const match_result_1 = require("./match-result");
6
6
  function checkMultiTypes(value, checks) {
7
+ var _a, _b, _c;
7
8
  let unmatched;
8
9
  for (const check of checks) {
9
10
  const res = check(value);
@@ -14,15 +15,15 @@ function checkMultiTypes(value, checks) {
14
15
  res._fn = check.name;
15
16
  // @ts-ignore
16
17
  (0, debug_1.log)('%s(%d) vs %s(%d)', unmatched === null || unmatched === void 0 ? void 0 : unmatched._fn, unmatched === null || unmatched === void 0 ? void 0 : unmatched.passCount, res._fn, res.passCount);
17
- const passedA = (unmatched === null || unmatched === void 0 ? void 0 : unmatched.passCount) || 0;
18
- const passedB = res.passCount || 0;
19
- const offsetA = (unmatched === null || unmatched === void 0 ? void 0 : unmatched.offset) || 0;
18
+ const passedA = (_a = unmatched === null || unmatched === void 0 ? void 0 : unmatched.passCount) !== null && _a !== void 0 ? _a : 0;
19
+ const passedB = (_b = res.passCount) !== null && _b !== void 0 ? _b : 0;
20
+ const offsetA = (_c = unmatched === null || unmatched === void 0 ? void 0 : unmatched.offset) !== null && _c !== void 0 ? _c : 0;
20
21
  const offsetB = res.offset;
21
22
  if (passedA < passedB || (passedA === passedB && offsetA <= offsetB)) {
22
23
  unmatched = res;
23
24
  }
24
25
  }
25
- const result = unmatched || (0, match_result_1.matched)();
26
+ const result = unmatched !== null && unmatched !== void 0 ? unmatched : (0, match_result_1.matched)();
26
27
  (0, debug_1.log)('%d checks result: %O', checks.length, result);
27
28
  return result;
28
29
  }
package/lib/check.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import type { Type, Result, List, CustomSyntax, CustomCssSyntax, Enum, KeywordDefinedType, Number } from './types';
2
- export declare function check(value: string, type: Type, ref?: string, cache?: boolean): Result;
3
- export declare function isKeyword(type: Type): type is KeywordDefinedType;
4
- export declare function isList(type: Type): type is List;
5
- export declare function isEnum(type: Type): type is Enum;
6
- export declare function isNumber(type: Type): type is Number;
2
+ import type { ReadonlyDeep } from 'type-fest';
3
+ export declare function check(value: string, type: ReadonlyDeep<Type>, ref?: string, cache?: boolean): Result;
4
+ export declare function isKeyword(type: ReadonlyDeep<Type>): type is ReadonlyDeep<KeywordDefinedType>;
5
+ export declare function isList(type: ReadonlyDeep<Type>): type is ReadonlyDeep<List>;
6
+ export declare function isEnum(type: ReadonlyDeep<Type>): type is ReadonlyDeep<Enum>;
7
+ export declare function isNumber(type: ReadonlyDeep<Type>): type is ReadonlyDeep<Number>;
7
8
  export declare function isCSSSyntax(type: CustomSyntax | CustomCssSyntax): type is CustomCssSyntax;
8
9
  export declare function isCustomSyntax(type: CustomSyntax | CustomCssSyntax): type is CustomSyntax;
package/lib/css-syntax.js CHANGED
@@ -9,6 +9,7 @@ const MIMIC_TAG_L = 'mimiccases---';
9
9
  const MIMIC_TAG_R = '---mimiccases';
10
10
  const MIMIC_LENGTH = (MIMIC_TAG_L + MIMIC_TAG_R).length;
11
11
  function cssSyntaxMatch(value, type) {
12
+ var _a;
12
13
  (0, debug_1.log)('Search CSS Syntax: "%s"', type);
13
14
  const origin = value;
14
15
  let defName;
@@ -24,7 +25,7 @@ function cssSyntaxMatch(value, type) {
24
25
  }
25
26
  else {
26
27
  defName = type.syntax.apply;
27
- ebnf = type.syntax.ebnf || null;
28
+ ebnf = (_a = type.syntax.ebnf) !== null && _a !== void 0 ? _a : null;
28
29
  if (ebnf) {
29
30
  // Work in progress
30
31
  return (0, match_result_1.matched)();
@@ -69,7 +70,7 @@ function cssSyntaxMatch(value, type) {
69
70
  throw new Error('MARKUPLINT_TYPE_NO_EXIST');
70
71
  }
71
72
  const refParam = isProp ? 'Property' : 'Type';
72
- ref = ref || `https://csstree.github.io/docs/syntax/#${refParam}:${name}`;
73
+ ref = ref !== null && ref !== void 0 ? ref : `https://csstree.github.io/docs/syntax/#${refParam}:${name}`;
73
74
  // eslint-disable-next-line no-console
74
75
  const _w = console.warn;
75
76
  if (debug_1.log.enabled) {
@@ -126,7 +127,17 @@ function detectName(def) {
126
127
  name,
127
128
  };
128
129
  }
129
- function eachMimicCases(key, obj) {
130
+ /**
131
+ *
132
+ * @param key
133
+ * @param obj
134
+ * @modifies obj
135
+ * @returns
136
+ */
137
+ function eachMimicCases(key,
138
+ // Mutable
139
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
140
+ obj) {
130
141
  const value = obj[key];
131
142
  if (!value) {
132
143
  return;
package/lib/defs.js CHANGED
@@ -115,7 +115,7 @@ exports.types = {
115
115
  reason: 'unexpected-space',
116
116
  });
117
117
  }
118
- if (!tokens.length) {
118
+ if (tokens.length === 0) {
119
119
  return (0, match_result_1.unmatched)(value, 'empty-token');
120
120
  }
121
121
  return (0, match_result_1.matched)();
@@ -431,7 +431,7 @@ exports.types = {
431
431
  if (!height) {
432
432
  return sep.unmatched({ reason: 'unexpected-token' });
433
433
  }
434
- if (tail && tail[0]) {
434
+ if (tail[0]) {
435
435
  return tail[0].unmatched({ reason: 'extra-token' });
436
436
  }
437
437
  if (!(0, primitive_1.isUint)(width.value)) {
package/lib/enum.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import type { Result } from './types';
2
2
  import type { Enum } from './types.schema';
3
- export declare function checkEnum(value: string, type: Enum, ref?: string): Result;
3
+ import type { ReadonlyDeep } from 'type-fest';
4
+ export declare function checkEnum(value: string, type: ReadonlyDeep<Enum>, ref?: string): Result;
package/lib/enum.js CHANGED
@@ -9,7 +9,7 @@ function checkEnum(value, type, ref) {
9
9
  if (!disallowToSurroundBySpaces) {
10
10
  value = value.trim();
11
11
  }
12
- let values = type.enum;
12
+ let values = type.enum.slice();
13
13
  if (caseInsensitive) {
14
14
  value = value.toLowerCase();
15
15
  values = type.enum.map(v => v.toLowerCase());
@@ -1,6 +1,6 @@
1
1
  type NullableString = string | null | undefined;
2
2
  export declare function getCandidate(
3
3
  value: NullableString,
4
- ...candidates: (NullableString | NullableString[])[]
4
+ ...candidates: readonly (NullableString | readonly NullableString[])[]
5
5
  ): string | undefined;
6
6
  export {};
@@ -33,6 +33,7 @@ function checkKeywordType(value, type, cache = true) {
33
33
  }
34
34
  exports.checkKeywordType = checkKeywordType;
35
35
  function _checkKeywordType(value, type) {
36
+ var _a, _b;
36
37
  const def = defs_1.types[type];
37
38
  if (!def) {
38
39
  (0, debug_1.log)('The "%s" type is not defined in custom type identifier markuplint specified', type);
@@ -51,8 +52,8 @@ function _checkKeywordType(value, type) {
51
52
  if (!matches.matched) {
52
53
  return {
53
54
  ...matches,
54
- ref: matches.ref || def.ref,
55
- expects: matches.expects || def.expects,
55
+ ref: (_a = matches.ref) !== null && _a !== void 0 ? _a : def.ref,
56
+ expects: (_b = matches.expects) !== null && _b !== void 0 ? _b : def.expects,
56
57
  };
57
58
  }
58
59
  return matches;
package/lib/list.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import type { Result } from './types';
2
2
  import type { List } from './types.schema';
3
- export declare function checkList(value: string, type: List, ref?: string, cache?: boolean): Result;
3
+ import type { ReadonlyDeep } from 'type-fest';
4
+ export declare function checkList(value: string, type: ReadonlyDeep<List>, ref?: string, cache?: boolean): Result;
package/lib/list.js CHANGED
@@ -5,6 +5,7 @@ const check_1 = require("./check");
5
5
  const match_result_1 = require("./match-result");
6
6
  const token_1 = require("./token");
7
7
  function checkList(value, type, ref, cache = true) {
8
+ var _a;
8
9
  const tokens = new token_1.TokenCollection(value, type);
9
10
  const matches = tokens.check({ ref });
10
11
  if (!matches.matched) {
@@ -17,7 +18,7 @@ function checkList(value, type, ref, cache = true) {
17
18
  const { offset, line, column } = token_1.Token.shiftLocation(token, res.offset);
18
19
  return {
19
20
  ...res,
20
- partName: res.partName || 'the content of the list',
21
+ partName: (_a = res.partName) !== null && _a !== void 0 ? _a : 'the content of the list',
21
22
  offset,
22
23
  line,
23
24
  column,
@@ -8,15 +8,15 @@ import type {
8
8
  export declare function matches(
9
9
  checker: FormattedPrimitiveTypeCheck,
10
10
  options?: UnmatchedResultOptions & {
11
- ref?: string;
12
- reason?: UnmatchedResultReason;
11
+ readonly ref?: string;
12
+ readonly reason?: UnmatchedResultReason;
13
13
  },
14
- ): (value: string) => UnmatchedResult | MatchedResult;
14
+ ): (value: string) => MatchedResult | UnmatchedResult;
15
15
  export declare function matched(): MatchedResult;
16
16
  export declare function unmatched(
17
17
  value: string,
18
18
  reason?: UnmatchedResultReason,
19
19
  options?: UnmatchedResultOptions & {
20
- ref?: string;
20
+ readonly ref?: string;
21
21
  },
22
22
  ): UnmatchedResult;
@@ -18,10 +18,11 @@ function matched() {
18
18
  }
19
19
  exports.matched = matched;
20
20
  function unmatched(value, reason, options) {
21
+ var _a;
21
22
  return {
22
23
  ...options,
23
24
  matched: false,
24
- ref: (options === null || options === void 0 ? void 0 : options.ref) || null,
25
+ ref: (_a = options === null || options === void 0 ? void 0 : options.ref) !== null && _a !== void 0 ? _a : null,
25
26
  raw: value,
26
27
  offset: 0,
27
28
  length: value.length,
package/lib/number.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import type { Result } from './types';
2
2
  import type { Number } from './types.schema';
3
- export declare function checkNumber(value: string, type: Number, ref?: string): Result;
3
+ export declare function checkNumber(value: string, type: Readonly<Number>, ref?: string): Result;
@@ -4,4 +4,8 @@
4
4
  * @param units
5
5
  * @param numberType
6
6
  */
7
- export declare function isQuantity(value: string, units: string[], numberType?: 'int' | 'uint' | 'float'): boolean;
7
+ export declare function isQuantity(
8
+ value: string,
9
+ units: readonly string[],
10
+ numberType?: 'int' | 'uint' | 'float',
11
+ ): boolean;
@@ -6,6 +6,6 @@
6
6
  export declare function isUint(
7
7
  value: string,
8
8
  options?: {
9
- gt?: number;
9
+ readonly gt?: number;
10
10
  },
11
11
  ): boolean;
@@ -1,19 +1,23 @@
1
+ import type { TokenValue } from './types';
1
2
  import type { UnmatchedResult } from '..';
2
3
  import type { Expect, Result, List } from '../types';
4
+ import type { ReadonlyDeep } from 'type-fest';
3
5
  import { Token } from './token';
4
6
  type TokenCollectionOptions = Partial<
5
7
  Omit<List, 'token'> & {
6
8
  specificSeparator: string | string[];
7
9
  }
8
10
  >;
9
- export type TokenEachCheck = (head: Token | null, tail: TokenCollection) => Result | void;
11
+ export type TokenEachCheck = (head: Readonly<Token> | null, tail: ReadonlyDeep<TokenCollection>) => Result | void;
10
12
  export declare class TokenCollection extends Array<Token> {
11
13
  static fromPatterns(
12
- value: Token | string,
13
- patterns: RegExp[],
14
- typeOptions?: Omit<TokenCollectionOptions, 'specificSeparator'> & {
15
- repeat?: boolean;
16
- },
14
+ value: Readonly<Token> | string,
15
+ patterns: readonly Readonly<RegExp>[],
16
+ typeOptions?: ReadonlyDeep<
17
+ Omit<TokenCollectionOptions, 'specificSeparator'> & {
18
+ repeat?: boolean;
19
+ }
20
+ >,
17
21
  ): TokenCollection;
18
22
  static get [Symbol.species](): ArrayConstructor;
19
23
  readonly allowEmpty: NonNullable<List['allowEmpty']>;
@@ -23,27 +27,29 @@ export declare class TokenCollection extends Array<Token> {
23
27
  readonly ordered: NonNullable<List['ordered']>;
24
28
  readonly separator: NonNullable<List['separator']>;
25
29
  readonly unique: NonNullable<List['unique']>;
26
- constructor(value?: string, typeOptions?: TokenCollectionOptions);
30
+ constructor(value?: string, typeOptions?: ReadonlyDeep<TokenCollectionOptions>);
27
31
  constructor(value?: number);
28
32
  get value(): string;
29
33
  check(options?: {
30
34
  expects?: Expect[];
31
35
  ref?: string;
32
36
  cache?: boolean;
33
- }): UnmatchedResult | import('..').MatchedResult;
37
+ }): import('..').MatchedResult | UnmatchedResult;
34
38
  chunk(split: number): TokenCollection[];
35
- compareTokens(callback: (prev: Token, current: Token) => Token | null | void): Token | null | undefined;
39
+ compareTokens(
40
+ callback: (prev: Readonly<Token>, current: Readonly<Token>) => Readonly<Token> | null | void,
41
+ ): Readonly<Token> | null | undefined;
36
42
  divide(position: number): readonly [TokenCollection, TokenCollection];
37
- eachCheck(...callbacks: TokenEachCheck[]): Result;
38
- filter(callback: (value: Token, index: number, array: Token[]) => boolean): TokenCollection;
39
- getConsecutiveToken(tokenType: number): Token | null;
43
+ eachCheck(...callbacks: readonly TokenEachCheck[]): Result;
44
+ filter(callback: Parameters<Array<Token>['filter']>[0]): TokenCollection;
45
+ getConsecutiveToken(tokenType: number): Readonly<Token> | null;
40
46
  getDuplicated(): Token | null;
41
47
  getIdentTokens(): TokenCollection;
42
48
  /**
43
49
  *
44
50
  * @param value The token value or the token type or its list
45
51
  */
46
- has(value: string | RegExp | number | (string | RegExp | number)[]): boolean;
52
+ has(value: TokenValue): boolean;
47
53
  headAndTail(): {
48
54
  head: Token | null;
49
55
  tail: TokenCollection;
@@ -52,7 +58,7 @@ export declare class TokenCollection extends Array<Token> {
52
58
  *
53
59
  * @param value The token value or the token type or its list
54
60
  */
55
- search(value: string | RegExp | number | (string | RegExp | number)[]): Token | null;
61
+ search(value: TokenValue): Token | null;
56
62
  takeTurns(
57
63
  tokenNumbers: ReadonlyArray<number>,
58
64
  lastTokenNumber?: number,
@@ -5,12 +5,13 @@ const match_result_1 = require("../match-result");
5
5
  const token_1 = require("./token");
6
6
  class TokenCollection extends Array {
7
7
  static fromPatterns(value, patterns, typeOptions) {
8
- const origin = typeof value === 'string' ? value : value.origin;
8
+ var _c;
9
+ const originalValue = typeof value === 'string' ? value : value.originalValue;
9
10
  let strings = typeof value === 'string' ? value : value.value;
10
11
  let cumulativeOffset = typeof value === 'string' ? 0 : value.offset;
11
12
  const tokens = [];
12
13
  function addToken(tokenValue) {
13
- const token = new token_1.Token(tokenValue, cumulativeOffset, origin);
14
+ const token = new token_1.Token(tokenValue, cumulativeOffset, originalValue);
14
15
  tokens.push(token);
15
16
  strings = strings.slice(tokenValue.length);
16
17
  cumulativeOffset += tokenValue.length;
@@ -31,7 +32,7 @@ class TokenCollection extends Array {
31
32
  value = strings.slice(res.index + res[0].length);
32
33
  }
33
34
  else {
34
- value = res[0] || '';
35
+ value = (_c = res[0]) !== null && _c !== void 0 ? _c : '';
35
36
  }
36
37
  }
37
38
  const token = addToken(value);
@@ -51,14 +52,15 @@ class TokenCollection extends Array {
51
52
  return Array;
52
53
  }
53
54
  constructor(value, typeOptions) {
55
+ var _c, _d, _e, _f, _g, _h;
54
56
  super();
55
- this.disallowToSurroundBySpaces = (typeOptions === null || typeOptions === void 0 ? void 0 : typeOptions.disallowToSurroundBySpaces) || false;
56
- this.allowEmpty = (typeOptions === null || typeOptions === void 0 ? void 0 : typeOptions.allowEmpty) || true;
57
- this.ordered = (typeOptions === null || typeOptions === void 0 ? void 0 : typeOptions.ordered) || false;
58
- this.unique = (typeOptions === null || typeOptions === void 0 ? void 0 : typeOptions.unique) || false;
59
- this.caseInsensitive = (typeOptions === null || typeOptions === void 0 ? void 0 : typeOptions.caseInsensitive) || true;
57
+ this.disallowToSurroundBySpaces = (_c = typeOptions === null || typeOptions === void 0 ? void 0 : typeOptions.disallowToSurroundBySpaces) !== null && _c !== void 0 ? _c : false;
58
+ this.allowEmpty = (_d = typeOptions === null || typeOptions === void 0 ? void 0 : typeOptions.allowEmpty) !== null && _d !== void 0 ? _d : true;
59
+ this.ordered = (_e = typeOptions === null || typeOptions === void 0 ? void 0 : typeOptions.ordered) !== null && _e !== void 0 ? _e : false;
60
+ this.unique = (_f = typeOptions === null || typeOptions === void 0 ? void 0 : typeOptions.unique) !== null && _f !== void 0 ? _f : false;
61
+ this.caseInsensitive = (_g = typeOptions === null || typeOptions === void 0 ? void 0 : typeOptions.caseInsensitive) !== null && _g !== void 0 ? _g : true;
60
62
  this.number = typeOptions === null || typeOptions === void 0 ? void 0 : typeOptions.number;
61
- this.separator = (typeOptions === null || typeOptions === void 0 ? void 0 : typeOptions.separator) || 'space';
63
+ this.separator = (_h = typeOptions === null || typeOptions === void 0 ? void 0 : typeOptions.separator) !== null && _h !== void 0 ? _h : 'space';
62
64
  if (typeof value === 'number') {
63
65
  this.length = value;
64
66
  return;
@@ -70,12 +72,12 @@ class TokenCollection extends Array {
70
72
  if (this.separator === 'comma') {
71
73
  separators.push(',');
72
74
  }
73
- if (typeOptions === null || typeOptions === void 0 ? void 0 : typeOptions.specificSeparator) {
74
- if (Array.isArray(typeOptions.specificSeparator)) {
75
- separators.push(...typeOptions.specificSeparator);
75
+ if ((typeOptions === null || typeOptions === void 0 ? void 0 : typeOptions.specificSeparator) != null) {
76
+ if (typeof typeOptions.specificSeparator === 'string') {
77
+ separators.push(typeOptions.specificSeparator);
76
78
  }
77
79
  else {
78
- separators.push(typeOptions.specificSeparator);
80
+ separators.push(...typeOptions.specificSeparator);
79
81
  }
80
82
  }
81
83
  const chars = value.split('');
@@ -191,7 +193,7 @@ class TokenCollection extends Array {
191
193
  chunk(split) {
192
194
  const chunks = [];
193
195
  const tokens = this.slice();
194
- while (tokens.length) {
196
+ while (tokens.length > 0) {
195
197
  const chunkTokens = tokens.splice(0, split);
196
198
  const chunk = TokenCollection._new(chunkTokens, this);
197
199
  chunks.push(chunk);
@@ -222,6 +224,7 @@ class TokenCollection extends Array {
222
224
  return [a, b];
223
225
  }
224
226
  eachCheck(...callbacks) {
227
+ var _c, _d;
225
228
  let headAndTail = this.headAndTail();
226
229
  let head = headAndTail.head;
227
230
  let tail = headAndTail.tail;
@@ -234,10 +237,10 @@ class TokenCollection extends Array {
234
237
  wait--;
235
238
  const result = callback.call(this, head, tail);
236
239
  if (result && !result.matched) {
237
- passCount += result.passCount || 0;
240
+ passCount += (_c = result.passCount) !== null && _c !== void 0 ? _c : 0;
238
241
  if (prev && result.offset === 0 && result.length === 0) {
239
242
  const { offset, line, column } = token_1.Token.shiftLocation(prev, cumulativeOffset);
240
- firstUnmatched = firstUnmatched || {
243
+ firstUnmatched = firstUnmatched !== null && firstUnmatched !== void 0 ? firstUnmatched : {
241
244
  ...result,
242
245
  offset,
243
246
  line,
@@ -245,7 +248,7 @@ class TokenCollection extends Array {
245
248
  };
246
249
  }
247
250
  if (!prev && this.value.length === 0) {
248
- firstUnmatched = firstUnmatched || {
251
+ firstUnmatched = firstUnmatched !== null && firstUnmatched !== void 0 ? firstUnmatched : {
249
252
  ...result,
250
253
  reason: 'empty-token',
251
254
  expects: undefined,
@@ -262,7 +265,7 @@ class TokenCollection extends Array {
262
265
  }
263
266
  }
264
267
  }
265
- firstUnmatched = firstUnmatched || result;
268
+ firstUnmatched = firstUnmatched !== null && firstUnmatched !== void 0 ? firstUnmatched : result;
266
269
  }
267
270
  else if (result && !firstUnmatched && result.matched) {
268
271
  return result;
@@ -272,7 +275,7 @@ class TokenCollection extends Array {
272
275
  passCount += 4 * wait;
273
276
  }
274
277
  }
275
- cumulativeOffset = (head === null || head === void 0 ? void 0 : head.length) || 0;
278
+ cumulativeOffset = (_d = head === null || head === void 0 ? void 0 : head.length) !== null && _d !== void 0 ? _d : 0;
276
279
  headAndTail = tail.headAndTail();
277
280
  prev = head;
278
281
  head = headAndTail.head;
@@ -296,7 +299,7 @@ class TokenCollection extends Array {
296
299
  return current;
297
300
  }
298
301
  });
299
- return resultToken || null;
302
+ return resultToken !== null && resultToken !== void 0 ? resultToken : null;
300
303
  }
301
304
  getDuplicated() {
302
305
  const aList = this.slice();
@@ -333,7 +336,7 @@ class TokenCollection extends Array {
333
336
  const copy = this.slice();
334
337
  const head = copy.shift();
335
338
  if (!head) {
336
- return { head: head || null, tail: TokenCollection._new([], this) };
339
+ return { head: head !== null && head !== void 0 ? head : null, tail: TokenCollection._new([], this) };
337
340
  }
338
341
  const tail = TokenCollection._new(copy, this);
339
342
  return { head, tail };
@@ -1,6 +1,6 @@
1
+ import type { TokenValue } from './types';
1
2
  import type { UnmatchedResult, UnmatchedResultOptions, UnmatchedResultReason } from '../types';
2
3
  export declare class Token {
3
- #private;
4
4
  /**
5
5
  * @see https://github.com/csstree/csstree/blob/master/lib/tokenizer/types.js
6
6
  */
@@ -20,9 +20,9 @@ export declare class Token {
20
20
  static readonly whitespace: ReadonlyArray<string>;
21
21
  static getCol(value: string, offset: number): number;
22
22
  static getLine(value: string, offset: number): number;
23
- static getType(value: string, separators?: string[]): 1 | 13 | 18;
23
+ static getType(value: string, separators?: readonly string[]): 1 | 13 | 18;
24
24
  static shiftLocation(
25
- token: Token,
25
+ token: Readonly<Token>,
26
26
  offset: number,
27
27
  ): {
28
28
  offset: number;
@@ -30,21 +30,22 @@ export declare class Token {
30
30
  column: number;
31
31
  };
32
32
  readonly offset: number;
33
+ readonly originalValue: string;
33
34
  readonly type: number;
34
35
  readonly value: string;
35
- constructor(value: string, offset: number, originalValue: string, separators?: string[]);
36
+ constructor(value: string, offset: number, originalValue: string, separators?: readonly string[]);
36
37
  get length(): number;
37
- get origin(): string;
38
+ clone(): Token;
38
39
  /**
39
40
  *
40
41
  * @param value The token value or the token type or its list
41
42
  */
42
- includes(value: string | RegExp | number | (string | RegExp | number)[], caseInsensitive?: boolean): boolean;
43
+ includes(value: TokenValue, caseInsensitive?: boolean): boolean;
43
44
  /**
44
45
  *
45
46
  * @param value The token value or the token type or its list
46
47
  */
47
- match(value: string | RegExp | number | (string | RegExp | number)[], caseInsensitive?: boolean): boolean;
48
+ match(value: TokenValue, caseInsensitive?: boolean): boolean;
48
49
  toJSON(): {
49
50
  type: number;
50
51
  value: string;
@@ -53,8 +54,8 @@ export declare class Token {
53
54
  toNumber(): number;
54
55
  unmatched(
55
56
  options?: UnmatchedResultOptions & {
56
- ref?: string;
57
- reason?: UnmatchedResultReason;
57
+ readonly ref?: string;
58
+ readonly reason?: UnmatchedResultReason;
58
59
  },
59
60
  ): UnmatchedResult;
60
61
  }
@@ -1,8 +1,6 @@
1
1
  "use strict";
2
- var _Token_originalValue;
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
3
  exports.Token = void 0;
5
- const tslib_1 = require("tslib");
6
4
  class Token {
7
5
  static getCol(value, offset) {
8
6
  var _a;
@@ -29,22 +27,21 @@ class Token {
29
27
  const shifted = token.offset + offset;
30
28
  return {
31
29
  offset: shifted,
32
- line: Token.getLine(tslib_1.__classPrivateFieldGet(token, _Token_originalValue, "f"), shifted),
33
- column: Token.getCol(tslib_1.__classPrivateFieldGet(token, _Token_originalValue, "f"), shifted),
30
+ line: Token.getLine(token.originalValue, shifted),
31
+ column: Token.getCol(token.originalValue, shifted),
34
32
  };
35
33
  }
36
34
  constructor(value, offset, originalValue, separators) {
37
- _Token_originalValue.set(this, void 0);
38
35
  this.type = Token.getType(value, separators);
39
36
  this.value = value;
40
37
  this.offset = offset;
41
- tslib_1.__classPrivateFieldSet(this, _Token_originalValue, originalValue, "f");
38
+ this.originalValue = originalValue;
42
39
  }
43
40
  get length() {
44
41
  return this.value.length;
45
42
  }
46
- get origin() {
47
- return tslib_1.__classPrivateFieldGet(this, _Token_originalValue, "f");
43
+ clone() {
44
+ return new Token(this.value, this.offset, this.originalValue);
48
45
  }
49
46
  /**
50
47
  *
@@ -57,7 +54,7 @@ class Token {
57
54
  if (typeof value === 'string') {
58
55
  const a = caseInsensitive ? this.value.toLowerCase() : this.value;
59
56
  const b = caseInsensitive ? value.toLowerCase() : value;
60
- return a.indexOf(b) !== -1;
57
+ return a.includes(b);
61
58
  }
62
59
  if (value instanceof RegExp) {
63
60
  const pattern = new RegExp(value, caseInsensitive ? 'i' : '');
@@ -92,25 +89,24 @@ class Token {
92
89
  };
93
90
  }
94
91
  toNumber() {
95
- return parseFloat(this.value);
92
+ const num = parseFloat(this.value);
93
+ return isNaN(num) ? 0 : num;
96
94
  }
97
95
  unmatched(options) {
98
- var _a;
96
+ var _a, _b;
99
97
  return {
100
98
  ...options,
101
99
  matched: false,
102
- ref: (options === null || options === void 0 ? void 0 : options.ref) || null,
100
+ ref: (_a = options === null || options === void 0 ? void 0 : options.ref) !== null && _a !== void 0 ? _a : null,
103
101
  raw: this.value,
104
102
  offset: this.offset,
105
103
  length: this.value.length,
106
- line: Token.getLine(tslib_1.__classPrivateFieldGet(this, _Token_originalValue, "f"), this.offset),
107
- column: Token.getCol(tslib_1.__classPrivateFieldGet(this, _Token_originalValue, "f"), this.offset),
108
- reason: (_a = options === null || options === void 0 ? void 0 : options.reason) !== null && _a !== void 0 ? _a : 'syntax-error',
104
+ line: Token.getLine(this.originalValue, this.offset),
105
+ column: Token.getCol(this.originalValue, this.offset),
106
+ reason: (_b = options === null || options === void 0 ? void 0 : options.reason) !== null && _b !== void 0 ? _b : 'syntax-error',
109
107
  };
110
108
  }
111
109
  }
112
- exports.Token = Token;
113
- _Token_originalValue = new WeakMap();
114
110
  /**
115
111
  * @see https://github.com/csstree/csstree/blob/master/lib/tokenizer/types.js
116
112
  */
@@ -128,3 +124,4 @@ Token.WhiteSpace = 13;
128
124
  * @see https://infra.spec.whatwg.org/#ascii-whitespace
129
125
  */
130
126
  Token.whitespace = ['\u0009', '\u000A', '\u000C', '\u000D', '\u0020'];
127
+ exports.Token = Token;
@@ -0,0 +1,3 @@
1
+ export type TokenValueScalar = string | Readonly<RegExp> | number;
2
+ export type TokenValueArray = readonly TokenValue[];
3
+ export type TokenValue = TokenValueScalar | TokenValueArray;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/lib/types.d.ts CHANGED
@@ -2,21 +2,21 @@ import type { cssSyntaxMatch } from './css-syntax';
2
2
  export type { Type, List, Enum, CssSyntax, KeywordDefinedType, Number } from './types.schema';
3
3
  export type Result = UnmatchedResult | MatchedResult;
4
4
  export type UnmatchedResult = {
5
- matched: false;
6
- ref: string | null;
7
- raw: string;
8
- length: number;
9
- offset: number;
10
- line: number;
11
- column: number;
12
- reason: UnmatchedResultReason;
13
- passCount?: number;
5
+ readonly matched: false;
6
+ readonly ref: string | null;
7
+ readonly raw: string;
8
+ readonly length: number;
9
+ readonly offset: number;
10
+ readonly line: number;
11
+ readonly column: number;
12
+ readonly reason: UnmatchedResultReason;
13
+ readonly passCount?: number;
14
14
  } & UnmatchedResultOptions;
15
15
  export type UnmatchedResultOptions = {
16
- partName?: string;
17
- expects?: Expect[];
18
- extra?: Expect;
19
- candidate?: string;
16
+ readonly partName?: string;
17
+ readonly expects?: readonly Expect[];
18
+ readonly extra?: Expect;
19
+ readonly candidate?: string;
20
20
  };
21
21
  export type UnmatchedResultReason =
22
22
  | 'syntax-error'
@@ -37,65 +37,65 @@ export type UnmatchedResultReason =
37
37
  | 'must-be-percent-encoded'
38
38
  | 'must-be-serialized'
39
39
  | {
40
- type: 'out-of-range';
41
- gt?: number;
42
- gte?: number;
43
- lt?: number;
44
- lte?: number;
40
+ readonly type: 'out-of-range';
41
+ readonly gt?: number;
42
+ readonly gte?: number;
43
+ readonly lt?: number;
44
+ readonly lte?: number;
45
45
  }
46
46
  | {
47
- type: 'out-of-range-length-char';
48
- gte: number;
49
- lte?: number;
47
+ readonly type: 'out-of-range-length-char';
48
+ readonly gte: number;
49
+ readonly lte?: number;
50
50
  }
51
51
  | {
52
- type: 'out-of-range-length-digit';
53
- gte: number;
54
- lte?: number;
52
+ readonly type: 'out-of-range-length-digit';
53
+ readonly gte: number;
54
+ readonly lte?: number;
55
55
  };
56
56
  export type MatchedResult = {
57
- matched: true;
57
+ readonly matched: true;
58
58
  };
59
59
  export type Expect = {
60
- type: 'const' | 'format' | 'syntax' | 'regexp' | 'common';
61
- value: string;
60
+ readonly type: 'const' | 'format' | 'syntax' | 'regexp' | 'common';
61
+ readonly value: string;
62
62
  };
63
63
  export type FormattedPrimitiveTypeCheck = (value: string) => boolean;
64
64
  export type FormattedPrimitiveTypeCreator<O = never> = (options?: O) => FormattedPrimitiveTypeCheck;
65
65
  export type Defs = Record<string, CustomCssSyntax | CustomSyntax>;
66
66
  export type CustomSyntax = {
67
- ref: string;
68
- expects?: Expect[];
69
- is: CustomSyntaxCheck;
67
+ readonly ref: string;
68
+ readonly expects?: readonly Expect[];
69
+ readonly is: CustomSyntaxCheck;
70
70
  };
71
71
  export type CustomSyntaxCheck = (value: string) => Result;
72
72
  export type CustomSyntaxChecker<O = {}> = (options?: O) => CustomSyntaxCheck;
73
73
  export type CustomCssSyntax = {
74
- ref: string;
75
- caseSensitive?: boolean;
76
- expects?: Expect[];
77
- syntax: {
78
- apply: `<${string}>`;
79
- def: Record<string, string | CssSyntaxTokenizer>;
74
+ readonly ref: string;
75
+ readonly caseSensitive?: boolean;
76
+ readonly expects?: readonly Expect[];
77
+ readonly syntax: {
78
+ readonly apply: `<${string}>`;
79
+ readonly def: Readonly<Record<string, string | CssSyntaxTokenizer>>;
80
80
  /**
81
81
  * @deprecated
82
82
  */
83
- ebnf?: Record<string, string | string[]>;
83
+ readonly ebnf?: Readonly<Record<string, string | readonly string[]>>;
84
84
  /**
85
85
  * @deprecated
86
86
  */
87
- properties?: Record<string, string>;
87
+ readonly properties?: Readonly<Record<string, string>>;
88
88
  };
89
89
  };
90
90
  export type CSSSyntaxToken = {
91
- type: number;
92
- value: string;
93
- index: number;
94
- balance: number;
95
- node?: any;
91
+ readonly type: number;
92
+ readonly value: string;
93
+ readonly index: number;
94
+ readonly balance: number;
95
+ readonly node?: any;
96
96
  };
97
97
  export type CssSyntaxTokenizer = (
98
- token: CSSSyntaxToken | null,
98
+ token: Readonly<CSSSyntaxToken> | null,
99
99
  getNextToken: GetNextToken,
100
100
  match: typeof cssSyntaxMatch,
101
101
  ) => number;
@@ -209,7 +209,7 @@ const checkAutoComplete = () => value => {
209
209
  acLog('[Unmatched ("%s")] Unexpected token: "%s"', value, contactableFiledToken.value);
210
210
  return contactableFiledToken.unmatched({
211
211
  reason: 'unexpected-token',
212
- expects: contactableFieldNames.slice().map(token => ({
212
+ expects: contactableFieldNames.map(token => ({
213
213
  type: 'const',
214
214
  value: token,
215
215
  })),
@@ -285,7 +285,7 @@ const checkAutoComplete = () => value => {
285
285
  const [prefix, namedGroupStr] = head.value.split('-');
286
286
  const candidatePrefix = (0, get_candidate_1.getCandidate)(prefix, 'section');
287
287
  if (candidatePrefix) {
288
- candidate = `${candidatePrefix}-${namedGroupStr || ''}`;
288
+ candidate = `${candidatePrefix}-${namedGroupStr !== null && namedGroupStr !== void 0 ? namedGroupStr : ''}`;
289
289
  }
290
290
  }
291
291
  else if (!hasPartOfAddress) {
@@ -299,13 +299,13 @@ const checkAutoComplete = () => value => {
299
299
  candidate = (0, get_candidate_1.getCandidate)(head.value, partOfAddress, autofillFieldNames, contactingTokens, contactableFieldNames);
300
300
  }
301
301
  else if (!hasContactingToken) {
302
- expects.push(...contactingTokens.slice().map(token => ({
302
+ expects.push(...contactingTokens.map(token => ({
303
303
  type: 'const',
304
304
  value: token,
305
305
  })));
306
306
  candidate = (0, get_candidate_1.getCandidate)(head.value, autofillFieldNames, contactingTokens, contactableFieldNames);
307
307
  }
308
- candidate = candidate || (0, get_candidate_1.getCandidate)(head.value, autofillFieldNames);
308
+ candidate = candidate !== null && candidate !== void 0 ? candidate : (0, get_candidate_1.getCandidate)(head.value, autofillFieldNames);
309
309
  if (candidate) {
310
310
  acLog('[Unmatched ("%s")] Unexpected token: "%s", Do you mean "%s"? ', value, head.value, candidate);
311
311
  }
@@ -95,7 +95,7 @@ exports.datetimeTokenCheck = {
95
95
  * > then the day can be 29, as if the year was a leap year.
96
96
  */
97
97
  date(date) {
98
- var _a;
98
+ var _a, _b, _c;
99
99
  (0, debug_1.log)('Parsing Datetime Date: "%s"', date === null || date === void 0 ? void 0 : date.value);
100
100
  if (!date || !date.value) {
101
101
  return (0, match_result_1.unmatched)('', 'missing-token', {
@@ -118,10 +118,10 @@ exports.datetimeTokenCheck = {
118
118
  });
119
119
  }
120
120
  // Set the leap year if _year is null
121
- const year = this._year || 2000;
121
+ const year = (_a = this._year) !== null && _a !== void 0 ? _a : 2000;
122
122
  const isLeap = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
123
- const month = this._month || 1;
124
- const dayOfMonth = ((_a = daysOfMonth[month]) !== null && _a !== void 0 ? _a : 0) + (month === 2 && isLeap ? 1 : 0);
123
+ const month = (_b = this._month) !== null && _b !== void 0 ? _b : 1;
124
+ const dayOfMonth = ((_c = daysOfMonth[month]) !== null && _c !== void 0 ? _c : 0) + (month === 2 && isLeap ? 1 : 0);
125
125
  const _date = date.toNumber();
126
126
  (0, debug_1.log)('Temporary Year and Month: %s, %s', this._year, this._month);
127
127
  (0, debug_1.log)('Computed Year, Month, and Day of Month: %s, %s, %s', year, month, dayOfMonth);
@@ -265,6 +265,7 @@ exports.datetimeTokenCheck = {
265
265
  * > Two ASCII digits, representing the week week, in the range 1 ≤ week ≤ maxweek, where maxweek is the week number of the last day of week-year year
266
266
  */
267
267
  week(week) {
268
+ var _a;
268
269
  (0, debug_1.log)('Parsing Datetime Week: "%s"', week === null || week === void 0 ? void 0 : week.value);
269
270
  if (!week || !week.value) {
270
271
  return (0, match_result_1.unmatched)('', 'missing-token', {
@@ -286,7 +287,7 @@ exports.datetimeTokenCheck = {
286
287
  partName: 'week',
287
288
  });
288
289
  }
289
- const maxweek = getMaxWeekNum(this._year || 0);
290
+ const maxweek = getMaxWeekNum((_a = this._year) !== null && _a !== void 0 ? _a : 0);
290
291
  const _week = week.toNumber();
291
292
  if (!(1 <= _week && _week <= maxweek)) {
292
293
  return week.unmatched({
@@ -495,9 +496,10 @@ const daysOfMonth = [
495
496
  ];
496
497
  function getMaxWeekNum(year) {
497
498
  let date = 31;
498
- while (date) {
499
+ while (date > 0) {
499
500
  const d = new Date(Date.UTC(year, 11, date, 0, 0, 0, 0));
500
- d.setDate(d.getDate() + 4 - (d.getDay() || 7));
501
+ const day = d.getDay();
502
+ d.setDate(d.getDate() + 4 - (day > 0 ? day : 7));
501
503
  const yearStart = new Date(d.getFullYear(), 0, 1);
502
504
  const weekNo = Math.ceil(((d.valueOf() - yearStart.valueOf()) / 86400000 + 1) / 7);
503
505
  if (weekNo !== 1) {
@@ -84,6 +84,7 @@ const checkDurationISO8601LikeString = () => function checkDurationISO8601LikeSt
84
84
  });
85
85
  }
86
86
  }, time => {
87
+ var _a, _b, _c;
87
88
  if (!time) {
88
89
  return (0, match_result_1.unmatched)('', 'missing-token', {
89
90
  expects: [
@@ -104,7 +105,7 @@ const checkDurationISO8601LikeString = () => function checkDurationISO8601LikeSt
104
105
  ]);
105
106
  (0, debug_1.log)('Time part: "%s" => %O', timeTokens.value, timeTokens);
106
107
  const [h, m, s] = timeTokens;
107
- if (((h === null || h === void 0 ? void 0 : h.value) || '') + ((m === null || m === void 0 ? void 0 : m.value) || '') + ((s === null || s === void 0 ? void 0 : s.value) || '') === '') {
108
+ if (((_a = h === null || h === void 0 ? void 0 : h.value) !== null && _a !== void 0 ? _a : '') + ((_b = m === null || m === void 0 ? void 0 : m.value) !== null && _b !== void 0 ? _b : '') + ((_c = s === null || s === void 0 ? void 0 : s.value) !== null && _c !== void 0 ? _c : '') === '') {
108
109
  return (0, match_result_1.unmatched)('', 'missing-token', {
109
110
  expects: [
110
111
  { type: 'common', value: 'hour' },
@@ -115,7 +116,7 @@ const checkDurationISO8601LikeString = () => function checkDurationISO8601LikeSt
115
116
  }
116
117
  const specified = new Set();
117
118
  for (const t of timeTokens) {
118
- if (!t || !t.value) {
119
+ if (!t.value) {
119
120
  continue;
120
121
  }
121
122
  if (specified.has('S')) {
@@ -95,8 +95,8 @@ const checkNormalizedLocalDateAndTimeString = () => function checkNormalizedLoca
95
95
  (0, debug_1.log)('Failed: %O', res);
96
96
  return res;
97
97
  }
98
- const _second = second.toNumber() || 0;
99
- const _fp = fp.toNumber() || 0;
98
+ const _second = second.toNumber();
99
+ const _fp = fp.toNumber();
100
100
  (0, debug_1.log)('Omitting the seconds component: "%s.%s" => %d, %d', second === null || second === void 0 ? void 0 : second.value, fp === null || fp === void 0 ? void 0 : fp.value, _second, _fp);
101
101
  if (second.value && _second === 0 && (!fp.value || (fp.value && _fp === 0))) {
102
102
  const res = second.unmatched({
@@ -4,4 +4,4 @@ import type { CustomSyntaxChecker } from '../../types';
4
4
  * @see https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#time-zones
5
5
  */
6
6
  export declare const checkTimeZoneOffsetString: CustomSyntaxChecker;
7
- export declare function parseTimeZone(zone: string | Token): import('../../types').Result;
7
+ export declare function parseTimeZone(zone: string | Readonly<Token>): import('../../types').Result;
@@ -27,7 +27,7 @@ const checkMIMEType = options => value => {
27
27
  if (value.toLowerCase() === mimeType.essence) {
28
28
  return (0, match_result_1.matched)();
29
29
  }
30
- if (!withoutParameters && mimeType.parameters.size) {
30
+ if (!withoutParameters && mimeType.parameters.size > 0) {
31
31
  return (0, match_result_1.matched)();
32
32
  }
33
33
  const extraToken = value.slice(mimeType.essence.length);
@@ -13,7 +13,7 @@ exports.isItempropName = void 0;
13
13
  */
14
14
  const isItempropName = () => {
15
15
  return value => {
16
- return value.indexOf(':') === -1 && value.indexOf('.') === -1 && value.indexOf(' ') === -1;
16
+ return value.includes(':') && value.includes('.') && value.includes(' ');
17
17
  };
18
18
  };
19
19
  exports.isItempropName = isItempropName;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markuplint/types",
3
- "version": "3.3.0",
3
+ "version": "3.5.0",
4
4
  "description": "Type declaration and value checker",
5
5
  "repository": "git@github.com:markuplint/markuplint.git",
6
6
  "author": "Yusuke Hirao <yusukehirao@me.com>",
@@ -30,7 +30,8 @@
30
30
  "@types/bcp-47": "1",
31
31
  "@types/css-tree": "^2.0.1",
32
32
  "@types/debug": "^4.1.7",
33
- "@types/whatwg-mimetype": "2"
33
+ "@types/whatwg-mimetype": "2",
34
+ "type-fest": "^3.6.1"
34
35
  },
35
- "gitHead": "a83e0f5f214a9bbcc0286b9e269074ddca6189e7"
36
+ "gitHead": "715dd53d3b1064a9bcf616c1533921cad9e3b187"
36
37
  }