@terrazzo/parser 2.0.0-alpha.7 → 2.0.0-beta.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.
Files changed (53) hide show
  1. package/dist/index.d.ts +39 -6
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +578 -512
  4. package/dist/index.js.map +1 -1
  5. package/package.json +3 -3
  6. package/src/build/index.ts +0 -209
  7. package/src/config.ts +0 -304
  8. package/src/index.ts +0 -95
  9. package/src/lib/code-frame.ts +0 -177
  10. package/src/lib/momoa.ts +0 -10
  11. package/src/lib/resolver-utils.ts +0 -35
  12. package/src/lint/index.ts +0 -142
  13. package/src/lint/plugin-core/index.ts +0 -103
  14. package/src/lint/plugin-core/lib/docs.ts +0 -3
  15. package/src/lint/plugin-core/rules/a11y-min-contrast.ts +0 -91
  16. package/src/lint/plugin-core/rules/a11y-min-font-size.ts +0 -66
  17. package/src/lint/plugin-core/rules/colorspace.ts +0 -108
  18. package/src/lint/plugin-core/rules/consistent-naming.ts +0 -65
  19. package/src/lint/plugin-core/rules/descriptions.ts +0 -43
  20. package/src/lint/plugin-core/rules/duplicate-values.ts +0 -85
  21. package/src/lint/plugin-core/rules/max-gamut.ts +0 -144
  22. package/src/lint/plugin-core/rules/required-children.ts +0 -106
  23. package/src/lint/plugin-core/rules/required-modes.ts +0 -75
  24. package/src/lint/plugin-core/rules/required-type.ts +0 -28
  25. package/src/lint/plugin-core/rules/required-typography-properties.ts +0 -65
  26. package/src/lint/plugin-core/rules/valid-boolean.ts +0 -41
  27. package/src/lint/plugin-core/rules/valid-border.ts +0 -57
  28. package/src/lint/plugin-core/rules/valid-color.ts +0 -265
  29. package/src/lint/plugin-core/rules/valid-cubic-bezier.ts +0 -83
  30. package/src/lint/plugin-core/rules/valid-dimension.ts +0 -199
  31. package/src/lint/plugin-core/rules/valid-duration.ts +0 -123
  32. package/src/lint/plugin-core/rules/valid-font-family.ts +0 -68
  33. package/src/lint/plugin-core/rules/valid-font-weight.ts +0 -89
  34. package/src/lint/plugin-core/rules/valid-gradient.ts +0 -79
  35. package/src/lint/plugin-core/rules/valid-link.ts +0 -41
  36. package/src/lint/plugin-core/rules/valid-number.ts +0 -63
  37. package/src/lint/plugin-core/rules/valid-shadow.ts +0 -67
  38. package/src/lint/plugin-core/rules/valid-string.ts +0 -41
  39. package/src/lint/plugin-core/rules/valid-stroke-style.ts +0 -104
  40. package/src/lint/plugin-core/rules/valid-transition.ts +0 -61
  41. package/src/lint/plugin-core/rules/valid-typography.ts +0 -67
  42. package/src/logger.ts +0 -213
  43. package/src/parse/index.ts +0 -124
  44. package/src/parse/load.ts +0 -172
  45. package/src/parse/normalize.ts +0 -163
  46. package/src/parse/process.ts +0 -251
  47. package/src/parse/token.ts +0 -553
  48. package/src/resolver/create-synthetic-resolver.ts +0 -86
  49. package/src/resolver/index.ts +0 -7
  50. package/src/resolver/load.ts +0 -215
  51. package/src/resolver/normalize.ts +0 -133
  52. package/src/resolver/validate.ts +0 -375
  53. package/src/types.ts +0 -468
