Skip to content

Commit

Permalink
add formatting and linting to rc package
Browse files Browse the repository at this point in the history
  • Loading branch information
zaviermiller committed Jan 5, 2024
1 parent 5330f51 commit abe75d6
Show file tree
Hide file tree
Showing 14 changed files with 674 additions and 114 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
}
5 changes: 0 additions & 5 deletions packages/receipt-components/.babelrc

This file was deleted.

3 changes: 2 additions & 1 deletion packages/receipt-components/.eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist/*
dist
node_modules
15 changes: 6 additions & 9 deletions packages/receipt-components/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ module.exports = {
node: true,
jest: true,
},
extends: ['eslint:recommended', 'prettier'],
extends: [
"eslint:recommended",
"prettier, plugin:@typescript-eslint/recommended",
],
parserOptions: {
ecmaVersion: 12,
sourceType: 'module',
},
plugins: ['prettier'],
rules: {
indent: ['error', 2, { SwitchCase: 1 }],
'linebreak-style': ['error', 'unix'],
quotes: ['error', 'single'],
semi: ['error', 'always'],
sourceType: "module",
},
plugins: ["prettier", "@typescript-eslint"],
};
10 changes: 10 additions & 0 deletions packages/receipt-components/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": false,
"trailingComma": "es5",
"bracketSpacing": true,
"endOfLine": "lf",
"arrowParens": "always"
}
7 changes: 7 additions & 0 deletions packages/receipt-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"build": "tsup",
"preview": "vite preview",
"start": "vite build && node dist/main.js",
"lint": "eslint --ignore-path .eslintignore --ext .js,.ts && tsc",
"format": "prettier --ignore-path .eslintignore --write \"**/*.+(js|ts|json)\"",
"test": "vitest",
"test:unit": "vitest run",
"coverage": "vitest --coverage",
Expand All @@ -25,7 +27,12 @@
"devDependencies": {
"@resaleai/receipt-renderer": "workspace:*",
"@types/node": "^20.10.5",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"@vitest/coverage-v8": "^0.34.6",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"prettier": "^3.1.1",
"tsup": "^7.2.0",
"typescript": "^5.3.3",
"vite": "^4.5.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/receipt-components/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export class InvalidRendererError extends Error {
constructor(msg: string) {
super(msg);
this.name = 'InvalidRendererError';
this.name = "InvalidRendererError";
}
}
6 changes: 3 additions & 3 deletions packages/receipt-components/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import escPosRendererPlugin from '@resaleai/receipt-escpos-renderer';
import { ReceiptComponent } from './receipt-component';
import { RendererName } from '@resaleai/receipt-plugin';
import escPosRendererPlugin from "@resaleai/receipt-escpos-renderer";
import { ReceiptComponent } from "./receipt-component";
import { RendererName } from "@resaleai/receipt-plugin";

declare global {
interface RendererMap {
Expand Down
21 changes: 13 additions & 8 deletions packages/receipt-components/src/receipt-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import {
ReceiptASTNodeRegistry,
nodeRegistry,
parseTemplateForAst,
} from '@resaleai/receipt-ast';
} from "@resaleai/receipt-ast";
import {
RCNodePlugin,
RCRendererPlugin,
RendererName,
} from '@resaleai/receipt-plugin';
import { InvalidRendererError } from './errors';
} from "@resaleai/receipt-plugin";
import { InvalidRendererError } from "./errors";

interface ReceiptComponentOptions<TProps> {
render: (props: TProps) => string; // render function should return a template
Expand Down Expand Up @@ -63,7 +63,10 @@ export class ReceiptComponent<TProps> {
// iterate through all node plugins and register render functions
this.nodePlugins.forEach((plugin) => {
if (plugin.renderers[renderer.name]) {
renderer.registerRenderFunc(plugin.name, plugin.renderers[renderer.name]);
renderer.registerRenderFunc(
plugin.name,
plugin.renderers[renderer.name]
);
}
});
}
Expand All @@ -73,13 +76,13 @@ export class ReceiptComponent<TProps> {
nodes.forEach((node) => {
// warn when node is already registered, as this may cause unexpected behavior
if (this.astBuilders[node.name]) {
console.warn('Node already registered', node.name);
console.warn("Node already registered", node.name);
}
this.astBuilders[node.name] = node.buildNode;
this.nodePlugins.push(node);
for (const name of node.aliases ?? []) {
if (this.astBuilders[name]) {
console.warn('Node already registered', name);
console.warn("Node already registered", name);
}
this.astBuilders[name] = node.buildNode;
}
Expand All @@ -96,10 +99,12 @@ export class ReceiptComponent<TProps> {

static use(plugins: (RCNodePlugin<any> | RCRendererPlugin)[]) {
const nodes = plugins.filter(
(plugin): plugin is RCNodePlugin<any> => (plugin as RCNodePlugin<any>).buildNode !== undefined
(plugin): plugin is RCNodePlugin<any> =>
(plugin as RCNodePlugin<any>).buildNode !== undefined
);
const renderers = plugins.filter(
(plugin): plugin is RCRendererPlugin => (plugin as RCRendererPlugin).renderer !== undefined
(plugin): plugin is RCRendererPlugin =>
(plugin as RCRendererPlugin).renderer !== undefined
);
this.registerNodes(nodes);
renderers.forEach((renderer) => this.registerRenderer(renderer));
Expand Down
26 changes: 13 additions & 13 deletions packages/receipt-components/test/helpers/dummies.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { ReceiptComponent } from '@/receipt-component';
import { RCNodePlugin, RCRendererPlugin } from '@resaleai/receipt-plugin';
import { ReceiptComponent } from "@/receipt-component";
import { RCNodePlugin, RCRendererPlugin } from "@resaleai/receipt-plugin";
import {
CoreReceiptRenderer,
buildReceiptRenderer,
} from '@resaleai/receipt-renderer';
import { vi } from 'vitest';
} from "@resaleai/receipt-renderer";
import { vi } from "vitest";

export const dummyNodePlugin: RCNodePlugin<null> = {
name: 'dummyNode',
buildNode: vi.fn(() => ({ name: 'dummy', props: null })),
name: "dummyNode",
buildNode: vi.fn(() => ({ name: "dummy", props: null })),
renderers: {
dummyRenderer: vi.fn(async (props: null) => ({})),
},
};
export const dummyNodePluginWithAlias: RCNodePlugin<null> = {
name: 'dummyNode',
aliases: ['dmy'],
buildNode: () => ({ name: 'dummy', props: null }),
name: "dummyNode",
aliases: ["dmy"],
buildNode: () => ({ name: "dummy", props: null }),
renderers: {
dummyRenderer: vi.fn(async (props: null) => ({})),
},
Expand All @@ -25,14 +25,14 @@ export const dummyNodePluginWithAlias: RCNodePlugin<null> = {
export const [dummyRenderFuncRegistry, dummyRegisterFunc] =
buildReceiptRenderer({} as CoreReceiptRenderer<any>);

export const dummyRenderer = vi.fn((ast: any) => '' as any);
export const dummyRenderer = vi.fn((ast: any) => "" as any);

export const dummyRendererPlugin: RCRendererPlugin = {
name: 'dummyRenderer',
name: "dummyRenderer",
renderer: dummyRenderer,
registerRenderFunc: dummyRegisterFunc,
};

export const dummyComponent = new ReceiptComponent('dummyComponent', {
render: vi.fn(() => ''),
export const dummyComponent = new ReceiptComponent("dummyComponent", {
render: vi.fn(() => ""),
});
Loading

0 comments on commit abe75d6

Please sign in to comment.