@terrazzo/parser 0.10.3 → 0.10.4

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.
package/dist/index.js CHANGED
@@ -36,10 +36,7 @@ function getMarkerLines(loc, source, opts = {}) {
36
36
  const sourceLength = source[lineNumber - 1].length;
37
37
  markerLines[lineNumber] = [startColumn, sourceLength - startColumn + 1];
38
38
  } else if (i === lineDiff) markerLines[lineNumber] = [0, endColumn];
39
- else {
40
- const sourceLength = source[lineNumber - i].length;
41
- markerLines[lineNumber] = [0, sourceLength];
42
- }
39
+ else markerLines[lineNumber] = [0, source[lineNumber - i].length];
43
40
  }
44
41
  else if (startColumn === endColumn) if (startColumn) markerLines[startLine] = [startColumn, 0];
45
42
  else markerLines[startLine] = true;
@@ -61,8 +58,7 @@ function codeFrameColumns(rawLines, loc, opts = {}) {
61
58
  const numberMaxWidth = String(end).length;
62
59
  let frame = rawLines.split(NEWLINE, end).slice(start, end).map((line, index) => {
63
60
  const number = start + 1 + index;
64
- const paddedNumber = ` ${number}`.slice(-numberMaxWidth);
65
- const gutter = ` ${paddedNumber} |`;
61
+ const gutter = ` ${` ${number}`.slice(-numberMaxWidth)} |`;
66
62
  const hasMarker = markerLines[number];
67
63
  const lastMarkerLine = !markerLines[number + 1];
68
64
  if (hasMarker) {
@@ -457,7 +453,7 @@ const rule$7 = {
457
453
  if (shouldIgnore?.(t.id)) continue;
458
454
  if (t.aliasOf) continue;
459
455
  switch (t.$type) {
460
- case "color": {
456
+ case "color":
461
457
  if (t.$value.colorSpace !== options.colorSpace) report({
462
458
  messageId: ERROR_COLOR$1,
463
459
  data: {
@@ -467,8 +463,7 @@ const rule$7 = {
467
463
  node: t.source.node
468
464
  });
469
465
  break;
470
- }
471
- case "border": {
466
+ case "border":
472
467
  if (!t.partialAliasOf?.color && t.$value.color.colorSpace !== options.colorSpace) report({
473
468
  messageId: ERROR_BORDER$1,
474
469
  data: {
@@ -478,8 +473,7 @@ const rule$7 = {
478
473
  node: t.source.node
479
474
  });
480
475
  break;
481
- }
482
- case "gradient": {
476
+ case "gradient":
483
477
  for (let stopI = 0; stopI < t.$value.length; stopI++) if (!t.partialAliasOf?.[stopI]?.color && t.$value[stopI].color.colorSpace !== options.colorSpace) report({
484
478
  messageId: ERROR_GRADIENT$1,
485
479
  data: {
@@ -489,8 +483,7 @@ const rule$7 = {
489
483
  node: t.source.node
490
484
  });
491
485
  break;
492
- }
493
- case "shadow": {
486
+ case "shadow":
494
487
  for (let shadowI = 0; shadowI < t.$value.length; shadowI++) if (!t.partialAliasOf?.[shadowI]?.color && t.$value[shadowI].color.colorSpace !== options.colorSpace) report({
495
488
  messageId: ERROR_SHADOW$1,
496
489
  data: {
@@ -500,7 +493,6 @@ const rule$7 = {
500
493
  node: t.source.node
501
494
  });
502
495
  break;
503
- }
504
496
  }
505
497
  }
506
498
  }
@@ -517,7 +509,7 @@ const STR_SPLITTERS = [
517
509
  "."
518
510
  ];
519
511
  function isUppercase(char = "") {
520
- if (NUMBER_CHAR_RE.test(char)) return void 0;
512
+ if (NUMBER_CHAR_RE.test(char)) return;
521
513
  return char !== char.toLowerCase();
522
514
  }
523
515
  function splitByCase(str, separators) {
@@ -599,8 +591,7 @@ const rule$6 = {
599
591
  SCREAMING_SNAKE_CASE: (name) => snakeCase(name).toLocaleUpperCase()
600
592
  }[String(options.format)];
601
593
  for (const t of Object.values(tokens)) if (basicFormatter) {
602
- const parts = t.id.split(".");
603
- if (!parts.every((part) => basicFormatter(part) === part)) report({
594
+ if (!t.id.split(".").every((part) => basicFormatter(part) === part)) report({
604
595
  messageId: ERROR_WRONG_FORMAT,
605
596
  data: {
606
597
  id: t.id,
@@ -609,8 +600,7 @@ const rule$6 = {
609
600
  node: t.source.node
610
601
  });
611
602
  } else if (typeof options.format === "function") {
612
- const result = options.format(t.id);
613
- if (result) report({
603
+ if (options.format(t.id)) report({
614
604
  messageId: ERROR_WRONG_FORMAT,
615
605
  data: {
616
606
  id: t.id,
@@ -744,7 +734,7 @@ const rule$3 = {
744
734
  if (shouldIgnore?.(t.id)) continue;
745
735
  if (t.aliasOf) continue;
746
736
  switch (t.$type) {
747
- case "color": {
737
+ case "color":
748
738
  if (!isWithinGamut(t.$value, options.gamut)) report({
749
739
  messageId: ERROR_COLOR,
750
740
  data: {
@@ -754,8 +744,7 @@ const rule$3 = {
754
744
  node: t.source.node
755
745
  });
756
746
  break;
757
- }
758
- case "border": {
747
+ case "border":
759
748
  if (!t.partialAliasOf?.color && !isWithinGamut(t.$value.color, options.gamut)) report({
760
749
  messageId: ERROR_BORDER,
761
750
  data: {
@@ -765,8 +754,7 @@ const rule$3 = {
765
754
  node: t.source.node
766
755
  });
767
756
  break;
768
- }
769
- case "gradient": {
757
+ case "gradient":
770
758
  for (let stopI = 0; stopI < t.$value.length; stopI++) if (!t.partialAliasOf?.[stopI]?.color && !isWithinGamut(t.$value[stopI].color, options.gamut)) report({
771
759
  messageId: ERROR_GRADIENT,
772
760
  data: {
@@ -776,8 +764,7 @@ const rule$3 = {
776
764
  node: t.source.node
777
765
  });
778
766
  break;
779
- }
780
- case "shadow": {
767
+ case "shadow":
781
768
  for (let shadowI = 0; shadowI < t.$value.length; shadowI++) if (!t.partialAliasOf?.[shadowI]?.color && !isWithinGamut(t.$value[shadowI].color, options.gamut)) report({
782
769
  messageId: ERROR_SHADOW,
783
770
  data: {
@@ -787,7 +774,6 @@ const rule$3 = {
787
774
  node: t.source.node
788
775
  });
789
776
  break;
790
- }
791
777
  }
792
778
  }
793
779
  }
@@ -1214,8 +1200,7 @@ async function lintRunner({ tokens, filename, config = {}, src, logger }) {
1214
1200
  "boolean"
1215
1201
  ].includes(typeof v) ? String(v) : JSON.stringify(v);
1216
1202
  message = message.replace(/{{[^}]+}}/g, (inner) => {
1217
- const key = inner.substring(2, inner.length - 2).trim();
1218
- return key === k ? formatted : inner;
1203
+ return inner.substring(2, inner.length - 2).trim() === k ? formatted : inner;
1219
1204
  });
1220
1205
  }
1221
1206
  (severity === "error" ? errors : warnings).push({
@@ -1265,7 +1250,7 @@ async function lintRunner({ tokens, filename, config = {}, src, logger }) {
1265
1250
  }
1266
1251
 
1267
1252
  //#endregion
1268
- //#region ../../node_modules/.pnpm/@humanwhocodes+momoa@3.3.8/node_modules/@humanwhocodes/momoa/dist/momoa.js
1253
+ //#region ../../node_modules/.pnpm/@humanwhocodes+momoa@3.3.9/node_modules/@humanwhocodes/momoa/dist/momoa.js
1269
1254
  /**
1270
1255
  * @fileoverview Character codes.
1271
1256
  * @author Nicholas C. Zakas
@@ -2341,6 +2326,31 @@ function normalizeIdentifier(identifier) {
2341
2326
  });
2342
2327
  }
2343
2328
  /**
2329
+ * Calculates the location at the end of the given text.
2330
+ * @param {string} text The text to calculate the end location for.
2331
+ * @returns {Location} The location at the end of the text.
2332
+ */
2333
+ function getEndLocation(text) {
2334
+ let line = 1;
2335
+ let column = 1;
2336
+ for (let i = 0; i < text.length; i++) {
2337
+ const char = text[i];
2338
+ if (char === "\n") {
2339
+ line++;
2340
+ column = 1;
2341
+ } else if (char === "\r") {
2342
+ if (text[i + 1] === "\n") i++;
2343
+ line++;
2344
+ column = 1;
2345
+ } else column++;
2346
+ }
2347
+ return {
2348
+ line,
2349
+ column,
2350
+ offset: text.length
2351
+ };
2352
+ }
2353
+ /**
2344
2354
  * Converts a JSON-encoded string into a JavaScript string, interpreting each
2345
2355
  * escape sequence.
2346
2356
  * @param {string} value The text for the token.
@@ -2495,12 +2505,11 @@ function parse$1(text, options) {
2495
2505
  const token = tokenizer.token;
2496
2506
  const range = createRange(token.loc.start, token.loc.end);
2497
2507
  const value = getLiteralValue(text.slice(token.loc.start.offset, token.loc.end.offset), token, json5);
2498
- const loc = {
2499
- start: { ...token.loc.start },
2500
- end: { ...token.loc.end }
2501
- };
2502
2508
  const parts = {
2503
- loc,
2509
+ loc: {
2510
+ start: { ...token.loc.start },
2511
+ end: { ...token.loc.end }
2512
+ },
2504
2513
  ...range
2505
2514
  };
2506
2515
  switch (tokenType) {
@@ -2519,12 +2528,11 @@ function parse$1(text, options) {
2519
2528
  function createJSON5IdentifierNode(token) {
2520
2529
  const range = createRange(token.loc.start, token.loc.end);
2521
2530
  const identifier = text.slice(token.loc.start.offset, token.loc.end.offset);
2522
- const loc = {
2523
- start: { ...token.loc.start },
2524
- end: { ...token.loc.end }
2525
- };
2526
2531
  const parts = {
2527
- loc,
2532
+ loc: {
2533
+ start: { ...token.loc.start },
2534
+ end: { ...token.loc.end }
2535
+ },
2528
2536
  ...range
2529
2537
  };
2530
2538
  if (token.type !== "Identifier") {
@@ -2674,15 +2682,14 @@ function parse$1(text, options) {
2674
2682
  }
2675
2683
  }
2676
2684
  const docBody = parseValue();
2677
- const unexpectedToken = next();
2678
- if (unexpectedToken) throw new UnexpectedToken(tokenizer.token);
2685
+ if (next()) throw new UnexpectedToken(tokenizer.token);
2679
2686
  const docParts = { loc: {
2680
2687
  start: {
2681
2688
  line: 1,
2682
2689
  column: 1,
2683
2690
  offset: 0
2684
2691
  },
2685
- end: { ...docBody.loc.end }
2692
+ end: { ...getEndLocation(text) }
2686
2693
  } };
2687
2694
  if (options.tokens) docParts.tokens = tokens;
2688
2695
  if (options.ranges) docParts.range = [docParts.loc.start.offset, docParts.loc.end.offset];
@@ -3068,48 +3075,42 @@ function applyAliases(token, options) {
3068
3075
  }
3069
3076
  const node = getObjMembers(options.node).$value || options.node;
3070
3077
  switch (token.$type) {
3071
- case "border": {
3078
+ case "border":
3072
3079
  applyBorderPartialAlias(token, mode, {
3073
3080
  ...options,
3074
3081
  node
3075
3082
  });
3076
3083
  break;
3077
- }
3078
- case "gradient": {
3084
+ case "gradient":
3079
3085
  applyGradientPartialAlias(token, mode, {
3080
3086
  ...options,
3081
3087
  node
3082
3088
  });
3083
3089
  break;
3084
- }
3085
- case "shadow": {
3090
+ case "shadow":
3086
3091
  applyShadowPartialAlias(token, mode, {
3087
3092
  ...options,
3088
3093
  node
3089
3094
  });
3090
3095
  break;
3091
- }
3092
- case "strokeStyle": {
3096
+ case "strokeStyle":
3093
3097
  applyStrokeStylePartialAlias(token, mode, {
3094
3098
  ...options,
3095
3099
  node
3096
3100
  });
3097
3101
  break;
3098
- }
3099
- case "transition": {
3102
+ case "transition":
3100
3103
  applyTransitionPartialAlias(token, mode, {
3101
3104
  ...options,
3102
3105
  node
3103
3106
  });
3104
3107
  break;
3105
- }
3106
- case "typography": {
3108
+ case "typography":
3107
3109
  applyTypographyPartialAlias(token, mode, {
3108
3110
  ...options,
3109
3111
  node
3110
3112
  });
3111
3113
  break;
3112
- }
3113
3114
  }
3114
3115
  }
3115
3116
  }
@@ -3361,7 +3362,7 @@ function normalizeValue(token) {
3361
3362
  if (typeof token.$value === "string" && isAlias(token.$value)) return token.$value;
3362
3363
  switch (token.$type) {
3363
3364
  case "boolean": return !!token.$value;
3364
- case "border": {
3365
+ case "border":
3365
3366
  if (typeof token.$value === "string") return token.$value;
3366
3367
  return {
3367
3368
  color: normalizeValue({
@@ -3377,7 +3378,6 @@ function normalizeValue(token) {
3377
3378
  $value: token.$value.width
3378
3379
  })
3379
3380
  };
3380
- }
3381
3381
  case "color": {
3382
3382
  if (typeof token.$value === "string") return parseColor(token.$value);
3383
3383
  const newValue = {
@@ -3388,14 +3388,13 @@ function normalizeValue(token) {
3388
3388
  if ("hex" in token.$value) newValue.hex = token.$value.hex;
3389
3389
  return newValue;
3390
3390
  }
3391
- case "cubicBezier": {
3391
+ case "cubicBezier":
3392
3392
  if (typeof token.$value === "string") return token.$value;
3393
3393
  return token.$value.map((value) => typeof value === "number" ? normalizeValue({
3394
3394
  $type: "number",
3395
3395
  $value: value
3396
3396
  }) : value);
3397
- }
3398
- case "dimension": {
3397
+ case "dimension":
3399
3398
  if (token.$value === 0) return {
3400
3399
  value: 0,
3401
3400
  unit: "px"
@@ -3408,8 +3407,7 @@ function normalizeValue(token) {
3408
3407
  };
3409
3408
  }
3410
3409
  return token.$value;
3411
- }
3412
- case "duration": {
3410
+ case "duration":
3413
3411
  if (token.$value === 0) return {
3414
3412
  value: 0,
3415
3413
  unit: "ms"
@@ -3422,12 +3420,10 @@ function normalizeValue(token) {
3422
3420
  };
3423
3421
  }
3424
3422
  return token.$value;
3425
- }
3426
3423
  case "fontFamily": return Array.isArray(token.$value) ? token.$value : [token.$value];
3427
- case "fontWeight": {
3424
+ case "fontWeight":
3428
3425
  if (typeof token.$value === "string" && FONT_WEIGHT_MAP[token.$value]) return FONT_WEIGHT_MAP[token.$value];
3429
3426
  return Math.min(999, Math.max(1, typeof token.$value === "string" ? Number.parseInt(token.$value) : token.$value));
3430
- }
3431
3427
  case "gradient": {
3432
3428
  if (typeof token.$value === "string") return token.$value;
3433
3429
  const output = [];
@@ -3443,7 +3439,7 @@ function normalizeValue(token) {
3443
3439
  return output;
3444
3440
  }
3445
3441
  case "number": return typeof token.$value === "number" ? token.$value : Number.parseFloat(token.$value);
3446
- case "shadow": {
3442
+ case "shadow":
3447
3443
  if (typeof token.$value === "string") return token.$value;
3448
3444
  return (Array.isArray(token.$value) ? token.$value : [token.$value]).map((layer) => ({
3449
3445
  color: normalizeValue({
@@ -3480,10 +3476,9 @@ function normalizeValue(token) {
3480
3476
  }),
3481
3477
  inset: layer.inset === true
3482
3478
  }));
3483
- }
3484
3479
  case "strokeStyle": return token.$value;
3485
3480
  case "string": return String(token.$value);
3486
- case "transition": {
3481
+ case "transition":
3487
3482
  if (typeof token.$value === "string") return token.$value;
3488
3483
  return {
3489
3484
  duration: normalizeValue({
@@ -3499,37 +3494,32 @@ function normalizeValue(token) {
3499
3494
  $value: token.$value.timingFunction
3500
3495
  })
3501
3496
  };
3502
- }
3503
3497
  case "typography": {
3504
3498
  if (typeof token.$value === "string") return token.$value;
3505
3499
  const output = {};
3506
3500
  for (const [k, $value] of Object.entries(token.$value)) switch (k) {
3507
- case "fontFamily": {
3501
+ case "fontFamily":
3508
3502
  output[k] = normalizeValue({
3509
3503
  $type: "fontFamily",
3510
3504
  $value
3511
3505
  });
3512
3506
  break;
3513
- }
3514
3507
  case "fontSize":
3515
- case "letterSpacing": {
3508
+ case "letterSpacing":
3516
3509
  output[k] = normalizeValue({
3517
3510
  $type: "dimension",
3518
3511
  $value
3519
3512
  });
3520
3513
  break;
3521
- }
3522
- case "lineHeight": {
3514
+ case "lineHeight":
3523
3515
  output[k] = normalizeValue({
3524
3516
  $type: typeof token.$value === "number" ? "number" : "dimension",
3525
3517
  $value
3526
3518
  });
3527
3519
  break;
3528
- }
3529
- default: {
3520
+ default:
3530
3521
  output[k] = $value;
3531
3522
  break;
3532
- }
3533
3523
  }
3534
3524
  return output;
3535
3525
  }
@@ -4132,56 +4122,49 @@ function validateTokenMemberNode(node, { filename, src, logger }) {
4132
4122
  message: "Token missing $type"
4133
4123
  });
4134
4124
  switch ($type.value) {
4135
- case "color": {
4125
+ case "color":
4136
4126
  validateColor($value, node, {
4137
4127
  logger,
4138
4128
  src
4139
4129
  });
4140
4130
  break;
4141
- }
4142
- case "cubicBezier": {
4131
+ case "cubicBezier":
4143
4132
  validateCubicBezier($value, node, {
4144
4133
  logger,
4145
4134
  src
4146
4135
  });
4147
4136
  break;
4148
- }
4149
- case "dimension": {
4137
+ case "dimension":
4150
4138
  validateDimension($value, node, {
4151
4139
  logger,
4152
4140
  src
4153
4141
  });
4154
4142
  break;
4155
- }
4156
- case "duration": {
4143
+ case "duration":
4157
4144
  validateDuration($value, node, {
4158
4145
  logger,
4159
4146
  src
4160
4147
  });
4161
4148
  break;
4162
- }
4163
- case "fontFamily": {
4149
+ case "fontFamily":
4164
4150
  validateFontFamily($value, node, {
4165
4151
  logger,
4166
4152
  src
4167
4153
  });
4168
4154
  break;
4169
- }
4170
- case "fontWeight": {
4155
+ case "fontWeight":
4171
4156
  validateFontWeight($value, node, {
4172
4157
  logger,
4173
4158
  src
4174
4159
  });
4175
4160
  break;
4176
- }
4177
- case "number": {
4161
+ case "number":
4178
4162
  validateNumber($value, node, {
4179
4163
  logger,
4180
4164
  src
4181
4165
  });
4182
4166
  break;
4183
- }
4184
- case "shadow": {
4167
+ case "shadow":
4185
4168
  if ($value.type === "Object") validateShadowLayer($value, node, {
4186
4169
  logger,
4187
4170
  src
@@ -4196,16 +4179,14 @@ function validateTokenMemberNode(node, { filename, src, logger }) {
4196
4179
  node: $value
4197
4180
  });
4198
4181
  break;
4199
- }
4200
- case "boolean": {
4182
+ case "boolean":
4201
4183
  if ($value.type !== "Boolean") logger.error({
4202
4184
  ...baseMessage,
4203
4185
  message: `Expected boolean, received ${$value.type}`,
4204
4186
  node: $value
4205
4187
  });
4206
4188
  break;
4207
- }
4208
- case "link": {
4189
+ case "link":
4209
4190
  if ($value.type !== "String") logger.error({
4210
4191
  ...baseMessage,
4211
4192
  message: `Expected string, received ${$value.type}`,
@@ -4217,48 +4198,42 @@ function validateTokenMemberNode(node, { filename, src, logger }) {
4217
4198
  node: $value
4218
4199
  });
4219
4200
  break;
4220
- }
4221
- case "string": {
4201
+ case "string":
4222
4202
  if ($value.type !== "String") logger.error({
4223
4203
  ...baseMessage,
4224
4204
  message: `Expected string, received ${$value.type}`,
4225
4205
  node: $value
4226
4206
  });
4227
4207
  break;
4228
- }
4229
- case "border": {
4208
+ case "border":
4230
4209
  validateBorder($value, node, {
4231
4210
  filename,
4232
4211
  src,
4233
4212
  logger
4234
4213
  });
4235
4214
  break;
4236
- }
4237
- case "gradient": {
4215
+ case "gradient":
4238
4216
  validateGradient($value, node, {
4239
4217
  filename,
4240
4218
  src,
4241
4219
  logger
4242
4220
  });
4243
4221
  break;
4244
- }
4245
- case "strokeStyle": {
4222
+ case "strokeStyle":
4246
4223
  validateStrokeStyle($value, node, {
4247
4224
  filename,
4248
4225
  src,
4249
4226
  logger
4250
4227
  });
4251
4228
  break;
4252
- }
4253
- case "transition": {
4229
+ case "transition":
4254
4230
  validateTransition($value, node, {
4255
4231
  filename,
4256
4232
  src,
4257
4233
  logger
4258
4234
  });
4259
4235
  break;
4260
- }
4261
- case "typography": {
4236
+ case "typography":
4262
4237
  if ($value.type !== "Object") {
4263
4238
  logger.error({
4264
4239
  ...baseMessage,
@@ -4281,7 +4256,6 @@ function validateTokenMemberNode(node, { filename, src, logger }) {
4281
4256
  logger
4282
4257
  });
4283
4258
  break;
4284
- }
4285
4259
  default: break;
4286
4260
  }
4287
4261
  }
@@ -4289,8 +4263,7 @@ function validateTokenMemberNode(node, { filename, src, logger }) {
4289
4263
  * Groups can have properties that their child nodes will inherit. */
4290
4264
  function isGroupNode(node) {
4291
4265
  if (node.type !== "Object") return false;
4292
- const has$value = node.members.some((m) => m.name.type === "String" && m.name.value === "$value");
4293
- return !has$value;
4266
+ return !node.members.some((m) => m.name.type === "String" && m.name.value === "$value");
4294
4267
  }
4295
4268
  /** Check if a token node has the specified property name, and if it does, stores
4296
4269
  * the value in the `inherited` object as a side effect for future use. If not,
@@ -4537,8 +4510,13 @@ async function parseSingle(input, { filename, logger, config, skipLint, continue
4537
4510
  });
4538
4511
  if (node.value.type === "Object") {
4539
4512
  const $value = node.value.members.find((m) => m.name.type === "String" && m.name.value === "$value");
4540
- if ($value && inheritedTypeNode?.value.type === "String" && transform?.[inheritedTypeNode.value.value]) {
4541
- const result = transform[inheritedTypeNode.value.value]?.(evaluate(node.value), subpath.join("."), node);
4513
+ let typeNode = inheritedTypeNode;
4514
+ if (!typeNode) {
4515
+ const local$type = node.value.members.find((m) => m.name.type === "String" && m.name.value === "$type");
4516
+ if (local$type) typeNode = local$type;
4517
+ }
4518
+ if ($value && typeNode?.value.type === "String" && transform?.[typeNode.value.value]) {
4519
+ const result = transform[typeNode.value.value]?.(evaluate(node.value), subpath.join("."), node);
4542
4520
  if (result) node.value = parseJSON(result).body;
4543
4521
  }
4544
4522
  const token = validateTokenNode(node, {