@@ -1,108 +0,0 @@
1
- import type { ColorValueNormalized } from '@terrazzo/token-tools';
2
- import wcmatch from 'wildcard-match';
3
- import type { LintRule } from '../../../types.js';
4
- import { docsLink } from '../lib/docs.js';
5
-
6
- export const COLORSPACE = 'core/colorspace';
7
-
8
- export interface RuleColorspaceOptions {
9
- colorSpace: ColorValueNormalized['colorSpace'];
10
- /** (optional) Token IDs to ignore. Supports globs (`*`). */
11
- ignore?: string[];
12
- }
13
-
14
- const ERROR_COLOR = 'COLOR';
15
- const ERROR_BORDER = 'BORDER';
16
- const ERROR_GRADIENT = 'GRADIENT';
17
- const ERROR_SHADOW = 'SHADOW';
18
-
19
- const rule: LintRule<
20
- typeof ERROR_COLOR | typeof ERROR_BORDER | typeof ERROR_GRADIENT | typeof ERROR_SHADOW,
21
- RuleColorspaceOptions
22
- > = {
23
- meta: {
24
- messages: {
25
- [ERROR_COLOR]: 'Color {{ id }} not in colorspace {{ colorSpace }}',
26
- [ERROR_BORDER]: 'Border {{ id }} not in colorspace {{ colorSpace }}',
27
- [ERROR_GRADIENT]: 'Gradient {{ id }} not in colorspace {{ colorSpace }}',
28
- [ERROR_SHADOW]: 'Shadow {{ id }} not in colorspace {{ colorSpace }}',
29
- },
30
- docs: {
31
- description: 'Enforce that all colors are in a specific colorspace.',
32
- url: docsLink(COLORSPACE),
33
- },
34
- },
35
- defaultOptions: { colorSpace: 'srgb' },
36
- create({ tokens, options, report }) {
37
- if (!options.colorSpace) {
38
- return;
39
- }
40
-
41
- const shouldIgnore = options.ignore ? wcmatch(options.ignore) : null;
42
-
43
- for (const t of Object.values(tokens)) {
44
- // skip ignored tokens
45
- if (shouldIgnore?.(t.id)) {
46
- continue;
47
- }
48
-
49
- // skip aliases
50
- if (t.aliasOf) {
51
- continue;
52
- }
53
-
54
- switch (t.$type) {
55
- case 'color': {
56
- if (t.$value.colorSpace !== options.colorSpace) {
57
- report({
58
- messageId: ERROR_COLOR,
59
- data: { id: t.id, colorSpace: options.colorSpace },
60
- node: t.source.node,
61
- filename: t.source.filename,
62
- });
63
- }
64
- break;
65
- }
66
- case 'border': {
67
- if (!t.partialAliasOf?.color && t.$value.color.colorSpace !== options.colorSpace) {
68
- report({
69
- messageId: ERROR_BORDER,
70
- data: { id: t.id, colorSpace: options.colorSpace },
71
- node: t.source.node,
72
- filename: t.source.filename,
73
- });
74
- }
75
- break;
76
- }
77
- case 'gradient': {
78
- for (let stopI = 0; stopI < t.$value.length; stopI++) {
79
- if (!t.partialAliasOf?.[stopI]?.color && t.$value[stopI]!.color.colorSpace !== options.colorSpace) {
80
- report({
81
- messageId: ERROR_GRADIENT,
82
- data: { id: t.id, colorSpace: options.colorSpace },
83
- node: t.source.node,
84
- filename: t.source.filename,
85
- });
86
- }
87
- }
88
- break;
89
- }
90
- case 'shadow': {
91
- for (let shadowI = 0; shadowI < t.$value.length; shadowI++) {
92
- if (!t.partialAliasOf?.[shadowI]?.color && t.$value[shadowI]!.color.colorSpace !== options.colorSpace) {
93
- report({
94
- messageId: ERROR_SHADOW,
95
- data: { id: t.id, colorSpace: options.colorSpace },
96
- node: t.source.node,
97
- filename: t.source.filename,
98
- });
99
- }
100
- }
101
- break;
102
- }
103
- }
104
- }
105
- },
106
- };
107
-
108
- export default rule;
@@ -1,65 +0,0 @@
1
- import { camelCase, kebabCase, pascalCase, snakeCase } from 'scule';
2
- import type { LintRule } from '../../../types.js';
3
- import { docsLink } from '../lib/docs.js';
4
-
5
- export const CONSISTENT_NAMING = 'core/consistent-naming';
6
- export const ERROR_WRONG_FORMAT = 'ERROR_WRONG_FORMAT';
7
-
8
- export interface RuleConsistentNamingOptions {
9
- /** Specify format, or custom naming validator */
10
- format:
11
- | 'kebab-case'
12
- | 'camelCase'
13
- | 'PascalCase'
14
- | 'snake_case'
15
- | 'SCREAMING_SNAKE_CASE'
16
- | ((tokenID: string) => boolean);
17
- /** Token IDs to ignore. Supports globs (`*`). */
18
- ignore?: string[];
19
- }
20
-
21
- const rule: LintRule<typeof ERROR_WRONG_FORMAT, RuleConsistentNamingOptions> = {
22
- meta: {
23
- messages: {
24
- [ERROR_WRONG_FORMAT]: '{{ id }} doesn’t match format {{ format }}',
25
- },
26
- docs: {
27
- description: 'Enforce consistent naming for tokens.',
28
- url: docsLink(CONSISTENT_NAMING),
29
- },
30
- },
31
- defaultOptions: { format: 'kebab-case' },
32
- create({ tokens, options, report }) {
33
- const basicFormatter = {
34
- 'kebab-case': kebabCase,
35
- camelCase,
36
- PascalCase: pascalCase,
37
- snake_case: snakeCase,
38
- SCREAMING_SNAKE_CASE: (name: string) => snakeCase(name).toLocaleUpperCase(),
39
- }[String(options.format)];
40
-
41
- for (const t of Object.values(tokens)) {
42
- if (basicFormatter) {
43
- const parts = t.id.split('.');
44
- if (!parts.every((part) => basicFormatter(part) === part)) {
45
- report({
46
- messageId: ERROR_WRONG_FORMAT,
47
- data: { id: t.id, format: options.format },
48
- node: t.source.node,
49
- });
50
- }
51
- } else if (typeof options.format === 'function') {
52
- const result = options.format(t.id);
53
- if (result) {
54
- report({
55
- messageId: ERROR_WRONG_FORMAT,
56
- data: { id: t.id, format: '(custom)' },
57
- node: t.source.node,
58
- });
59
- }
60
- }
61
- }
62
- },
63
- };
64
-
65
- export default rule;
@@ -1,43 +0,0 @@
1
- import wcmatch from 'wildcard-match';
2
- import type { LintRule } from '../../../types.js';
3
- import { docsLink } from '../lib/docs.js';
4
-
5
- export const DESCRIPTIONS = 'core/descriptions';
6
-
7
- export interface RuleDescriptionsOptions {
8
- /** Token IDs to ignore. Supports globs (`*`). */
9
- ignore?: string[];
10
- }
11
-
12
- const ERROR_MISSING_DESCRIPTION = 'MISSING_DESCRIPTION';
13
-
14
- const rule: LintRule<typeof ERROR_MISSING_DESCRIPTION, RuleDescriptionsOptions> = {
15
- meta: {
16
- messages: {
17
- [ERROR_MISSING_DESCRIPTION]: '{{ id }} missing description',
18
- },
19
- docs: {
20
- description: 'Enforce tokens have descriptions.',
21
- url: docsLink(DESCRIPTIONS),
22
- },
23
- },
24
- defaultOptions: {},
25
- create({ tokens, options, report }) {
26
- const shouldIgnore = options.ignore ? wcmatch(options.ignore) : null;
27
-
28
- for (const t of Object.values(tokens)) {
29
- if (shouldIgnore?.(t.id)) {
30
- continue;
31
- }
32
- if (!t.$description) {
33
- report({
34
- messageId: ERROR_MISSING_DESCRIPTION,
35
- data: { id: t.id },
36
- node: t.source.node,
37
- });
38
- }
39
- }
40
- },
41
- };
42
-
43
- export default rule;
@@ -1,85 +0,0 @@
1
- import { isAlias } from '@terrazzo/token-tools';
2
- import wcmatch from 'wildcard-match';
3
- import type { LintRule } from '../../../types.js';
4
- import { docsLink } from '../lib/docs.js';
5
-
6
- export const DUPLICATE_VALUES = 'core/duplicate-values';
7
-
8
- export interface RuleDuplicateValueOptions {
9
- /** Token IDs to ignore. Supports globs (`*`). */
10
- ignore?: string[];
11
- }
12
-
13
- const ERROR_DUPLICATE_VALUE = 'ERROR_DUPLICATE_VALUE';
14
-
15
- const rule: LintRule<typeof ERROR_DUPLICATE_VALUE, RuleDuplicateValueOptions> = {
16
- meta: {
17
- messages: {
18
- [ERROR_DUPLICATE_VALUE]: '{{ id }} declared a duplicate value',
19
- },
20
- docs: {
21
- description: 'Enforce tokens can’t redeclare the same value (excludes aliases).',
22
- url: docsLink(DUPLICATE_VALUES),
23
- },
24
- },
25
- defaultOptions: {},
26
- create({ report, tokens, options }) {
27
- const values: Record<string, Set<any>> = {};
28
-
29
- const shouldIgnore = options.ignore ? wcmatch(options.ignore) : null;
30
-
31
- for (const t of Object.values(tokens)) {
32
- // skip ignored tokens
33
- if (shouldIgnore?.(t.id)) {
34
- continue;
35
- }
36
-
37
- if (!values[t.$type]) {
38
- values[t.$type] = new Set();
39
- }
40
-
41
- // primitives: direct comparison is easy
42
- if (
43
- t.$type === 'boolean' ||
44
- t.$type === 'duration' ||
45
- t.$type === 'fontWeight' ||
46
- t.$type === 'link' ||
47
- t.$type === 'number' ||
48
- t.$type === 'string'
49
- ) {
50
- // skip aliases (note: $value will be resolved)
51
- if (typeof t.aliasOf === 'string' && isAlias(t.aliasOf)) {
52
- continue;
53
- }
54
-
55
- if (values[t.$type]?.has(t.$value)) {
56
- report({
57
- messageId: ERROR_DUPLICATE_VALUE,
58
- data: { id: t.id },
59
- node: t.source.node,
60
- filename: t.source.filename,
61
- });
62
- }
63
-
64
- values[t.$type]?.add(t.$value);
65
- } else {
66
- // everything else: use deepEqual
67
- for (const v of values[t.$type]!.values() ?? []) {
68
- // TODO: don’t JSON.stringify
69
- if (JSON.stringify(t.$value) === JSON.stringify(v)) {
70
- report({
71
- messageId: ERROR_DUPLICATE_VALUE,
72
- data: { id: t.id },
73
- node: t.source.node,
74
- filename: t.source.filename,
75
- });
76
- break;
77
- }
78
- }
79
- values[t.$type]!.add(t.$value);
80
- }
81
- }
82
- },
83
- };
84
-
85
- export default rule;
@@ -1,144 +0,0 @@
1
- import { type ColorValueNormalized, tokenToCulori } from '@terrazzo/token-tools';
2
- import { type Color, clampChroma } from 'culori';
3
- import wcmatch from 'wildcard-match';
4
- import type { LintRule } from '../../../types.js';
5
- import { docsLink } from '../lib/docs.js';
6
-
7
- export const MAX_GAMUT = 'core/max-gamut';
8
-
9
- export interface RuleMaxGamutOptions {
10
- /** Gamut to constrain color tokens to. */
11
- gamut: 'srgb' | 'p3' | 'rec2020';
12
- /** (optional) Token IDs to ignore. Supports globs (`*`). */
13
- ignore?: string[];
14
- }
15
-
16
- const TOLERANCE = 0.000001; // threshold above which it counts as an error (take rounding errors into account)
17
-
18
- /** is a Culori-parseable color within the specified gamut? */
19
- function isWithinGamut(color: ColorValueNormalized, gamut: RuleMaxGamutOptions['gamut']): boolean {
20
- const parsed = tokenToCulori(color);
21
- if (!parsed) {
22
- return false;
23
- }
24
- if (['rgb', 'hsl', 'hwb'].includes(parsed.mode)) {
25
- return true;
26
- }
27
- const clamped = clampChroma(parsed, parsed.mode, gamut === 'srgb' ? 'rgb' : gamut);
28
- return isWithinThreshold(parsed, clamped);
29
- }
30
-
31
- /** is Color A close enough to Color B? */
32
- function isWithinThreshold(a: Color, b: Color, tolerance = TOLERANCE) {
33
- for (const k in a) {
34
- if (k === 'mode' || k === 'alpha') {
35
- continue;
36
- }
37
- if (!(k in b)) {
38
- throw new Error(`Can’t compare ${a.mode} to ${b.mode}`);
39
- }
40
- if (Math.abs((a as any)[k] - (b as any)[k]) > tolerance) {
41
- return false;
42
- }
43
- }
44
- return true;
45
- }
46
-
47
- const ERROR_COLOR = 'COLOR';
48
- const ERROR_BORDER = 'BORDER';
49
- const ERROR_GRADIENT = 'GRADIENT';
50
- const ERROR_SHADOW = 'SHADOW';
51
-
52
- const rule: LintRule<
53
- typeof ERROR_COLOR | typeof ERROR_BORDER | typeof ERROR_GRADIENT | typeof ERROR_SHADOW,
54
- RuleMaxGamutOptions
55
- > = {
56
- meta: {
57
- messages: {
58
- [ERROR_COLOR]: 'Color {{ id }} is outside {{ gamut }} gamut',
59
- [ERROR_BORDER]: 'Border {{ id }} is outside {{ gamut }} gamut',
60
- [ERROR_GRADIENT]: 'Gradient {{ id }} is outside {{ gamut }} gamut',
61
- [ERROR_SHADOW]: 'Shadow {{ id }} is outside {{ gamut }} gamut',
62
- },
63
- docs: {
64
- description: 'Enforce colors are within the specified gamut.',
65
- url: docsLink(MAX_GAMUT),
66
- },
67
- },
68
- defaultOptions: { gamut: 'rec2020' },
69
- create({ tokens, options, report }) {
70
- if (!options?.gamut) {
71
- return;
72
- }
73
- if (options.gamut !== 'srgb' && options.gamut !== 'p3' && options.gamut !== 'rec2020') {
74
- throw new Error(`Unknown gamut "${options.gamut}". Options are "srgb", "p3", or "rec2020"`);
75
- }
76
-
77
- const shouldIgnore = options.ignore ? wcmatch(options.ignore) : null;
78
-
79
- for (const t of Object.values(tokens)) {
80
- // skip ignored tokens
81
- if (shouldIgnore?.(t.id)) {
82
- continue;
83
- }
84
-
85
- // skip aliases
86
- if (t.aliasOf) {
87
- continue;
88
- }
89
-
90
- switch (t.$type) {
91
- case 'color': {
92
- if (!isWithinGamut(t.$value, options.gamut)) {
93
- report({
94
- messageId: ERROR_COLOR,
95
- data: { id: t.id, gamut: options.gamut },
96
- node: t.source.node,
97
- filename: t.source.filename,
98
- });
99
- }
100
- break;
101
- }
102
- case 'border': {
103
- if (!t.partialAliasOf?.color && !isWithinGamut(t.$value.color, options.gamut)) {
104
- report({
105
- messageId: ERROR_BORDER,
106
- data: { id: t.id, gamut: options.gamut },
107
- node: t.source.node,
108
- filename: t.source.filename,
109
- });
110
- }
111
- break;
112
- }
113
- case 'gradient': {
114
- for (let stopI = 0; stopI < t.$value.length; stopI++) {
115
- if (!t.partialAliasOf?.[stopI]?.color && !isWithinGamut(t.$value[stopI]!.color, options.gamut)) {
116
- report({
117
- messageId: ERROR_GRADIENT,
118
- data: { id: t.id, gamut: options.gamut },
119
- node: t.source.node,
120
- filename: t.source.filename,
121
- });
122
- }
123
- }
124
- break;
125
- }
126
- case 'shadow': {
127
- for (let shadowI = 0; shadowI < t.$value.length; shadowI++) {
128
- if (!t.partialAliasOf?.[shadowI]?.color && !isWithinGamut(t.$value[shadowI]!.color, options.gamut)) {
129
- report({
130
- messageId: ERROR_SHADOW,
131
- data: { id: t.id, gamut: options.gamut },
132
- node: t.source.node,
133
- filename: t.source.filename,
134
- });
135
- }
136
- }
137
- break;
138
- }
139
- }
140
- }
141
- },
142
- };
143
-
144
- export default rule;
@@ -1,106 +0,0 @@
1
- import wcmatch from 'wildcard-match';
2
- import type { LintRule } from '../../../types.js';
3
- import { docsLink } from '../lib/docs.js';
4
-
5
- export const REQUIRED_CHILDREN = 'core/required-children';
6
-
7
- export interface RequiredChildrenMatch {
8
- /** Glob of tokens/groups to match */
9
- match: string[];
10
- /** Required token IDs to match (this only looks at the very last segment of a token ID!) */
11
- requiredTokens?: string[];
12
- /** Required groups to match (this only looks at the beginning/middle segments of a token ID!) */
13
- requiredGroups?: string[];
14
- }
15
-
16
- export interface RuleRequiredChildrenOptions {
17
- matches: RequiredChildrenMatch[];
18
- }
19
-
20
- export const ERROR_EMPTY_MATCH = 'EMPTY_MATCH';
21
- export const ERROR_MISSING_REQUIRED_TOKENS = 'MISSING_REQUIRED_TOKENS';
22
- export const ERROR_MISSING_REQUIRED_GROUP = 'MISSING_REQUIRED_GROUP';
23
-
24
- const rule: LintRule<
25
- typeof ERROR_EMPTY_MATCH | typeof ERROR_MISSING_REQUIRED_TOKENS | typeof ERROR_MISSING_REQUIRED_GROUP,
26
- RuleRequiredChildrenOptions
27
- > = {
28
- meta: {
29
- messages: {
30
- [ERROR_EMPTY_MATCH]: 'No tokens matched {{ matcher }}',
31
- [ERROR_MISSING_REQUIRED_TOKENS]: 'Match {{ index }}: some groups missing required token "{{ token }}"',
32
- [ERROR_MISSING_REQUIRED_GROUP]: 'Match {{ index }}: some tokens missing required group "{{ group }}"',
33
- },
34
- docs: {
35
- description: 'Enforce token groups have specific children, whether tokens and/or groups.',
36
- url: docsLink(REQUIRED_CHILDREN),
37
- },
38
- },
39
- defaultOptions: { matches: [] },
40
- create({ tokens, options, report }) {
41
- if (!options.matches?.length) {
42
- throw new Error('Invalid config. Missing `matches: […]`');
43
- }
44
-
45
- // note: in many other rules, the operation can be completed in one iteration through all tokens
46
- // in this rule, however, we have to scan all tokens every time per-match, because they may overlap
47
-
48
- for (let matchI = 0; matchI < options.matches.length; matchI++) {
49
- const { match, requiredTokens, requiredGroups } = options.matches[matchI]!;
50
-
51
- // validate
52
- if (!match.length) {
53
- throw new Error(`Match ${matchI}: must declare \`match: […]\``);
54
- }
55
- if (!requiredTokens?.length && !requiredGroups?.length) {
56
- throw new Error(`Match ${matchI}: must declare either \`requiredTokens: […]\` or \`requiredGroups: […]\``);
57
- }
58
-
59
- const matcher = wcmatch(match);
60
-
61
- const matchGroups: string[] = [];
62
- const matchTokens: string[] = [];
63
- let tokensMatched = false;
64
- for (const t of Object.values(tokens)) {
65
- if (!matcher(t.id)) {
66
- continue;
67
- }
68
- tokensMatched = true;
69
- const groups = t.id.split('.');
70
- matchTokens.push(groups.pop()!);
71
- matchGroups.push(...groups);
72
- }
73
-
74
- if (!tokensMatched) {
75
- report({
76
- messageId: ERROR_EMPTY_MATCH,
77
- data: { matcher: JSON.stringify(match) },
78
- });
79
- continue;
80
- }
81
-
82
- if (requiredTokens) {
83
- for (const id of requiredTokens) {
84
- if (!matchTokens.includes(id)) {
85
- report({
86
- messageId: ERROR_MISSING_REQUIRED_TOKENS,
87
- data: { index: matchI, token: id },
88
- });
89
- }
90
- }
91
- }
92
- if (requiredGroups) {
93
- for (const groupName of requiredGroups) {
94
- if (!matchGroups.includes(groupName)) {
95
- report({
96
- messageId: ERROR_MISSING_REQUIRED_GROUP,
97
- data: { index: matchI, group: groupName },
98
- });
99
- }
100
- }
101
- }
102
- }
103
- },
104
- };
105
-
106
- export default rule;
@@ -1,75 +0,0 @@
1
- import wcmatch from 'wildcard-match';
2
- import type { LintRule } from '../../../types.js';
3
- import { docsLink } from '../lib/docs.js';
4
-
5
- export const REQUIRED_MODES = 'core/required-modes';
6
-
7
- export type RequiredModesMatch = {
8
- /** Glob of tokens/groups to match */
9
- match: string[];
10
- /** Required modes */
11
- modes: string[];
12
- };
13
-
14
- export interface RuleRequiredModesOptions {
15
- matches: RequiredModesMatch[];
16
- }
17
-
18
- const rule: LintRule<never, RuleRequiredModesOptions> = {
19
- meta: {
20
- docs: {
21
- description: 'Enforce certain tokens have specific modes.',
22
- url: docsLink(REQUIRED_MODES),
23
- },
24
- },
25
- defaultOptions: { matches: [] },
26
- create({ tokens, options, report }) {
27
- if (!options?.matches?.length) {
28
- throw new Error('Invalid config. Missing `matches: […]`');
29
- }
30
-
31
- // note: in many other rules, the operation can be completed in one iteration through all tokens
32
- // in this rule, however, we have to scan all tokens every time per-match, because they may overlap
33
- for (let matchI = 0; matchI < options.matches.length; matchI++) {
34
- const { match, modes } = options.matches[matchI]!;
35
-
36
- // validate
37
- if (!match.length) {
38
- throw new Error(`Match ${matchI}: must declare \`match: […]\``);
39
- }
40
- if (!modes?.length) {
41
- throw new Error(`Match ${matchI}: must declare \`modes: […]\``);
42
- }
43
-
44
- const matcher = wcmatch(match);
45
-
46
- let tokensMatched = false;
47
- for (const t of Object.values(tokens)) {
48
- if (!matcher(t.id)) {
49
- continue;
50
- }
51
- tokensMatched = true;
52
-
53
- for (const mode of modes) {
54
- if (!t.mode?.[mode]) {
55
- report({
56
- message: `Token ${t.id}: missing required mode "${mode}"`,
57
- node: t.source.node,
58
- filename: t.source.filename,
59
- });
60
- }
61
- }
62
-
63
- if (!tokensMatched) {
64
- report({
65
- message: `Match "${matchI}": no tokens matched ${JSON.stringify(match)}`,
66
- node: t.source.node,
67
- filename: t.source.filename,
68
- });
69
- }
70
- }
71
- }
72
- },
73
- };
74
-
75
- export default rule;
@@ -1,28 +0,0 @@
1
- import type { LintRule } from '../../../types.js';
2
- import { docsLink } from '../lib/docs.js';
3
-
4
- export const REQUIRED_TYPE = 'core/required-type';
5
-
6
- export const ERROR = 'ERROR';
7
-
8
- const rule: LintRule<typeof ERROR> = {
9
- meta: {
10
- messages: {
11
- [ERROR]: 'Token missing $type.',
12
- },
13
- docs: {
14
- description: 'Requiring every token to have $type, even aliases, simplifies computation.',
15
- url: docsLink(REQUIRED_TYPE),
16
- },
17
- },
18
- defaultOptions: {},
19
- create({ tokens, report }) {
20
- for (const t of Object.values(tokens)) {
21
- if (!t.originalValue?.$type) {
22
- report({ messageId: ERROR, node: t.source.node, filename: t.source.filename });
23
- }
24
- }
25
- },
26
- };
27
-
28
- export default rule;