Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to TypeScript / Add storybook #68

Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
4964332
feat: update dependencies
florianstancioiu Mar 26, 2024
3431083
feat: add storybook and typescript deps
florianstancioiu Mar 26, 2024
b224e05
feat: add tsconfig files
florianstancioiu Mar 26, 2024
ee1b7f1
feat: update lint script & add default export
florianstancioiu Mar 26, 2024
7547e7b
feat: update .gitignore file
florianstancioiu Mar 26, 2024
3a2d781
feat: use .ts extension for tests and main file
florianstancioiu Mar 26, 2024
b72aa6a
feat: add types to TS files (src & test)
florianstancioiu Mar 26, 2024
f89e356
feat: update treenode with advice from review
florianstancioiu Mar 27, 2024
d3fb820
feat: remove unneeded dependencies
florianstancioiu Mar 27, 2024
205ef84
feat: use Tree instead of DefaultTree in test files
florianstancioiu Mar 27, 2024
93004f7
feat: add stories to the project
florianstancioiu Mar 27, 2024
421a2a9
feat: remove ownerTree from args
florianstancioiu Mar 27, 2024
4899443
fix: use camelCase property names
florianstancioiu Mar 28, 2024
3e67a87
feat: add AlternateTemplateWithChildren story
florianstancioiu Mar 28, 2024
962f3cf
feat: pass all TS checks in src file
florianstancioiu Mar 28, 2024
e283208
fix: rename Displaying to Display in stories
florianstancioiu Mar 28, 2024
1cd63bf
fix: eliminate the TS errors
florianstancioiu Mar 28, 2024
4aa9bfd
feat: use children args to improve stories
florianstancioiu Mar 28, 2024
4c0faca
feat: wip - convert stories to typescript
florianstancioiu Mar 28, 2024
83c32f5
fix: make the stories file pass TS checks
florianstancioiu Mar 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ debug.log
yarn-error.log
.DS_Store
.eslintcache
dist/
storybook-static/
demo-storybook/
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"eslint.validate": [ "javascript" ],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
}
}
16,832 changes: 13,968 additions & 2,864 deletions package-lock.json

Large diffs are not rendered by default.

45 changes: 36 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@
"test": "test"
},
"scripts": {
"lint": "eslint --cache --ext .js .",
"lint": "tsc && eslint --cache .",
"build": "tsc -p tsconfig.build.json",
"start": "wds",
"test": "wtr --coverage",
"test:watch": "wtr --watch",
"prepare": "husky install"
"prepare": "husky install",
"storybook:start": "storybook dev -p 8000",
"storybook:build": "storybook build",
"storybook:deploy": "storybook-to-ghpages",
"storybook:preview": "npm run storybook:build && http-server -d ./storybook-static/"
},
"release": {
"plugins": [
Expand All @@ -46,23 +51,45 @@
"@commitlint/config-conventional"
]
},
"files": [
"dist/"
],
"exports": {
".": "./dist/cosmoz-treenode.js",
"./cosmoz-treenode": "./dist/cosmoz-treenode.js",
"./cosmoz-treenode.js": "./dist/cosmoz-treenode.js"
},
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
"dependencies": {
"@neovici/cosmoz-tree": "^3.0.0",
"@pionjs/pion": "^2.0.0"
"@neovici/cosmoz-tree": "^3.6.0",
"@pionjs/pion": "^2.5.2"
},
"devDependencies": {
"@commitlint/cli": "^18.0.0",
"@commitlint/config-conventional": "^18.0.0",
"@neovici/cfg": "^1.15.2",
"@commitlint/cli": "^19.2.1",
"@commitlint/config-conventional": "^19.1.0",
"@neovici/cfg": "^1.50.0",
"@open-wc/testing": "^4.0.0",
"@semantic-release/changelog": "^6.0.0",
"@semantic-release/git": "^10.0.0",
"husky": "^8.0.0",
"semantic-release": "^22.0.0"
"@storybook/addon-essentials": "^7.0.0",
"@storybook/addon-links": "^7.0.0",
"@storybook/builder-vite": "7.6.17",
"@storybook/storybook-deployer": "^2.8.5",
"@storybook/web-components": "7.6.17",
"@types/mocha": "^10.0.6",
"@web/storybook-builder": "^0.1.9",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't @web/storybook-builder and @web/storybook-framework-web-components enough ? Do we need to add all the @storybook/* plugin to deps?

"@web/storybook-framework-web-components": "^0.1.1",
"esbuild": "^0.20.2",
"http-server": "^14.1.1",
"husky": "^9.0.11",
"rollup-plugin-esbuild": "^6.1.1",
"rollup-plugin-terser": "^7.0.2",
"semantic-release": "^23.0.6",
"storybook": "^7.6.17",
"typescript": "^5.0.0"
}
}
47 changes: 39 additions & 8 deletions cosmoz-treenode.js → src/cosmoz-treenode.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
import { component, html } from '@pionjs/pion';
import { Tree, Node } from '@neovici/cosmoz-tree/cosmoz-tree';

