Skip to content

Commit

Permalink
feat: improve parse options & file.message and fileData type namings …
Browse files Browse the repository at this point in the history
…and export (#70)

* fix: remove options.warningMessage

* feat: add position and sourceId vfile message info

* feat: rename FileData into ParseFileData

* feat: rename FileData into StringifyFileData & use it in relottie
  • Loading branch information
Aidosmf committed Nov 27, 2023
1 parent 47e2dee commit a8e9a62
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 61 deletions.
5 changes: 5 additions & 0 deletions .changeset/lucky-needles-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lottiefiles/relottie-parse": patch
---

feat: do not duplicate log messages with the vfile.messages and remove the unnecessary "warningMessage" option
5 changes: 5 additions & 0 deletions .changeset/many-mice-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lottiefiles/relottie": patch
---

feat: FileData extends StringifyFileData type
6 changes: 6 additions & 0 deletions .changeset/ninety-queens-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@lottiefiles/relottie": patch
"@lottiefiles/relottie-parse": patch
---

feat: rename export relottie-parse's FileData type into ParseFileData
5 changes: 5 additions & 0 deletions .changeset/shaggy-islands-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lottiefiles/relottie-parse": patch
---

feat: add position and sourceId info when adding a vfile message
5 changes: 5 additions & 0 deletions .changeset/tender-mirrors-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lottiefiles/relottie-stringify": patch
---

feat: rename export FileData type into StringifyFileData
6 changes: 3 additions & 3 deletions packages/relottie-parse/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { readFileSync } from 'fs';
import type { Root } from '@lottiefiles/last';
import type { CompilerFunction, Plugin } from 'unified';
import { unified } from 'unified';
// import { reporter } from 'vfile-reporter';
import { reporter } from 'vfile-reporter';

import relottieParse from './src/index.js';

Expand All @@ -28,15 +28,15 @@ const path = `../../__fixtures__/features/${name}.json`;

const lottieJsonFile = readFileSync(path, 'utf8');

const processor = unified().use(relottieParse, { warningMessage: true }).use(emptyCompiler);
const processor = unified().use(relottieParse).use(emptyCompiler);

// const tree = processor.parse(lottieJsonFile);

// writeFileSync(`${name}-tree.json`, JSON.stringify(tree, null, 2), 'utf8');

const vfile = processor.processSync(lottieJsonFile);

// console.log(reporter(vfile));
console.log(reporter(vfile));

console.log(vfile.data);

Expand Down
4 changes: 4 additions & 0 deletions packages/relottie-parse/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import type {

const { number: NT, string: ST } = TITLES;

export const fileConstants = {
sourceId: 'relottie-parse',
} as const;

export type ConstantNumMap = Map<number, string>;

export const blendModeValues: ConstantNumMap = new Map<BlendMode.Value, string>([
Expand Down
3 changes: 2 additions & 1 deletion packages/relottie-parse/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright 2022 Design Barn Inc.
*/

export { default, type Options, type FileData } from './unified-relottie-parse.js';
export { default, type Options } from './unified-relottie-parse.js';
export type { ParseFileData } from './parse.js';
export { DEFAULT_OPTIONS } from './options.js';
export { Stack } from './helpers.js';
5 changes: 0 additions & 5 deletions packages/relottie-parse/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ export interface ParseOptions {
* Include 'valueType' prop into nodes (default, true)
*/
valueType: boolean;
/**
* include 'warning' type messages into vfile.data (default, false)
*/
warningMessage: boolean;
}

export const DEFAULT_OPTIONS: ParseOptions = {
position: true,
valueType: true,
warningMessage: false,
} as const;
55 changes: 13 additions & 42 deletions packages/relottie-parse/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import type { PrimitiveParts } from '@lottiefiles/last-builder';
import { is } from 'unist-util-is';
import type { VFile, Data } from 'vfile';

import { fileConstants } from './constants.js';
import type { Dependent } from './entities.js';
import { getMemberEntity, getNoKeyEntity } from './entities.js';
import { Stack } from './helpers.js';
Expand All @@ -46,21 +47,13 @@ import type { ParseOptions } from './options.js';
import type { SettingsOptions } from './unified-relottie-parse.js';

export interface ParseFileData extends Data {
parse: {
messages?: VFile['messages'];
};
parse?: object;
}

export interface Info {
hasExpressions: Root['hasExpressions'];
}

const addWarningMessage = (file: VFile, message: string, options: ParseOptions): void => {
if (options.warningMessage) {
file.message(message);
}
};

const createValueType = (node: Momoa.AstNode, options: ParseOptions): PrimitiveParts<PrimitiveValueType> => {
if (!options.valueType || node.type === 'Array' || node.type === 'Object' || node.type === 'Document') {
return {};
Expand Down Expand Up @@ -157,7 +150,6 @@ const getTitleFromMemberValue = (
parentNodeTitle: ParentTitle,
dependent: Dependent,
file: VFile,
options: ParseOptions,
): AnyTitle | undefined => {
const { key, parentTitle, type } = dependent;

Expand All @@ -178,7 +170,7 @@ const getTitleFromMemberValue = (
if (!constTitle) {
const message = `[${parentNodeTitle}] '${constantKey}' is missing in "dependent.parentTitle.values"`;

addWarningMessage(file, message, options);
file.message(message, node, fileConstants.sourceId);
}

const title = typeof constTitle === 'undefined' ? defaultConstTitle : constTitle;
Expand All @@ -198,7 +190,7 @@ const getTitleFromMemberValue = (
if (type !== node.type) {
const message = `${parentNodeTitle}'s '${key}' type is ${node.type} but has to be ${type}`;

addWarningMessage(file, message, options);
file.message(message, node, fileConstants.sourceId);
break;
}

Expand All @@ -213,7 +205,6 @@ const getDependentTitle = (
members: Momoa.Member[],
dependents: Dependent[],
file: VFile,
options: ParseOptions,
): AnyTitle | undefined => {
const memberKeyValue = members.reduce((acc, member) => {
const key = member.name.value;
Expand All @@ -229,37 +220,27 @@ const getDependentTitle = (

if (!node) continue;

const title = getTitleFromMemberValue(node, parentTitle, dependent, file, options);
const title = getTitleFromMemberValue(node, parentTitle, dependent, file);

if (title) return title;
}

return undefined;
};

const getObjectNodeTitle = (
node: Momoa.Obj,
parentNodeTitle: ParentTitle,
file: VFile,
options: ParseOptions,
): ObjectTitle => {
const getObjectNodeTitle = (node: Momoa.Obj, parentNodeTitle: ParentTitle, file: VFile): ObjectTitle => {
const entity = getNoKeyEntity(node, parentNodeTitle);

const { defaultTitle, dependents } = entity;

if (!dependents) return defaultTitle as ObjectTitle;

const title = getDependentTitle(parentNodeTitle, node.members, dependents, file, options);
const title = getDependentTitle(parentNodeTitle, node.members, dependents, file);

return (title || defaultTitle) as ObjectTitle;
};

const getArrayNodeTitle = (
node: Momoa.Arr,
parentNodeTitle: ParentTitle,
file: VFile,
options: ParseOptions,
): ArrayTitle => {
const getArrayNodeTitle = (node: Momoa.Arr, parentNodeTitle: ParentTitle, file: VFile): ArrayTitle => {
const entity = getNoKeyEntity(node, parentNodeTitle);

const { defaultTitle, dependents } = entity;
Expand All @@ -268,7 +249,7 @@ const getArrayNodeTitle = (

const members = getMembersFromArrNode(node);

const title = getDependentTitle(parentNodeTitle, members, dependents, file, options);
const title = getDependentTitle(parentNodeTitle, members, dependents, file);

return (title || defaultTitle) as ArrayTitle;
};
Expand Down Expand Up @@ -307,7 +288,7 @@ const traverseJsonEnter = (
const element = stack.peek();

assertNodeType<Element>(element, 'element', file);
const elementValueTitle = getObjectNodeTitle(node, element.title, file, options);
const elementValueTitle = getObjectNodeTitle(node, element.title, file);

stack.push(objectNode(elementValueTitle, [], { ...position }));
break;
Expand All @@ -316,7 +297,7 @@ const traverseJsonEnter = (
const array = stack.peek();

assertNodeType<ArrayNode>(array, 'array', file);
const objectTitle = getObjectNodeTitle(node, array.title, file, options);
const objectTitle = getObjectNodeTitle(node, array.title, file);

stack.push(objectNode(objectTitle, [], { ...position }));
break;
Expand All @@ -332,7 +313,7 @@ const traverseJsonEnter = (
const collection = stack.peek();

assertNodeType<Collection>(collection, 'collection', file);
const collectionValueTitle = getArrayNodeTitle(node, collection.title, file, options);
const collectionValueTitle = getArrayNodeTitle(node, collection.title, file);

stack.push(arrayNode(collectionValueTitle, [], { ...position }));
break;
Expand All @@ -341,7 +322,7 @@ const traverseJsonEnter = (
const array = stack.peek();

assertNodeType<ArrayNode>(array, 'array', file);
const arrayTitle = getArrayNodeTitle(node, array.title, file, options);
const arrayTitle = getArrayNodeTitle(node, array.title, file);

stack.push(arrayNode(arrayTitle, [], { ...position }));
break;
Expand Down Expand Up @@ -545,16 +526,6 @@ export function parse(document: string, file: VFile, settings: SettingsOptions =
},
});

const dataMessages = options.warningMessage && file.messages.length > 0 ? { messages: file.messages } : {};

const fileData: ParseFileData = {
parse: {
...dataMessages,
},
};

Object.assign(file.data, fileData);

const tree = stack.pop();

if (is<Root>(tree, 'root')) {
Expand Down
3 changes: 0 additions & 3 deletions packages/relottie-parse/src/unified-relottie-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type { Root } from '@lottiefiles/last';
import type { ParserFunction, Plugin } from 'unified';

import type { ParseOptions } from './options.js';
import type { ParseFileData } from './parse.js';
import { parse } from './parse.js';

export type Options = Partial<ParseOptions>;
Expand All @@ -18,8 +17,6 @@ export interface SettingsOptions extends Record<string, unknown> {
parse?: Options;
}

export type FileData = Partial<ParseFileData>;

const relottieParse: Plugin<[Options?], string, Root> = function relottieParse(options: Options = {}): void {
const settings = (this.data('settings') || { parse: {} }) as SettingsOptions;

Expand Down
3 changes: 2 additions & 1 deletion packages/relottie-stringify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright 2022 Design Barn Inc.
*/

export { default, type Options, type FileData } from './unified-relottie-stringify.js';
export { default, type Options } from './unified-relottie-stringify.js';
export type { StringifyFileData } from './stringify.js';
export * from './helpers.js';
export { DEFAULT_OPTIONS } from './options.js';
3 changes: 0 additions & 3 deletions packages/relottie-stringify/src/unified-relottie-stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type { Root } from '@lottiefiles/last';
import type { Plugin, CompilerFunction } from 'unified';

import type { StringifyOptions } from './options.js';
import type { StringifyFileData } from './stringify';
import { stringify } from './stringify.js';

export type Options = Partial<StringifyOptions>;
Expand All @@ -18,8 +17,6 @@ export interface SettingsOptions extends Record<string, unknown> {
stringify?: Options;
}

export type FileData = Partial<StringifyFileData>;

const relottieStringify: Plugin<[Options?], Root, string> = function relottieStringify(options: Options = {}) {
const settings = (this.data('settings') || { stringify: {} }) as SettingsOptions;

Expand Down
6 changes: 3 additions & 3 deletions packages/relottie/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
*/

import type { Root } from '@lottiefiles/last';
import parse, { type Options as ParseOptions, type FileData as ParseFileData } from '@lottiefiles/relottie-parse';
import stringify, { type Options as StringifyOptions } from '@lottiefiles/relottie-stringify';
import parse, { type Options as ParseOptions, type ParseFileData } from '@lottiefiles/relottie-parse';
import stringify, { type Options as StringifyOptions, type StringifyFileData } from '@lottiefiles/relottie-stringify';
import { unified, type FrozenProcessor } from 'unified';

export type Options = Partial<{
parse: ParseOptions;
stringify: StringifyOptions;
}>;

export interface FileData extends ParseFileData {}
export interface FileData extends ParseFileData, StringifyFileData {}

export const relottie: FrozenProcessor<Root, Root, Root, string> = unified().use(parse).use(stringify).freeze();

0 comments on commit a8e9a62

Please sign in to comment.