@glion/lint-profile-required-components 0.14.1
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/LICENSE +21 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Rethink Health
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Root } from "@glion/ast";
|
|
2
|
+
/**
|
|
3
|
+
* Lint rule that validates required components in composite datatype fields.
|
|
4
|
+
*
|
|
5
|
+
* For each field with a composite datatype, checks that all components
|
|
6
|
+
* marked `required: true` in the datatype definition are present and
|
|
7
|
+
* non-empty in every field repetition.
|
|
8
|
+
*
|
|
9
|
+
* Empty fields are skipped (the required-fields rule handles that).
|
|
10
|
+
* Simple (non-composite) datatypes are skipped.
|
|
11
|
+
* Segments without a known profile (e.g., Z-segments) are silently skipped.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* unified().use(hl7v2LintRequiredComponents);
|
|
16
|
+
* ```;
|
|
17
|
+
*/
|
|
18
|
+
declare const hl7v2LintRequiredComponents: import("unified-lint-rule").Plugin<Root, unknown>;
|
|
19
|
+
export default hl7v2LintRequiredComponents;
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAiC,IAAI,EAAW,MAAM,YAAY,CAAC;AAO/E;;;;;;;;;;;;;;;GAeG;AACH,QAAA,MAAM,2BAA2B,mDAsDhC,CAAC;AAsCF,eAAe,2BAA2B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { SKIP, visit } from "@glion/util-visit";
|
|
2
|
+
import { isEmptyNode } from "@glion/utils";
|
|
3
|
+
import { lintRule } from "unified-lint-rule";
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
/**
|
|
6
|
+
* Lint rule that validates required components in composite datatype fields.
|
|
7
|
+
*
|
|
8
|
+
* For each field with a composite datatype, checks that all components
|
|
9
|
+
* marked `required: true` in the datatype definition are present and
|
|
10
|
+
* non-empty in every field repetition.
|
|
11
|
+
*
|
|
12
|
+
* Empty fields are skipped (the required-fields rule handles that).
|
|
13
|
+
* Simple (non-composite) datatypes are skipped.
|
|
14
|
+
* Segments without a known profile (e.g., Z-segments) are silently skipped.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* unified().use(hl7v2LintRequiredComponents);
|
|
19
|
+
* ```;
|
|
20
|
+
*/
|
|
21
|
+
const hl7v2LintRequiredComponents = lintRule({ origin: "hl7v2-lint:required-components" }, (tree, file) => {
|
|
22
|
+
const ctx = file.data.profile;
|
|
23
|
+
if (!ctx) return;
|
|
24
|
+
visit(tree, "field", (fieldNode, ancestors, info) => {
|
|
25
|
+
if (isEmptyNode(fieldNode)) return SKIP;
|
|
26
|
+
const segment = ancestors.at(-1);
|
|
27
|
+
if (!segment || segment.type !== "segment") return SKIP;
|
|
28
|
+
const fieldDef = ctx.fields.get(segment.name);
|
|
29
|
+
if (!fieldDef) return SKIP;
|
|
30
|
+
const fieldProfile = fieldDef.bySequence.get(info.sequence);
|
|
31
|
+
if (!fieldProfile) return SKIP;
|
|
32
|
+
const dtDef = ctx.datatypes.get(fieldProfile.datatype);
|
|
33
|
+
if (!dtDef || dtDef.kind !== "composite" || dtDef.requiredSequences.size === 0) return SKIP;
|
|
34
|
+
for (const repetition of fieldNode.children) checkRepetition(file, dtDef, repetition, segment, info.sequence, ancestors, fieldNode);
|
|
35
|
+
return SKIP;
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
/** Check a single field repetition for missing required components. */
|
|
39
|
+
function checkRepetition(file, dtDef, repetition, segment, sequence, ancestors, fieldNode) {
|
|
40
|
+
for (const compSeq of dtDef.requiredSequences) {
|
|
41
|
+
const component = repetition.children[compSeq - 1];
|
|
42
|
+
if (!(component?.children[0]?.value !== void 0 && component.children[0].value.length > 0)) {
|
|
43
|
+
const compProfile = dtDef.componentsBySequence.get(compSeq);
|
|
44
|
+
const compName = compProfile?.name ? ` (${compProfile.name})` : "";
|
|
45
|
+
file.message(`Required component ${segment.name}-${sequence}.${compSeq}${compName} is missing or empty`, {
|
|
46
|
+
ancestors: [
|
|
47
|
+
...ancestors,
|
|
48
|
+
fieldNode,
|
|
49
|
+
repetition,
|
|
50
|
+
...component ? [component] : []
|
|
51
|
+
],
|
|
52
|
+
place: component?.position ?? repetition.position ?? fieldNode.position
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
export { hl7v2LintRequiredComponents as default };
|
|
59
|
+
|
|
60
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["// oxlint-disable-next-line no-unused-vars -- triggers VFile DataMap augmentation\nimport type { ProfileContext } from \"@glion/annotate-profile-context\";\nimport type { Field, FieldRepetition, Nodes, Root, Segment } from \"@glion/ast\";\nimport type { DatatypeDefinition } from \"@glion/profiles\";\nimport { SKIP, visit } from \"@glion/util-visit\";\nimport { isEmptyNode } from \"@glion/utils\";\nimport { lintRule } from \"unified-lint-rule\";\nimport type { VFile } from \"vfile\";\n\n/**\n * Lint rule that validates required components in composite datatype fields.\n *\n * For each field with a composite datatype, checks that all components\n * marked `required: true` in the datatype definition are present and\n * non-empty in every field repetition.\n *\n * Empty fields are skipped (the required-fields rule handles that).\n * Simple (non-composite) datatypes are skipped.\n * Segments without a known profile (e.g., Z-segments) are silently skipped.\n *\n * @example\n * ```typescript\n * unified().use(hl7v2LintRequiredComponents);\n * ```;\n */\nconst hl7v2LintRequiredComponents = lintRule<Root>(\n { origin: \"hl7v2-lint:required-components\" },\n (tree, file) => {\n const ctx = file.data.profile;\n if (!ctx) {\n return;\n }\n\n visit(tree, \"field\", (fieldNode, ancestors, info) => {\n // Check emptiness first — most fields in a message are empty,\n // so this avoids segment/profile lookups for the majority of fields\n if (isEmptyNode(fieldNode as Field)) {\n return SKIP;\n }\n\n const segment = ancestors.at(-1) as Segment | undefined;\n if (!segment || segment.type !== \"segment\") {\n return SKIP;\n }\n\n const fieldDef = ctx.fields.get(segment.name);\n if (!fieldDef) {\n return SKIP;\n }\n\n const fieldProfile = fieldDef.bySequence.get(info.sequence);\n if (!fieldProfile) {\n return SKIP;\n }\n\n const dtDef = ctx.datatypes.get(fieldProfile.datatype);\n if (\n !dtDef ||\n dtDef.kind !== \"composite\" ||\n dtDef.requiredSequences.size === 0\n ) {\n return SKIP;\n }\n\n for (const repetition of (fieldNode as Field).children) {\n checkRepetition(\n file,\n dtDef,\n repetition,\n segment,\n info.sequence,\n ancestors,\n fieldNode as Field\n );\n }\n\n return SKIP;\n });\n }\n);\n\n/** Check a single field repetition for missing required components. */\nfunction checkRepetition(\n file: VFile,\n dtDef: DatatypeDefinition,\n repetition: FieldRepetition,\n segment: Segment,\n sequence: number,\n ancestors: Nodes[],\n fieldNode: Field\n): void {\n for (const compSeq of dtDef.requiredSequences) {\n const component = repetition.children[compSeq - 1];\n const compHasValue =\n component?.children[0]?.value !== undefined &&\n component.children[0].value.length > 0;\n\n if (!compHasValue) {\n const compProfile = dtDef.componentsBySequence.get(compSeq);\n const compName = compProfile?.name ? ` (${compProfile.name})` : \"\";\n file.message(\n `Required component ${segment.name}-${sequence}.${compSeq}${compName} is missing or empty`,\n {\n ancestors: [\n ...ancestors,\n fieldNode,\n repetition,\n ...(component ? [component] : []),\n ],\n place:\n component?.position ?? repetition.position ?? fieldNode.position,\n }\n );\n }\n }\n}\n\nexport default hl7v2LintRequiredComponents;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAyBA,MAAM,8BAA8B,SAClC,EAAE,QAAQ,kCAAkC,GAC3C,MAAM,SAAS;CACd,MAAM,MAAM,KAAK,KAAK;AACtB,KAAI,CAAC,IACH;AAGF,OAAM,MAAM,UAAU,WAAW,WAAW,SAAS;AAGnD,MAAI,YAAY,UAAmB,CACjC,QAAO;EAGT,MAAM,UAAU,UAAU,GAAG,GAAG;AAChC,MAAI,CAAC,WAAW,QAAQ,SAAS,UAC/B,QAAO;EAGT,MAAM,WAAW,IAAI,OAAO,IAAI,QAAQ,KAAK;AAC7C,MAAI,CAAC,SACH,QAAO;EAGT,MAAM,eAAe,SAAS,WAAW,IAAI,KAAK,SAAS;AAC3D,MAAI,CAAC,aACH,QAAO;EAGT,MAAM,QAAQ,IAAI,UAAU,IAAI,aAAa,SAAS;AACtD,MACE,CAAC,SACD,MAAM,SAAS,eACf,MAAM,kBAAkB,SAAS,EAEjC,QAAO;AAGT,OAAK,MAAM,cAAe,UAAoB,SAC5C,iBACE,MACA,OACA,YACA,SACA,KAAK,UACL,WACA,UACD;AAGH,SAAO;GACP;EAEL;;AAGD,SAAS,gBACP,MACA,OACA,YACA,SACA,UACA,WACA,WACM;AACN,MAAK,MAAM,WAAW,MAAM,mBAAmB;EAC7C,MAAM,YAAY,WAAW,SAAS,UAAU;AAKhD,MAAI,EAHF,WAAW,SAAS,IAAI,UAAU,KAAA,KAClC,UAAU,SAAS,GAAG,MAAM,SAAS,IAEpB;GACjB,MAAM,cAAc,MAAM,qBAAqB,IAAI,QAAQ;GAC3D,MAAM,WAAW,aAAa,OAAO,KAAK,YAAY,KAAK,KAAK;AAChE,QAAK,QACH,sBAAsB,QAAQ,KAAK,GAAG,SAAS,GAAG,UAAU,SAAS,uBACrE;IACE,WAAW;KACT,GAAG;KACH;KACA;KACA,GAAI,YAAY,CAAC,UAAU,GAAG,EAAE;KACjC;IACD,OACE,WAAW,YAAY,WAAW,YAAY,UAAU;IAC3D,CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@glion/lint-profile-required-components",
|
|
3
|
+
"version": "0.14.1",
|
|
4
|
+
"description": "Lint rule to validate required components in composite datatype fields",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"health",
|
|
7
|
+
"healthcare",
|
|
8
|
+
"hl7",
|
|
9
|
+
"hl7v2",
|
|
10
|
+
"nodejs",
|
|
11
|
+
"typescript"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://glion.dev",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": {
|
|
16
|
+
"name": "Melek Somai",
|
|
17
|
+
"email": "melek@rethinkhealth.io"
|
|
18
|
+
},
|
|
19
|
+
"repository": "rethinkhealth/glion.git",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": "./dist/index.js"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"unified-lint-rule": "3.0.1",
|
|
33
|
+
"@glion/util-visit": "0.14.1",
|
|
34
|
+
"@glion/ast": "0.14.1",
|
|
35
|
+
"@glion/utils": "0.14.1"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/node": "^25.5.0",
|
|
39
|
+
"@vitest/coverage-v8": "4.1.2",
|
|
40
|
+
"tsdown": "0.21.7",
|
|
41
|
+
"typescript": "^5.9.3",
|
|
42
|
+
"unified": "^11.0.5",
|
|
43
|
+
"vfile": "^6.0.3",
|
|
44
|
+
"vitest": "4.1.2",
|
|
45
|
+
"@glion/annotate-profile-context": "0.14.1",
|
|
46
|
+
"@glion/builder": "0.14.1",
|
|
47
|
+
"@glion/profiles": "0.14.1",
|
|
48
|
+
"@glion/testing": "0.0.2",
|
|
49
|
+
"@glion/tsconfig": "0.0.1"
|
|
50
|
+
},
|
|
51
|
+
"engines": {
|
|
52
|
+
"node": ">=18"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"build": "tsdown && tsc --emitDeclarationOnly",
|
|
56
|
+
"check-types": "tsc --noEmit",
|
|
57
|
+
"bench": "vitest bench --run",
|
|
58
|
+
"test": "vitest run",
|
|
59
|
+
"test:coverage": "vitest run --coverage",
|
|
60
|
+
"test:watch": "vitest"
|
|
61
|
+
}
|
|
62
|
+
}
|