export const computePathToRender = (path, hideFromRoot, showMaxNodes) => {
export type ComputePathTextType = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not include the word Type in a type. We know its a type cause its TS.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this uses some properties from Options we can reuse. See https://www.typescriptlang.org/docs/handbook/utility-types.html#picktype-keys

Suggested change
export type ComputePathTextType = {
export interface ComputePathText extends Pick<Props, 'ownerTree'|'ellipsis'| ...> {
// add here own props that aren't the same
pathToRender?: string
}

ownerTree: Tree;
ellipsis: string;
pathToRender: string | undefined;
path: string;
valueProperty: string;
pathSeparator: string | undefined;
};

export type RenderType = {
title: string;
text: string;
};

export type TreenodeType = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Props

valueProperty: string;
pathSeparator: string;
hideFromRoot: number;
showMaxNodes: number;
keyProperty: string;
keyValue: string;
ownerTree: Tree;
ellipsis: string;
fallback: string;
};

export const computePathToRender = (
path?: string,
hideFromRoot?: number,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hideFromRoot and showMaxNodes can't be optional.
path also can't be optional but you can make it string|undefined.

showMaxNodes?: number,
) => {
if (!path) {
return;
}
let pathToRender = path;
if (hideFromRoot > 0 && path.length > hideFromRoot) {

Check failure on line 39 in src/cosmoz-treenode.ts

View workflow job for this annotation

GitHub Actions / build / build

'hideFromRoot' is possibly 'undefined'.

Check failure on line 39 in src/cosmoz-treenode.ts

View workflow job for this annotation

GitHub Actions / build / build

'hideFromRoot' is possibly 'undefined'.
pathToRender = path.slice(hideFromRoot);
}

if (showMaxNodes > 0 && pathToRender.length > showMaxNodes) {

Check failure on line 43 in src/cosmoz-treenode.ts

View workflow job for this annotation

GitHub Actions / build / build

'showMaxNodes' is possibly 'undefined'.

Check failure on line 43 in src/cosmoz-treenode.ts

View workflow job for this annotation

GitHub Actions / build / build

'showMaxNodes' is possibly 'undefined'.
pathToRender = path.slice(-showMaxNodes);

Check failure on line 44 in src/cosmoz-treenode.ts

View workflow job for this annotation

GitHub Actions / build / build

'showMaxNodes' is possibly 'undefined'.
}
return pathToRender;
},
Expand All @@ -20,7 +51,7 @@
* @param {Array} inputPath Array of path parts
* @returns {Array} Array with defined parts
*/
getKnownPath = (inputPath) => {
getKnownPath = (inputPath: (Node | undefined)[] | null) => {
let path = inputPath;
if (!Array.isArray(path) || path.length === 0) {
return path;
Expand All @@ -36,7 +67,7 @@
}
return path;
},
computePath = (ownerTree, keyProperty, keyValue) => {
computePath = (ownerTree: Tree, keyProperty: string, keyValue: string) => {
// HACK(pasleq): Cosmoz.Tree API needs to be fixed to avoid such code in components
let path = null;

Expand All @@ -62,13 +93,13 @@
path,
valueProperty,
pathSeparator,
}) => {
}: ComputePathTextType) => {
if (!pathToRender) {
return '';
}

const stringParts = pathToRender.map((node) =>

Check failure on line 101 in src/cosmoz-treenode.ts

View workflow job for this annotation

GitHub Actions / build / build

Property 'map' does not exist on type 'string'.

Check failure on line 101 in src/cosmoz-treenode.ts

View workflow job for this annotation

GitHub Actions / build / build

Parameter 'node' implicitly has an 'any' type.
ownerTree.getProperty(node, valueProperty)
ownerTree.getProperty(node, valueProperty),
);

let text = stringParts.join(pathSeparator);
Expand All @@ -78,7 +109,7 @@
}
return text;
},
render = ({ title, text }) => html`
render = ({ title, text }: RenderType) => html`
<style>
:host {
display: block;
Expand Down Expand Up @@ -111,10 +142,10 @@
ownerTree,
ellipsis = '… / ',
fallback,
}) => {
}: TreenodeType) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this i component I'd simply name this Props.

const path = computePath(ownerTree, keyProperty, keyValue);
if (!path) return render({ text: fallback, title: fallback });
const pathToRender = computePathToRender(path, hideFromRoot, showMaxNodes),

Check failure on line 148 in src/cosmoz-treenode.ts

View workflow job for this annotation

GitHub Actions / build / build

