@drupal-canvas/eslint-config 0.3.0 → 0.4.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 +7 -6
  2. package/dist/index.js +231 -132
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -33,12 +33,13 @@ export default defineConfig([
33
33
  The following custom rules are part of the `required` config and validate Drupal
34
34
  Canvas Code Components:
35
35
 
36
- | Rule | Description |
37
- | ------------------------------ | ------------------------------------------------------------------------------------------------------- |
38
- | `component-dir-name` | Validates that `machineName` matches the directory name (index-style) or filename prefix (named-style). |
39
- | `component-exports` | Validates that component has a default export. |
40
- | `component-image-example-urls` | Validates that `canvas.module/image` prop examples use fully qualified image URLs. |
41
- | `component-prop-names` | Validates that component prop IDs match the camelCase version of their titles. |
36
+ | Rule | Description |
37
+ | ---------------------------------------------- | ------------------------------------------------------------------------------------------------------- |
38
+ | `component-dir-name` | Validates that `machineName` matches the directory name (index-style) or filename prefix (named-style). |
39
+ | `component-exports` | Validates that component has a default export. |
40
+ | `component-prop-example-value-image-url` | Validates that `canvas.module/image` prop examples use fully qualified image URLs. |
41
+ | `component-prop-example-value-no-empty-string` | Validates that string prop examples do not contain empty string values. |
42
+ | `component-prop-names` | Validates that component prop IDs match the camelCase version of their titles. |
42
43
 
43
44
  ### Deprecated rules
44
45
 
package/dist/index.js CHANGED
@@ -320,9 +320,9 @@ var require_ignore = __commonJS({
320
320
  };
321
321
  }
322
322
  if (checkPattern(pattern.pattern)) {
323
- const rule9 = createRule(pattern, this._ignoreCase);
323
+ const rule10 = createRule(pattern, this._ignoreCase);
324
324
  this._added = true;
325
- this._rules.push(rule9);
325
+ this._rules.push(rule10);
326
326
  }
327
327
  }
328
328
  // @param {Array<string> | string | Ignore} pattern
@@ -344,18 +344,18 @@ var require_ignore = __commonJS({
344
344
  let ignored = false;
345
345
  let unignored = false;
346
346
  let matchedRule;
347
- this._rules.forEach((rule9) => {
348
- const { negative } = rule9;
347
+ this._rules.forEach((rule10) => {
348
+ const { negative } = rule10;
349
349
  if (unignored === negative && ignored !== unignored || negative && !ignored && !unignored && !checkUnignored) {
350
350
  return;
351
351
  }
352
- const matched = rule9[mode].test(path2);
352
+ const matched = rule10[mode].test(path2);
353
353
  if (!matched) {
354
354
  return;
355
355
  }
356
356
  ignored = !negative;
357
357
  unignored = negative;
358
- matchedRule = negative ? UNDEFINED : rule9;
358
+ matchedRule = negative ? UNDEFINED : rule10;
359
359
  });
360
360
  const ret = {
361
361
  ignored,
@@ -577,6 +577,15 @@ function getYAMLStringValue(node) {
577
577
  }
578
578
  return null;
579
579
  }
580
+ function isYAMLMapping(node) {
581
+ return node?.type === "YAMLMapping";
582
+ }
583
+ function isYAMLSequence(node) {
584
+ return node?.type === "YAMLSequence";
585
+ }
586
+ function getYAMLMappingPair(mapping, key) {
587
+ return mapping.pairs.find((pair) => getYAMLStringValue(pair.key) === key);
588
+ }
580
589
 
581
590
  // src/rules/component-dir-name.ts
582
591
  var NAMED_SUFFIX2 = ".component.yml";
@@ -668,104 +677,6 @@ var rule2 = {
668
677
  }
669
678
  };
670
679
  var component_exports_default = rule2;
671
-
672
- // src/rules/component-image-example-urls.ts
673
- var IMAGE_REF = "json-schema-definitions://canvas.module/image";
674
- function getMappingPair(mapping, key) {
675
- return mapping.pairs.find((pair) => getYAMLStringValue(pair.key) === key);
676
- }
677
- function isYamlMapping(node) {
678
- return node?.type === "YAMLMapping";
679
- }
680
- function isYamlSequence(node) {
681
- return node?.type === "YAMLSequence";
682
- }
683
- function hasImageRef(propMapping) {
684
- const ref = getYAMLStringValue(
685
- getMappingPair(propMapping, "$ref")?.value ?? null
686
- );
687
- if (ref === IMAGE_REF) {
688
- return true;
689
- }
690
- const itemsValue = getMappingPair(propMapping, "items")?.value;
691
- if (!isYamlMapping(itemsValue)) {
692
- return false;
693
- }
694
- return getYAMLStringValue(getMappingPair(itemsValue, "$ref")?.value ?? null) === IMAGE_REF;
695
- }
696
- function isFullyQualifiedUrl(value) {
697
- try {
698
- const parsed = new URL(value);
699
- return parsed.protocol.length > 0 && parsed.hostname.length > 0;
700
- } catch {
701
- return false;
702
- }
703
- }
704
- function getInvalidImageExampleNodes(examplesValue) {
705
- if (!isYamlSequence(examplesValue)) {
706
- return [];
707
- }
708
- const invalidNodes = [];
709
- for (const example of examplesValue.entries) {
710
- const imageEntries = isYamlSequence(example) ? example.entries : [example];
711
- for (const imageEntry of imageEntries) {
712
- if (!isYamlMapping(imageEntry)) {
713
- continue;
714
- }
715
- const srcNode = getMappingPair(imageEntry, "src")?.value;
716
- if (srcNode?.type === "YAMLScalar" && typeof srcNode.value === "string" && !isFullyQualifiedUrl(srcNode.value)) {
717
- invalidNodes.push(srcNode);
718
- }
719
- }
720
- }
721
- return invalidNodes;
722
- }
723
- var rule3 = {
724
- meta: {
725
- type: "problem",
726
- docs: {
727
- description: "Validates that default examples for image props use fully-qualified URLs"
728
- }
729
- },
730
- create(context) {
731
- if (!isComponentYmlFile(context.filename)) {
732
- return {};
733
- }
734
- return {
735
- YAMLPair(node) {
736
- const keyName = getYAMLStringValue(node.key);
737
- if (keyName !== "props" || !isYamlMapping(node.value)) {
738
- return;
739
- }
740
- const propertiesValue = getMappingPair(node.value, "properties")?.value;
741
- if (!isYamlMapping(propertiesValue)) {
742
- return;
743
- }
744
- for (const propPair of propertiesValue.pairs) {
745
- const propId = getYAMLStringValue(propPair.key);
746
- if (!propId || !isYamlMapping(propPair.value)) {
747
- continue;
748
- }
749
- if (!hasImageRef(propPair.value)) {
750
- continue;
751
- }
752
- const examplesValue = getMappingPair(
753
- propPair.value,
754
- "examples"
755
- )?.value;
756
- const invalidNodes = getInvalidImageExampleNodes(examplesValue);
757
- for (const invalidNode of invalidNodes) {
758
- context.report({
759
- node: invalidNode,
760
- message: `Image prop "${propId}" example src must be a fully-qualified URL with both scheme and host. Use a placeholder URL such as https://placehold.co/600x400.`
761
- });
762
- }
763
- }
764
- }
765
- };
766
- }
767
- };
768
- var component_image_example_urls_default = rule3;
769
680
  var Gt = (n7, t, e) => {
770
681
  let s = n7 instanceof RegExp ? ce(n7, e) : n7, i = t instanceof RegExp ? ce(t, e) : t, r = s !== null && i != null && ss(s, i, e);
771
682
  return r && { start: r[0], end: r[1], pre: e.slice(0, r[0]), body: e.slice(r[0] + s.length, r[1]), post: e.slice(r[1] + i.length) };
@@ -4670,7 +4581,7 @@ function checkImportSource(context, node, source) {
4670
4581
  return;
4671
4582
  }
4672
4583
  }
4673
- var rule4 = {
4584
+ var rule3 = {
4674
4585
  meta: {
4675
4586
  type: "problem",
4676
4587
  docs: {
@@ -4696,32 +4607,218 @@ var rule4 = {
4696
4607
  };
4697
4608
  }
4698
4609
  };
4699
- var component_imports_default = rule4;
4700
- function extractProps(propsNode) {
4701
- if (!propsNode.value || propsNode.value.type !== "YAMLMapping") {
4610
+ var component_imports_default = rule3;
4611
+
4612
+ // src/rules/component-prop-example-value-image-url.ts
4613
+ var IMAGE_REF = "json-schema-definitions://canvas.module/image";
4614
+ function hasImageRef(propMapping) {
4615
+ const ref = getYAMLStringValue(
4616
+ getYAMLMappingPair(propMapping, "$ref")?.value ?? null
4617
+ );
4618
+ if (ref === IMAGE_REF) {
4619
+ return true;
4620
+ }
4621
+ const itemsValue = getYAMLMappingPair(propMapping, "items")?.value;
4622
+ if (!isYAMLMapping(itemsValue)) {
4623
+ return false;
4624
+ }
4625
+ return getYAMLStringValue(
4626
+ getYAMLMappingPair(itemsValue, "$ref")?.value ?? null
4627
+ ) === IMAGE_REF;
4628
+ }
4629
+ function isFullyQualifiedUrl(value) {
4630
+ try {
4631
+ const parsed = new URL(value);
4632
+ return parsed.protocol.length > 0 && parsed.hostname.length > 0;
4633
+ } catch {
4634
+ return false;
4635
+ }
4636
+ }
4637
+ function getInvalidImageExampleNodes(examplesValue) {
4638
+ if (!isYAMLSequence(examplesValue)) {
4702
4639
  return [];
4703
4640
  }
4704
- const propsMapping = propsNode.value;
4705
- const propertiesPair = propsMapping.pairs.find(
4706
- (p) => getYAMLStringValue(p.key) === "properties"
4641
+ const invalidNodes = [];
4642
+ for (const example of examplesValue.entries) {
4643
+ const imageEntries = isYAMLSequence(example) ? example.entries : [example];
4644
+ for (const imageEntry of imageEntries) {
4645
+ if (!isYAMLMapping(imageEntry)) {
4646
+ continue;
4647
+ }
4648
+ const srcNode = getYAMLMappingPair(imageEntry, "src")?.value;
4649
+ if (srcNode?.type === "YAMLScalar" && typeof srcNode.value === "string" && !isFullyQualifiedUrl(srcNode.value)) {
4650
+ invalidNodes.push(srcNode);
4651
+ }
4652
+ }
4653
+ }
4654
+ return invalidNodes;
4655
+ }
4656
+ var rule4 = {
4657
+ meta: {
4658
+ type: "problem",
4659
+ docs: {
4660
+ description: "Validates that default examples for image props use fully-qualified URLs"
4661
+ }
4662
+ },
4663
+ create(context) {
4664
+ if (!isComponentYmlFile(context.filename)) {
4665
+ return {};
4666
+ }
4667
+ return {
4668
+ YAMLPair(node) {
4669
+ const keyName = getYAMLStringValue(node.key);
4670
+ if (keyName !== "props" || !isYAMLMapping(node.value)) {
4671
+ return;
4672
+ }
4673
+ const propertiesValue = getYAMLMappingPair(
4674
+ node.value,
4675
+ "properties"
4676
+ )?.value;
4677
+ if (!isYAMLMapping(propertiesValue)) {
4678
+ return;
4679
+ }
4680
+ for (const propPair of propertiesValue.pairs) {
4681
+ const propId = getYAMLStringValue(propPair.key);
4682
+ if (!propId || !isYAMLMapping(propPair.value)) {
4683
+ continue;
4684
+ }
4685
+ if (!hasImageRef(propPair.value)) {
4686
+ continue;
4687
+ }
4688
+ const examplesValue = getYAMLMappingPair(
4689
+ propPair.value,
4690
+ "examples"
4691
+ )?.value;
4692
+ const invalidNodes = getInvalidImageExampleNodes(examplesValue);
4693
+ for (const invalidNode of invalidNodes) {
4694
+ context.report({
4695
+ node: invalidNode,
4696
+ message: `Image prop "${propId}" example src must be a fully-qualified URL with both scheme and host. Use a placeholder URL such as https://placehold.co/600x400.`
4697
+ });
4698
+ }
4699
+ }
4700
+ }
4701
+ };
4702
+ }
4703
+ };
4704
+ var component_prop_example_value_image_url_default = rule4;
4705
+
4706
+ // src/rules/component-prop-example-value-no-empty-string.ts
4707
+ function isEmptyStringScalar(node) {
4708
+ return node?.type === "YAMLScalar" && node.value === "";
4709
+ }
4710
+ function getStringExampleMode(propMapping) {
4711
+ const type2 = getYAMLStringValue(
4712
+ getYAMLMappingPair(propMapping, "type")?.value ?? null
4707
4713
  );
4708
- if (!propertiesPair || !propertiesPair.value || propertiesPair.value.type !== "YAMLMapping") {
4714
+ if (type2 === "string") {
4715
+ return "string";
4716
+ }
4717
+ if (type2 !== "array") {
4718
+ return null;
4719
+ }
4720
+ const itemsValue = getYAMLMappingPair(propMapping, "items")?.value;
4721
+ if (!isYAMLMapping(itemsValue)) {
4722
+ return null;
4723
+ }
4724
+ return getYAMLStringValue(
4725
+ getYAMLMappingPair(itemsValue, "type")?.value ?? null
4726
+ ) === "string" ? "string-array" : null;
4727
+ }
4728
+ function getInvalidExampleNodes(examplesValue, mode) {
4729
+ if (!isYAMLSequence(examplesValue)) {
4730
+ return [];
4731
+ }
4732
+ if (mode === "string") {
4733
+ const invalidNodes2 = [];
4734
+ for (const example of examplesValue.entries) {
4735
+ if (isEmptyStringScalar(example)) {
4736
+ invalidNodes2.push(example);
4737
+ }
4738
+ }
4739
+ return invalidNodes2;
4740
+ }
4741
+ const invalidNodes = [];
4742
+ for (const example of examplesValue.entries) {
4743
+ if (!isYAMLSequence(example)) {
4744
+ continue;
4745
+ }
4746
+ for (const item of example.entries) {
4747
+ if (isEmptyStringScalar(item)) {
4748
+ invalidNodes.push(item);
4749
+ }
4750
+ }
4751
+ }
4752
+ return invalidNodes;
4753
+ }
4754
+ var rule5 = {
4755
+ meta: {
4756
+ type: "problem",
4757
+ docs: {
4758
+ description: "Validates that string prop examples do not contain empty string values"
4759
+ }
4760
+ },
4761
+ create(context) {
4762
+ if (!isComponentYmlFile(context.filename)) {
4763
+ return {};
4764
+ }
4765
+ return {
4766
+ YAMLPair(node) {
4767
+ const keyName = getYAMLStringValue(node.key);
4768
+ if (keyName !== "props" || !isYAMLMapping(node.value)) {
4769
+ return;
4770
+ }
4771
+ const propertiesValue = getYAMLMappingPair(
4772
+ node.value,
4773
+ "properties"
4774
+ )?.value;
4775
+ if (!isYAMLMapping(propertiesValue)) {
4776
+ return;
4777
+ }
4778
+ for (const propPair of propertiesValue.pairs) {
4779
+ const propId = getYAMLStringValue(propPair.key);
4780
+ if (!propId || !isYAMLMapping(propPair.value)) {
4781
+ continue;
4782
+ }
4783
+ const mode = getStringExampleMode(propPair.value);
4784
+ if (!mode) {
4785
+ continue;
4786
+ }
4787
+ const examplesValue = getYAMLMappingPair(
4788
+ propPair.value,
4789
+ "examples"
4790
+ )?.value;
4791
+ const invalidNodes = getInvalidExampleNodes(examplesValue, mode);
4792
+ for (const invalidNode of invalidNodes) {
4793
+ context.report({
4794
+ node: invalidNode,
4795
+ message: `Prop "${propId}" example values must not be empty strings. Remove the empty example or use a non-empty placeholder value.`
4796
+ });
4797
+ }
4798
+ }
4799
+ }
4800
+ };
4801
+ }
4802
+ };
4803
+ var component_prop_example_value_no_empty_string_default = rule5;
4804
+ function extractProps(propsNode) {
4805
+ if (!isYAMLMapping(propsNode.value)) {
4806
+ return [];
4807
+ }
4808
+ const propertiesValue = getYAMLMappingPair(
4809
+ propsNode.value,
4810
+ "properties"
4811
+ )?.value;
4812
+ if (!isYAMLMapping(propertiesValue)) {
4709
4813
  return [];
4710
4814
  }
4711
- const propertiesMapping = propertiesPair.value;
4712
4815
  const props = [];
4713
- for (const pair of propertiesMapping.pairs) {
4816
+ for (const pair of propertiesValue.pairs) {
4714
4817
  const propId = getYAMLStringValue(pair.key);
4715
4818
  if (!propId) continue;
4716
- if (!pair.value || pair.value.type !== "YAMLMapping") continue;
4717
- const propMapping = pair.value;
4718
- const titlePair = propMapping.pairs.find(
4719
- (p) => getYAMLStringValue(p.key) === "title"
4720
- );
4819
+ if (!isYAMLMapping(pair.value)) continue;
4721
4820
  let title = null;
4722
- if (titlePair) {
4723
- title = getYAMLStringValue(titlePair.value);
4724
- }
4821
+ title = getYAMLStringValue(getYAMLMappingPair(pair.value, "title")?.value);
4725
4822
  props.push({
4726
4823
  id: propId,
4727
4824
  title,
@@ -4730,7 +4827,7 @@ function extractProps(propsNode) {
4730
4827
  }
4731
4828
  return props;
4732
4829
  }
4733
- var rule5 = {
4830
+ var rule6 = {
4734
4831
  meta: {
4735
4832
  type: "problem",
4736
4833
  docs: {
@@ -4771,7 +4868,7 @@ var rule5 = {
4771
4868
  };
4772
4869
  }
4773
4870
  };
4774
- var component_prop_names_default = rule5;
4871
+ var component_prop_names_default = rule6;
4775
4872
 
4776
4873
  // src/configs/required.ts
4777
4874
  var required = defineConfig([
@@ -4795,18 +4892,20 @@ var required = defineConfig([
4795
4892
  plugins: {
4796
4893
  "drupal-canvas": {
4797
4894
  rules: {
4895
+ "component-prop-example-value-image-url": component_prop_example_value_image_url_default,
4896
+ "component-prop-example-value-no-empty-string": component_prop_example_value_no_empty_string_default,
4798
4897
  "component-dir-name": component_dir_name_default,
4799
4898
  "component-exports": component_exports_default,
4800
- "component-image-example-urls": component_image_example_urls_default,
4801
4899
  "component-imports": component_imports_default,
4802
4900
  "component-prop-names": component_prop_names_default
4803
4901
  }
4804
4902
  }
4805
4903
  },
4806
4904
  rules: {
4905
+ "drupal-canvas/component-prop-example-value-image-url": "error",
4906
+ "drupal-canvas/component-prop-example-value-no-empty-string": "error",
4807
4907
  "drupal-canvas/component-dir-name": "error",
4808
4908
  "drupal-canvas/component-exports": "error",
4809
- "drupal-canvas/component-image-example-urls": "error",
4810
4909
  "drupal-canvas/component-imports": "error",
4811
4910
  "drupal-canvas/component-prop-names": "error"
4812
4911
  }
@@ -4854,7 +4953,7 @@ var IGNORED_FILES = [
4854
4953
  function isFileAllowed(fileName, allowedFiles) {
4855
4954
  return allowedFiles.some((allowedFile) => allowedFile === fileName);
4856
4955
  }
4857
- var rule6 = {
4956
+ var rule7 = {
4858
4957
  meta: {
4859
4958
  type: "problem",
4860
4959
  docs: {
@@ -4893,7 +4992,7 @@ var rule6 = {
4893
4992
  };
4894
4993
  }
4895
4994
  };
4896
- var component_files_default = rule6;
4995
+ var component_files_default = rule7;
4897
4996
  function checkImportSource2(context, node, source) {
4898
4997
  if (source.startsWith("./") || source.startsWith("../")) {
4899
4998
  context.report({
@@ -5022,7 +5121,7 @@ function checkImportSource2(context, node, source) {
5022
5121
  message: `Importing "${source}" is not supported. If this is a local import via a path alias, use the "@/components/" alias instead. If you are importing a third-party package, see the list of supported packages at https://project.pages.drupalcode.org/canvas/code-components/packages. (The status of supporting any third-party package can be tracked at https://drupal.org/i/3560197.)`
5023
5122
  });
5024
5123
  }
5025
- var rule7 = {
5124
+ var rule8 = {
5026
5125
  meta: {
5027
5126
  type: "problem",
5028
5127
  docs: {
@@ -5049,7 +5148,7 @@ var rule7 = {
5049
5148
  };
5050
5149
  }
5051
5150
  };
5052
- var component_imports_deprecated_default = rule7;
5151
+ var component_imports_deprecated_default = rule8;
5053
5152
  function findTopmostComponentsParentDir(currentParentDir, rootDir) {
5054
5153
  if (currentParentDir === rootDir) {
5055
5154
  return currentParentDir;
@@ -5070,7 +5169,7 @@ function hasComponentSubdirectories(dirPath) {
5070
5169
  }
5071
5170
  return false;
5072
5171
  }
5073
- var rule8 = {
5172
+ var rule9 = {
5074
5173
  meta: {
5075
5174
  type: "problem",
5076
5175
  docs: {
@@ -5105,7 +5204,7 @@ var rule8 = {
5105
5204
  };
5106
5205
  }
5107
5206
  };
5108
- var component_no_hierarchy_default = rule8;
5207
+ var component_no_hierarchy_default = rule9;
5109
5208
 
5110
5209
  // src/configs/requiredDeprecated.ts
5111
5210
  var required2 = defineConfig([
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drupal-canvas/eslint-config",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "ESLint config for validating Drupal Canvas Code Components",
5
5
  "license": "MIT",
6
6
  "repository": {