patram 0.5.0 → 0.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.
@@ -5,7 +5,11 @@
5
5
  import { getFileExtension } from './claim-helpers.js';
6
6
  import { parseJsdocClaims } from './parse-jsdoc-claims.js';
7
7
  import { parseMarkdownClaims } from './parse-markdown-claims.js';
8
- import { MARKDOWN_SOURCE_FILE_EXTENSIONS } from './source-file-defaults.js';
8
+ import { parseYamlClaims } from './parse-yaml-claims.js';
9
+ import {
10
+ MARKDOWN_SOURCE_FILE_EXTENSIONS,
11
+ YAML_SOURCE_FILE_EXTENSIONS,
12
+ } from './source-file-defaults.js';
9
13
 
10
14
  /**
11
15
  * Source claim dispatch.
@@ -26,21 +30,24 @@ import { MARKDOWN_SOURCE_FILE_EXTENSIONS } from './source-file-defaults.js';
26
30
  */
27
31
 
28
32
  const MARKDOWN_EXTENSIONS = new Set(MARKDOWN_SOURCE_FILE_EXTENSIONS);
33
+ const YAML_EXTENSIONS = new Set(YAML_SOURCE_FILE_EXTENSIONS);
29
34
 
30
35
  /**
31
36
  * Parse one source file into claims and diagnostics.
32
37
  *
33
38
  * @param {ParseClaimsInput} parse_input
39
+ * @param {{ multi_value_directive_names?: ReadonlySet<string> }} [parse_options]
34
40
  * @returns {ParseSourceFileResult}
35
41
  */
