eslint-plugin-yml 3.4.0 → 3.6.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 (3) hide show
  1. package/README.md +1 -0
  2. package/lib/index.mjs +145 -19
  3. package/package.json +4 -4
package/README.md CHANGED
@@ -257,6 +257,7 @@ The rules with the following star :star: are included in the config.
257
257
  | [yml/key-spacing](https://ota-meshi.github.io/eslint-plugin-yml/rules/key-spacing.html) | enforce consistent spacing between keys and values in mapping pairs | :wrench: | | :star: |
258
258
  | [yml/no-irregular-whitespace](https://ota-meshi.github.io/eslint-plugin-yml/rules/no-irregular-whitespace.html) | disallow irregular whitespace | | :star: | :star: |
259
259
  | [yml/no-multiple-empty-lines](https://ota-meshi.github.io/eslint-plugin-yml/rules/no-multiple-empty-lines.html) | disallow multiple empty lines | :wrench: | | :star: |
260
+ | [yml/no-trailing-spaces](https://ota-meshi.github.io/eslint-plugin-yml/rules/no-trailing-spaces.html) | disallow trailing whitespace at the end of lines | :wrench: | | :star: |
260
261
  | [yml/spaced-comment](https://ota-meshi.github.io/eslint-plugin-yml/rules/spaced-comment.html) | enforce consistent spacing after the `#` in a comment | :wrench: | | :star: |
261
262
 
262
263
  <!--RULES_TABLE_END-->
package/lib/index.mjs CHANGED
@@ -3376,6 +3376,99 @@ var no_tab_indent_default = createRule("no-tab-indent", {
3376
3376
  }
3377
3377
  });
3378
3378
  //#endregion
3379
+ //#region src/rules/no-trailing-spaces.ts
3380
+ const BLANK_CLASS = "[\\t \\u00a0\\u2000\\u2001\\u2002\\u2003\\u2004\\u2005\\u2006\\u2007\\u2008\\u2009\\u200a\\u200b\\u3000]";
3381
+ const TRAILING_SPACES = new RegExp(`${BLANK_CLASS}+$`, "u");
3382
+ const BLANK_LINE = new RegExp(`^${BLANK_CLASS}*$`, "u");
3383
+ /** Check whether a reported whitespace range is fully inside a known range. */
3384
+ function isInRange(rangeStart, rangeEnd, range) {
3385
+ return range[0] <= rangeStart && rangeEnd <= range[1];
3386
+ }
3387
+ var no_trailing_spaces_default = createRule("no-trailing-spaces", {
3388
+ meta: {
3389
+ docs: {
3390
+ description: "disallow trailing whitespace at the end of lines",
3391
+ categories: ["standard"],
3392
+ extensionRule: "no-trailing-spaces",
3393
+ layout: true
3394
+ },
3395
+ fixable: "whitespace",
3396
+ schema: [{
3397
+ type: "object",
3398
+ properties: {
3399
+ skipBlankLines: {
3400
+ type: "boolean",
3401
+ default: false
3402
+ },
3403
+ ignoreComments: {
3404
+ type: "boolean",
3405
+ default: false
3406
+ }
3407
+ },
3408
+ additionalProperties: false
3409
+ }],
3410
+ messages: { trailingSpace: "Trailing spaces not allowed." },
3411
+ type: "layout"
3412
+ },
3413
+ create(context) {
3414
+ const sourceCode = context.sourceCode;
3415
+ if (!sourceCode.parserServices?.isYAML) return {};
3416
+ const options = context.options[0] || {};
3417
+ const skipBlankLines = Boolean(options.skipBlankLines);
3418
+ const ignoreComments = Boolean(options.ignoreComments);
3419
+ const scalarRanges = [];
3420
+ /** Check ranges that should not be reported or fixed. */
3421
+ function isIgnoredRange(lineNumber, rangeStart, rangeEnd) {
3422
+ if (ignoreComments && sourceCode.getAllComments().some((comment) => isInRange(rangeStart, rangeEnd, comment.range))) return true;
3423
+ return scalarRanges.some((scalar) => lineNumber > scalar.startLine && isInRange(rangeStart, rangeEnd, scalar.range));
3424
+ }
3425
+ return {
3426
+ YAMLScalar(node) {
3427
+ if (node.loc.start.line < node.loc.end.line) scalarRanges.push({
3428
+ range: node.range,
3429
+ startLine: node.loc.start.line
3430
+ });
3431
+ },
3432
+ "Program:exit"() {
3433
+ for (let lineIndex = 0; lineIndex < sourceCode.lines.length; lineIndex++) {
3434
+ const line = sourceCode.lines[lineIndex];
3435
+ const match = TRAILING_SPACES.exec(line);
3436
+ if (!match) continue;
3437
+ if (skipBlankLines && BLANK_LINE.test(line)) continue;
3438
+ const lineNumber = lineIndex + 1;
3439
+ const startColumn = match.index;
3440
+ const endColumn = line.length;
3441
+ const rangeStart = sourceCode.getIndexFromLoc({
3442
+ line: lineNumber,
3443
+ column: startColumn
3444
+ });
3445
+ const rangeEnd = sourceCode.getIndexFromLoc({
3446
+ line: lineNumber,
3447
+ column: endColumn
3448
+ });
3449
+ if (isIgnoredRange(lineNumber, rangeStart, rangeEnd)) continue;
3450
+ context.report({
3451
+ loc: {
3452
+ start: {
3453
+ line: lineNumber,
3454
+ column: startColumn
3455
+ },
3456
+ end: {
3457
+ line: lineNumber,
3458
+ column: endColumn
3459
+ }
3460
+ },
3461
+ messageId: "trailingSpace",
3462
+ fix(fixer) {
3463
+ return fixer.removeRange([rangeStart, rangeEnd]);
3464
+ }
3465
+ });
3466
+ }
3467
+ }
3468
+ };
3469
+ }
3470
+ });
3471
+ //#endregion
3379
3472
  //#region src/rules/no-trailing-zeros.ts
3380
3473
  var no_trailing_zeros_default = createRule("no-trailing-zeros", {
3381
3474
  meta: {
@@ -4506,6 +4599,10 @@ var YAMLEntryData = class {
4506
4599
  get value() {
4507
4600
  return (this.cached ?? (this.cached = { value: this.node == null ? null : getStaticYAMLValue(this.node) })).value;
4508
4601
  }
4602
+ getValueForKey(key) {
4603
+ const val = this.value;
4604
+ if (val !== null && typeof val === "object" && !Array.isArray(val)) return val[key];
4605
+ }
4509
4606
  };
4510
4607
  var YAMLSequenceData = class {
4511
4608
  node;
@@ -4524,7 +4621,7 @@ var YAMLSequenceData = class {
4524
4621
  /**
4525
4622
  * Build function which check that the given 2 names are in specific order.
4526
4623
  */
4527
- function buildValidatorFromType(order, insensitive, natural) {
4624
+ function buildValidatorFromType(order, insensitive, natural, key) {
4528
4625
  let compareValue = ([a, b]) => a <= b;
4529
4626
  let compareText = compareValue;
4530
4627
  if (natural) compareText = ([a, b]) => naturalCompare(a, b) <= 0;
@@ -4538,11 +4635,23 @@ function buildValidatorFromType(order, insensitive, natural) {
4538
4635
  const baseCompareValue = compareValue;
4539
4636
  compareValue = (args) => baseCompareValue(args.reverse());
4540
4637
  }
4541
- return (a, b) => {
4542
- if (typeof a.value === "string" && typeof b.value === "string") return compareText([a.value, b.value]);
4543
- const type = getYAMLPrimitiveType(a.value);
4544
- if (type && type === getYAMLPrimitiveType(b.value)) return compareValue([a.value, b.value]);
4638
+ /**
4639
+ * Compare resolved sequence entry values.
4640
+ */
4641
+ function compare(aVal, bVal) {
4642
+ if (typeof aVal === "string" && typeof bVal === "string") return compareText([aVal, bVal]);
4643
+ const type = getYAMLPrimitiveType(aVal);
4644
+ if (type && type === getYAMLPrimitiveType(bVal)) return compareValue([aVal, bVal]);
4545
4645
  return true;
4646
+ }
4647
+ return (a, b) => {
4648
+ if (key) {
4649
+ const aVal = a.getValueForKey(key);
4650
+ const bVal = b.getValueForKey(key);
4651
+ if (aVal === void 0 || bVal === void 0) return true;
4652
+ return compare(aVal, bVal);
4653
+ }
4654
+ return compare(a.value, b.value);
4546
4655
  };
4547
4656
  }
4548
4657
  /**
@@ -4557,14 +4666,16 @@ function parseOptions(options, sourceCode) {
4557
4666
  const type = order.type ?? "asc";
4558
4667
  const insensitive = order.caseSensitive === false;
4559
4668
  const natural = Boolean(order.natural);
4669
+ const key = order.key;
4560
4670
  return {
4561
4671
  isTargetArray,
4562
- ignore: () => false,
4563
- isValidOrder: buildValidatorFromType(type, insensitive, natural),
4672
+ ignore: key ? (v) => v.getValueForKey(key) === void 0 : () => false,
4673
+ isValidOrder: buildValidatorFromType(type, insensitive, natural, key),
4564
4674
  orderText(data) {
4565
- if (typeof data.value === "string") return `${natural ? "natural " : ""}${insensitive ? "insensitive " : ""}${type}ending`;
4566
- return `${type}ending`;
4567
- }
4675
+ const base = typeof data.value === "string" || key ? `${natural ? "natural " : ""}${insensitive ? "insensitive " : ""}${type}ending` : `${type}ending`;
4676
+ return key ? `${base} by '${key}'` : base;
4677
+ },
4678
+ key
4568
4679
  };
4569
4680
  }
4570
4681
  const parsedOrder = [];
@@ -4578,9 +4689,16 @@ function parseOptions(options, sourceCode) {
4578
4689
  const type = nestOrder.type ?? "asc";
4579
4690
  const insensitive = nestOrder.caseSensitive === false;
4580
4691
  const natural = Boolean(nestOrder.natural);
4692
+ const itemKey = nestOrder.key;
4581
4693
  parsedOrder.push({
4582
- test: (v) => valuePattern ? Boolean(getYAMLPrimitiveType(v.value)) && valuePattern.test(String(v.value)) : true,
4583
- isValidNestOrder: buildValidatorFromType(type, insensitive, natural)
4694
+ test: (v) => {
4695
+ if (itemKey) {
4696
+ const keyVal = v.getValueForKey(itemKey);
4697
+ return valuePattern ? keyVal !== void 0 && valuePattern.test(String(keyVal)) : keyVal !== void 0;
4698
+ }
4699
+ return valuePattern ? Boolean(getYAMLPrimitiveType(v.value)) && valuePattern.test(String(v.value)) : true;
4700
+ },
4701
+ isValidNestOrder: buildValidatorFromType(type, insensitive, natural, itemKey)
4584
4702
  });
4585
4703
  }
4586
4704
  return {
@@ -4650,7 +4768,8 @@ const ORDER_OBJECT_SCHEMA = {
4650
4768
  properties: {
4651
4769
  type: { enum: ["asc", "desc"] },
4652
4770
  caseSensitive: { type: "boolean" },
4653
- natural: { type: "boolean" }
4771
+ natural: { type: "boolean" },
4772
+ key: { type: "string" }
4654
4773
  },
4655
4774
  additionalProperties: false
4656
4775
  };
@@ -4745,8 +4864,8 @@ var sort_sequence_values_default = createRule("sort-sequence-values", {
4745
4864
  loc: edit.a.reportLoc,
4746
4865
  messageId: "shouldBeAfter",
4747
4866
  data: {
4748
- thisValue: toText(edit.a),
4749
- targetValue: toText(target),
4867
+ thisValue: toText(edit.a, option.key),
4868
+ targetValue: toText(target, option.key),
4750
4869
  orderText: option.orderText(edit.a)
4751
4870
  },
4752
4871
  *fix(fixer) {
@@ -4761,8 +4880,8 @@ var sort_sequence_values_default = createRule("sort-sequence-values", {
4761
4880
  loc: edit.a.reportLoc,
4762
4881
  messageId: "shouldBeBefore",
4763
4882
  data: {
4764
- thisValue: toText(edit.a),
4765
- targetValue: toText(target),
4883
+ thisValue: toText(edit.a, option.key),
4884
+ targetValue: toText(target, option.key),
4766
4885
  orderText: option.orderText(edit.a)
4767
4886
  },
4768
4887
  *fix(fixer) {
@@ -4834,7 +4953,11 @@ var sort_sequence_values_default = createRule("sort-sequence-values", {
4834
4953
  /**
4835
4954
  * Convert to display text.
4836
4955
  */
4837
- function toText(data) {
4956
+ function toText(data, key) {
4957
+ if (key) {
4958
+ const keyVal = data.getValueForKey(key);
4959
+ if (keyVal !== void 0) return String(keyVal);
4960
+ }
4838
4961
  if (getYAMLPrimitiveType(data.value)) return String(data.value);
4839
4962
  return sourceCode.getText(data.node);
4840
4963
  }
@@ -5088,6 +5211,7 @@ const rules$1 = [
5088
5211
  no_irregular_whitespace_default,
5089
5212
  no_multiple_empty_lines_default,
5090
5213
  no_tab_indent_default,
5214
+ no_trailing_spaces_default,
5091
5215
  no_trailing_zeros_default,
5092
5216
  plain_scalar_default,
5093
5217
  quotes_default,
@@ -5269,6 +5393,7 @@ var standard_default = [...base_default, { rules: {
5269
5393
  "yml/no-irregular-whitespace": "error",
5270
5394
  "yml/no-multiple-empty-lines": "error",
5271
5395
  "yml/no-tab-indent": "error",
5396
+ "yml/no-trailing-spaces": "error",
5272
5397
  "yml/no-trailing-zeros": "error",
5273
5398
  "yml/plain-scalar": "error",
5274
5399
  "yml/quotes": "error",
@@ -5288,13 +5413,14 @@ var prettier_default = [...base_default, { rules: {
5288
5413
  "yml/indent": "off",
5289
5414
  "yml/key-spacing": "off",
5290
5415
  "yml/no-multiple-empty-lines": "off",
5416
+ "yml/no-trailing-spaces": "off",
5291
5417
  "yml/no-trailing-zeros": "off",
5292
5418
  "yml/quotes": "off"
5293
5419
  } }];
5294
5420
  //#endregion
5295
5421
  //#region package.json
5296
5422
  var name$1 = "eslint-plugin-yml";
5297
- var version$1 = "3.4.0";
5423
+ var version$1 = "3.6.0";
5298
5424
  //#endregion
5299
5425
  //#region src/meta.ts
5300
5426
  var meta_exports = /* @__PURE__ */ __exportAll({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-yml",
3
- "version": "3.4.0",
3
+ "version": "3.6.0",
4
4
  "description": "This ESLint plugin provides linting rules for YAML.",
5
5
  "type": "module",
6
6
  "main": "lib/index.mjs",
@@ -89,8 +89,8 @@
89
89
  "@types/natural-compare": "^1.4.0",
90
90
  "@types/node": "^24.0.0",
91
91
  "@types/semver": "^7.3.1",
92
- "@typescript-eslint/eslint-plugin": "~8.60.0",
93
- "@typescript-eslint/parser": "~8.60.0",
92
+ "@typescript-eslint/eslint-plugin": "~8.62.0",
93
+ "@typescript-eslint/parser": "~8.62.0",
94
94
  "cross-env": "^10.0.0",
95
95
  "env-cmd": "^11.0.0",
96
96
  "eslint": "^10.0.0",
@@ -111,7 +111,7 @@
111
111
  "mocha": "^11.0.0",
112
112
  "monaco-editor": "^0.55.0",
113
113
  "nyc": "^18.0.0",
114
- "pako": "^2.1.0",
114
+ "pako": "^3.0.0",
115
115
  "prettier": "^3.0.3",
116
116
  "semver": "^7.3.2",
117
117
  "stylelint": "^17.0.0",