mirror of
https://github.com/morluto/rea
synced 2026-10-27 14:42:18 -04:00
refactor(javascript): group domain semantics and contracts (#907)
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import * as t from "@babel/types";
|
||||
|
||||
/** Read an identifier or literal property name without evaluating syntax. */
|
||||
export const propertyName = (node: t.Node): string => {
|
||||
if (t.isIdentifier(node)) return node.name;
|
||||
if (t.isStringLiteral(node) || t.isNumericLiteral(node))
|
||||
return String(node.value);
|
||||
return "";
|
||||
};
|
||||
|
||||
/** Read a property name only when its syntax commits to one exact key. */
|
||||
export const semanticStaticPropertyName = (
|
||||
property: t.Node,
|
||||
computed: boolean,
|
||||
): string => semanticStaticPropertyKey(property, computed) ?? "";
|
||||
|
||||
/** Read an exact property key, preserving the legal empty-string key. */
|
||||
export const semanticStaticPropertyKey = (
|
||||
property: t.Node,
|
||||
computed: boolean,
|
||||
): string | null => {
|
||||
if (t.isStringLiteral(property) || t.isNumericLiteral(property))
|
||||
return String(property.value);
|
||||
return !computed && t.isIdentifier(property) ? property.name : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Display an exact JavaScript string as nonempty label text. Graph labels and
|
||||
* artifact-local keys are nonempty, so the legal empty value is shown as `""`;
|
||||
* producers keep the exact value in their properties.
|
||||
*/
|
||||
export const javascriptDisplayText = (value: string): string =>
|
||||
value === "" ? '""' : value;
|
||||
Reference in New Issue
Block a user