36
- export function parseSourceFile(parse_input) {
42
+ export function parseSourceFile(parse_input, parse_options) {
37
43
  const file_extension = getFileExtension(parse_input.path);
38
44
 
39
45
  if (MARKDOWN_EXTENSIONS.has(file_extension)) {
40
- return {
41
- claims: parseMarkdownClaims(parse_input),
42
- diagnostics: [],
43
- };
46
+ return parseMarkdownClaims(parse_input, parse_options);
47
+ }
48
+
49
+ if (YAML_EXTENSIONS.has(file_extension)) {
50
+ return parseYamlClaims(parse_input, parse_options);
44
51
  }
45
52
 
46
53
  return parseJsdocClaims(parse_input);
@@ -50,8 +57,88 @@ export function parseSourceFile(parse_input) {
50
57
  * Parse a file into neutral Patram claims.
51
58
  *
52
59
  * @param {ParseClaimsInput} parse_input
60
+ * @param {{ multi_value_directive_names?: ReadonlySet<string> }} [parse_options]
53
61
  * @returns {PatramClaim[]}
54
62
  */
55
- export function parseClaims(parse_input) {
56
- return parseSourceFile(parse_input).claims;
63
+ export function parseClaims(parse_input, parse_options) {
64
+ return parseSourceFile(parse_input, parse_options).claims;
65
+ }
66
+
67
+ /**
68
+ * Build parser options from repo config.
69
+ *
70
+ * @param {{ fields?: Record<string, { multiple?: boolean }>, mappings?: Record<string, { emit?: unknown, node?: unknown }> } | undefined} repo_config
71
+ * @returns {{ multi_value_directive_names: Set<string> }}
72
+ */
73
+ export function createParseOptions(repo_config) {
74
+ return {
75
+ multi_value_directive_names: collectMultiValueDirectiveNames(repo_config),
76
+ };
77
+ }
78
+
79
+ /**
80
+ * @param {{ fields?: Record<string, { multiple?: boolean }>, mappings?: Record<string, { emit?: unknown, node?: unknown }> } | undefined} repo_config
81
+ * @returns {Set<string>}
82
+ */
83
+ function collectMultiValueDirectiveNames(repo_config) {
84
+ /** @type {Set<string>} */
85
+ const multi_value_directive_names = new Set();
86
+
87
+ collectMultipleFieldNames(repo_config?.fields, multi_value_directive_names);
88
+ collectEmitOnlyDirectiveNames(
89
+ repo_config?.mappings,
90
+ multi_value_directive_names,
91
+ );
92
+
93
+ return multi_value_directive_names;
94
+ }
95
+
96
+ /**
97
+ * @param {Record<string, { multiple?: boolean }> | undefined} fields
98
+ * @param {Set<string>} multi_value_directive_names
99
+ */
100
+ function collectMultipleFieldNames(fields, multi_value_directive_names) {
101
+ for (const [field_name, field_definition] of Object.entries(fields ?? {})) {
102
+ if (field_definition.multiple === true) {
103
+ multi_value_directive_names.add(field_name);
104
+ }
105
+ }
106
+ }
107
+
108
+ /**
109
+ * @param {Record<string, { emit?: unknown, node?: unknown }> | undefined} mappings
110
+ * @param {Set<string>} multi_value_directive_names
111
+ */
112
+ function collectEmitOnlyDirectiveNames(mappings, multi_value_directive_names) {
113
+ for (const [mapping_name, mapping_definition] of Object.entries(
114
+ mappings ?? {},
115
+ )) {
116
+ const directive_name = resolveEmitOnlyDirectiveName(
117
+ mapping_name,
118
+ mapping_definition,
119
+ );
120
+
121
+ if (directive_name) {
122
+ multi_value_directive_names.add(directive_name);
123
+ }
124
+ }
125
+ }
126
+
127
+ /**
128
+ * @param {string} mapping_name
129
+ * @param {{ emit?: unknown, node?: unknown }} mapping_definition
130
+ * @returns {string | null}
131
+ */
132
+ function resolveEmitOnlyDirectiveName(mapping_name, mapping_definition) {
133
+ const directive_match = mapping_name.match(/^[^.]+\.directive\.(.+)$/du);
134
+
135
+ if (
136
+ !directive_match ||
137
+ mapping_definition.emit === undefined ||
138
+ mapping_definition.node !== undefined
139
+ ) {
140
+ return null;
141
+ }
142
+
143
+ return directive_match[1];
57
144
  }
@@ -1,5 +1,11 @@
1
1
  import type { PatramDiagnostic } from './load-patram-config.types.ts';
2
2
 
3
+ export type MarkdownDirectiveStyle =
4
+ | 'front_matter'
5
+ | 'visible_line'
6
+ | 'list_item'
7
+ | 'hidden_tag';
8
+
3
9
  export interface ParseClaimsInput {
4
10
  path: string;
5
11
  source: string;
@@ -14,6 +20,7 @@ export interface ClaimOrigin {
14
20
  export interface PatramClaim {
15
21
  document_id: string;
16
22
  id: string;
23
+ markdown_style?: MarkdownDirectiveStyle;
17
24
  name?: string;
18
25
  origin: ClaimOrigin;
19
26
  parser?: string;
@@ -1,5 +1,6 @@
1
1
  /**
2
2
  * @import { PatramClaim, ParseClaimsInput, PatramClaimFields } from './parse-claims.types.ts';
3
+ * @import { PatramDiagnostic } from './load-patram-config.types.ts';
3
4
  */
4
5
 
5
6
  import { createClaim, isPathLikeTarget } from './claim-helpers.js';
@@ -31,9 +32,10 @@ const MARKDOWN_LINK_PATTERN = /\[([^\]]+)\]\(([^)]+)\)/dgu;
31
32
 
32
33
  /**
33
34
  * @param {ParseClaimsInput} parse_input
34
- * @returns {PatramClaim[]}
35
+ * @param {{ multi_value_directive_names?: ReadonlySet<string> }} [parse_options]
36
+ * @returns {{ claims: PatramClaim[], diagnostics: PatramDiagnostic[] }}
35
37
  */
36
- export function parseMarkdownClaims(parse_input) {
38
+ export function parseMarkdownClaims(parse_input, parse_options) {
37
39
  const lines = parse_input.source.split('\n');
38
40
 
39
41
  /** @type {PatramClaim[]} */
@@ -41,6 +43,7 @@ export function parseMarkdownClaims(parse_input) {
41
43
  const front_matter_result = parseFrontMatterDirectiveFields(
42
44
  parse_input.path,
43
45
  lines,
46
+ parse_options,
44
47
  );
45
48
  /** @type {{ character: string, length: number } | null} */
46
49
  let open_fence = null;
@@ -93,7 +96,10 @@ export function parseMarkdownClaims(parse_input) {
93
96
  collectHiddenDirectiveClaims(parse_input.path, line, line_number, claims);
94
97
  }
95
98
 
96
- return claims;
99
+ return {
100
+ claims,
101
+ diagnostics: front_matter_result.diagnostics,
102
+ };
97
103
  }
98
104
 
99
105
  /**
@@ -1,22 +1,31 @@
1
1
  /**
2
- * @import { PatramClaimFields } from './parse-claims.types.ts';
2
+ * @import { MarkdownDirectiveStyle, PatramClaimFields } from './parse-claims.types.ts';
3
+ * @import { PatramDiagnostic } from './load-patram-config.types.ts';
3
4
  */
4
5
 
6
+ import { parseYamlDirectiveFields } from './parse-yaml-claims.js';
7
+
5
8
  const FRONT_MATTER_BOUNDARY_PATTERN = /^---$/du;
6
- const FRONT_MATTER_DIRECTIVE_PATTERN = /^([A-Za-z][A-Za-z0-9 _-]*):\s+(.+)$/du;
7
9
  const MARKDOWN_HIDDEN_DIRECTIVE_PATTERN =
8
10
  /^\[patram\s+([A-Za-z][A-Za-z0-9 _-]*)=(.+)\]:\s*#\s*$/du;
9
- const VISIBLE_DIRECTIVE_PATTERN = /^(?:-\s+)?([A-Z][A-Za-z _-]*):\s+(.+)$/du;
11
+ const LIST_ITEM_DIRECTIVE_PATTERN = /^-\s+([A-Z][A-Za-z _-]*):\s+(.+)$/du;
12
+ const VISIBLE_DIRECTIVE_PATTERN = /^([A-Z][A-Za-z _-]*):\s+(.+)$/du;
10
13
 
11
14
  /**
12
15
  * @param {string} file_path
13
16
  * @param {string[]} lines
14
- * @returns {{ body_start: number, directive_fields: PatramClaimFields[] }}
17
+ * @param {{ multi_value_directive_names?: ReadonlySet<string> }} [parse_options]
18
+ * @returns {{ body_start: number, diagnostics: PatramDiagnostic[], directive_fields: PatramClaimFields[] }}
15
19
  */
16
- export function parseFrontMatterDirectiveFields(file_path, lines) {
20
+ export function parseFrontMatterDirectiveFields(
21
+ file_path,
22
+ lines,
23
+ parse_options,
24
+ ) {
17
25
  if (lines[0] !== '---') {
18
26
  return {
19
27
  body_start: 0,
28
+ diagnostics: [],
20
29
  directive_fields: [],
21
30
  };
22
31
  }
@@ -26,31 +35,23 @@ export function parseFrontMatterDirectiveFields(file_path, lines) {
26
35
  if (closing_line_index < 0) {
27
36
  return {
28
37
  body_start: 0,
38
+ diagnostics: [],
29
39
  directive_fields: [],
30
40
  };
31
41
  }
32
-
33
- /** @type {PatramClaimFields[]} */
34
- const directive_fields = [];
35
-
36
- for (let line_index = 1; line_index < closing_line_index; line_index += 1) {
37
- const directive_fields_match = matchDirectiveFields(
38
- FRONT_MATTER_DIRECTIVE_PATTERN,
39
- file_path,
40
- lines[line_index],
41
- line_index + 1,
42
- );
43
-
44
- if (!directive_fields_match) {
45
- continue;
46
- }
47
-
48
- directive_fields.push(directive_fields_match);
49
- }
42
+ const parse_result = parseYamlDirectiveFields({
43
+ file_path,
44
+ markdown_style: 'front_matter',
45
+ multi_value_directive_names: parse_options?.multi_value_directive_names,
46
+ parser: 'markdown',
47
+ source_text: lines.slice(1, closing_line_index).join('\n'),
48
+ start_line: 2,
49
+ });
50
50
 
51
51
  return {
52
52
  body_start: closing_line_index + 1,
53
- directive_fields,
53
+ diagnostics: parse_result.diagnostics,
54
+ directive_fields: parse_result.directive_fields,
54
55
  };
55
56
  }
56
57
 
@@ -61,8 +62,21 @@ export function parseFrontMatterDirectiveFields(file_path, lines) {
61
62
  * @returns {PatramClaimFields | null}
62
63
  */
63
64
  export function matchVisibleDirectiveFields(file_path, line, line_number) {
65
+ const list_item_match = matchDirectiveFields(
66
+ LIST_ITEM_DIRECTIVE_PATTERN,
67
+ 'list_item',
68
+ file_path,
69
+ line,
70
+ line_number,
71
+ );
72
+
73
+ if (list_item_match) {
74
+ return list_item_match;
75
+ }
76
+
64
77
  return matchDirectiveFields(
65
78
  VISIBLE_DIRECTIVE_PATTERN,
79
+ 'visible_line',
66
80
  file_path,
67
81
  line,
68
82
  line_number,
@@ -78,6 +92,7 @@ export function matchVisibleDirectiveFields(file_path, line, line_number) {
78
92
  export function matchHiddenDirectiveFields(file_path, line, line_number) {
79
93
  return matchDirectiveFields(
80
94
  MARKDOWN_HIDDEN_DIRECTIVE_PATTERN,
95
+ 'hidden_tag',
81
96
  file_path,
82
97
  line,
83
98
  line_number,
@@ -100,12 +115,19 @@ function findFrontMatterClosingLineIndex(lines) {
100
115
 
101
116
  /**
102
117
  * @param {RegExp} pattern
118
+ * @param {MarkdownDirectiveStyle} markdown_style
103
119
  * @param {string} file_path
104
120
  * @param {string} line
105
121
  * @param {number} line_number
106
122
  * @returns {PatramClaimFields | null}
107
123
  */
108
- function matchDirectiveFields(pattern, file_path, line, line_number) {
124
+ function matchDirectiveFields(
125
+ pattern,
126
+ markdown_style,
127
+ file_path,
128
+ line,
129
+ line_number,
130
+ ) {
109
131
  const directive_match = line.match(pattern);
110
132
 
111
133
  if (!directive_match) {
@@ -113,6 +135,7 @@ function matchDirectiveFields(pattern, file_path, line, line_number) {
113
135
  }
114
136
 
115
137
  return {
138
+ markdown_style,
116
139
  name: normalizeDirectiveName(directive_match[1]),
117
140
  origin: {
118
141
  column: 1,