Argument of type '(Node | undefined)[]' is not assignable to parameter of type 'string'.
opts = {
ownerTree,
ellipsis,
Expand All @@ -123,13 +154,13 @@
pathSeparator,
};
return render({
text: computePathText({

Check failure on line 157 in src/cosmoz-treenode.ts

View workflow job for this annotation

GitHub Actions / build / build

Argument of type '{ pathToRender: string | undefined; ownerTree: Tree; ellipsis: string; path: (Node | undefined)[]; valueProperty: string; pathSeparator: string; }' is not assignable to parameter of type 'ComputePathTextType'.
...opts,
pathToRender,
}),
title: computePathText({
...opts,
pathToRender: path,

Check failure on line 163 in src/cosmoz-treenode.ts

View workflow job for this annotation

GitHub Actions / build / build

Type '(Node | undefined)[]' is not assignable to type 'string'.
}),
});
};
Expand All @@ -152,5 +183,5 @@
'show-max-nodes',
'fallback',
],
})
}),
);
22 changes: 11 additions & 11 deletions test/cosmoz-treenode.test.js → test/cosmoz-treenode.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { html, fixture, expect, elementUpdated } from '@open-wc/testing';
import { DefaultTree } from '@neovici/cosmoz-tree/cosmoz-default-tree';
import { computePath, computePathToRender } from '../cosmoz-treenode.js';
import { computePath, computePathToRender } from '../src/cosmoz-treenode';

const treeBaseUrl = '/node_modules/@neovici/cosmoz-tree/test/data',
basicTreeUrl = `${treeBaseUrl}/basicTree.json`,
Expand All @@ -12,7 +12,7 @@ const treeBaseUrl = '/node_modules/@neovici/cosmoz-tree/test/data',
};

suite('basic', () => {
let basicFixture, basicTree;
let basicFixture, basicTree: DefaultTree;

suiteSetup(async () => {
basicTree = await treeFromJsonUrl(basicTreeUrl);
Expand All @@ -35,17 +35,17 @@ suite('basic', () => {
test('computePath', () => {
expect(computePath()).to.equal(undefined);
expect(
computePath(basicTree, 'id', '11111111-1111-1111-1111-111111111111')
computePath(basicTree, 'id', '11111111-1111-1111-1111-111111111111'),
).deep.equal(basicTree.getPathNodes('1'));
expect(
computePath(basicTree, 'id', '3a7654f1-e3e6-49c7-b6a8-a4fb00f31245')
computePath(basicTree, 'id', '3a7654f1-e3e6-49c7-b6a8-a4fb00f31245'),
).deep.equal(basicTree.getPathNodes('1.2.3'));
expect(
computePath(
new DefaultTree({}),
'id',
'11111111-1111-1111-1111-111111111111'
)
'11111111-1111-1111-1111-111111111111',
),
).to.equal(null);
});

Expand All @@ -65,7 +65,7 @@ suite('basic', () => {
const textContent =
basicFixture.shadowRoot.querySelector('span').textContent;
expect(textContent).to.include(
['Root', 'Node2', 'Node3', 'Node301'].join(' / ')
['Root', 'Node2', 'Node3', 'Node301'].join(' / '),
);
});

Expand All @@ -76,13 +76,13 @@ suite('basic', () => {
const textContent =
basicFixture.shadowRoot.querySelector('span').textContent;
expect(textContent).to.include(
['Root', 'Node2', 'Node3', 'Node301'].join(customSep)
['Root', 'Node2', 'Node3', 'Node301'].join(customSep),
);
});
});

suite('lookupNodeById', () => {
let basicFixture, basicTree;
let basicFixture, basicTree: DefaultTree;

suiteSetup(async () => {
basicTree = await treeFromJsonUrl(basicTreeUrl);
Expand All @@ -107,7 +107,7 @@ suite('lookupNodeById', () => {
});

suite('multiRoot', () => {
let multiRootFixture, multiRootTree;
let multiRootFixture, multiRootTree: DefaultTree;

suiteSetup(async () => {
multiRootTree = await treeFromJsonUrl(multiRootTreeUrl);
Expand All @@ -132,7 +132,7 @@ suite('multiRoot', () => {
});

suite('missingAncestor', () => {
let missingAncestorFixture, missingAncestorTree;
let missingAncestorFixture, missingAncestorTree: DefaultTree;

suiteSetup(async () => {
missingAncestorTree = await treeFromJsonUrl(missingAncestorTreeUrl);
Expand Down
11 changes: 11 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"declaration": true,
"noEmit": false
},
"include": [
"src"
]
}
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"noEmit": true,
"module": "esnext",
"moduleResolution": "bundler",
"strict": true,
"target": "esnext",
"allowJs": true,
"types": [
"node",
"mocha"
]
}
}