Skip to content

Commit

Permalink
fix type guard helpers, type non-existent properties as never
Browse files Browse the repository at this point in the history
  • Loading branch information
pokornyd committed Apr 8, 2024
1 parent ee2359f commit e6f3908
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/parser/parser-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ type DeliverObjectElementAttributes = {
'data-rel': 'component' | 'link';
'data-type': 'item';
'data-codename': string;
'data-id': undefined;
'data-id': never;
}

type ManagementObjectElementAttributes = {
'data-rel': undefined;
'data-type': 'item' | 'component';
'data-id': string;
'data-codename': undefined;
'data-codename': never;
}

export type AssetLinkElementAttributes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ const transformItem: TransformElementFunction<ObjectElementAttributes> = (node)
// data-codename reference is for DAPI, data-id for MAPI
const itemReference: Reference = {
_type: "reference",
_ref: node.attributes["data-codename"] ?? node.attributes["data-id"],
_ref: node.attributes["data-codename"] || node.attributes["data-id"],
};

/**
Expand All @@ -305,7 +305,7 @@ const transformItem: TransformElementFunction<ObjectElementAttributes> = (node)
* data-type is present in both DAPI and MAPI but differentiates
* only in the latter
*/
const modularContentType = (node.attributes["data-rel"] ?? node.attributes["data-type"]) as ModularContentType;
const modularContentType = node.attributes['data-rel'] ?? node.attributes['data-type'] as ModularContentType;

return [createComponentBlock(uid().toString(), itemReference, modularContentType)];
}
Expand Down
13 changes: 7 additions & 6 deletions src/utils/common-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DomHtmlNode, DomNode, DomTextNode, ItemLinkElementAttributes } from "../index.js";
import { DomHtmlNode, DomNode, DomTextNode, FigureElementAttributes, ItemLinkElementAttributes, ObjectElementAttributes } from "../index.js";

export const isOrderedListBlock = (node: DomHtmlNode): boolean =>
node.tagName === 'ol';
Expand Down Expand Up @@ -31,19 +31,20 @@ export const isElement = (node: DomNode): node is DomHtmlNode =>
node.type === 'tag';

/**
* Returns `true` if the node is a linked item node (`<object></object>`).
* Returns `true` if the node is a linked item node (`<object></object>`) and narrows type guard.
*/
export const isLinkedItem = (node: DomNode): boolean =>
export const isLinkedItem = (node: DomNode): node is DomHtmlNode<ObjectElementAttributes> =>
isElement(node) &&
node.tagName === 'object' &&
node.attributes['type'] === 'application/kenticocloud';

/**
* Returns `true` if the node is a rich text image node (`<figure></figure>`).
* Returns `true` if the node is a rich text image node (`<figure></figure>`) and narrows type guard.
*/
export const isImage = (node: DomNode): boolean =>
export const isImage = (node: DomNode): node is DomHtmlNode<FigureElementAttributes> =>
isElement(node) &&
node.tagName === 'figure' &&
node.attributes['data-image-id'] ? true : false;
node.attributes['data-asset-id'] !== undefined;

/**
* Returns `true` if the node is a link to a content item and narrows type guard.
Expand Down

0 comments on commit e6f3908

Please sign in to comment.