Skip to content

Commit

Permalink
refactor(riot): migrate to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetanmaisse committed Jan 8, 2021
1 parent 9b37d68 commit 13c190b
Show file tree
Hide file tree
Showing 20 changed files with 114 additions and 71 deletions.
9 changes: 5 additions & 4 deletions app/riot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@
"ts-dedent": "^2.0.0"
},
"devDependencies": {
"@babel/plugin-transform-modules-commonjs": "^7.12.1",
"@babel/preset-env": "^7.12.1",
"@babel/preset-flow": "^7.12.1",
"@babel/preset-react": "^7.12.1"
"@types/riot": "^3.6.2",
"riot": "^3.13.2",
"riot-compiler": "^3.6.0",
"riot-hot-reload": "^1.0.0",
"riot-tag-loader": "^2.1.0"
},
"peerDependencies": {
"@babel/core": "*",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import compiler from 'riot-compiler';

export const alreadyCompiledMarker = "var riot = require('riot')";

export function asCompiledCode(text) {
export function asCompiledCode(text: string) {
return compiler
.compile(text, {})
.replace('var riot = require("riot")', '')
.replace('riot.tag2(', 'tag2(');
}

export function compileNow(tag2, text) {
export function compileNow(tag2: unknown, text: string) {
// eslint-disable-next-line no-eval
return tag2 && eval(asCompiledCode(text));
}

export function getRidOfRiotNoise(compiled) {
export function getRidOfRiotNoise(compiled: string) {
return compiled.replace(/riot\.tag2/g, 'tag2').replace(alreadyCompiledMarker, '');
}

export function setConstructor(compiledSourceCode, constructor) {
export function setConstructor(compiledSourceCode: string, constructor: any) {
return compiledSourceCode.replace(/function\(opts\)\s*{\s*}(?=\);$)/, constructor.toString());
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ export const {
} = clientApi;

const framework = 'riot';
export const storiesOf = (...args) => clientApi.storiesOf(...args).addParameters({ framework });
export const configure = (...args) => coreConfigure(framework, ...args);
export const storiesOf = (...args: any) =>
clientApi.storiesOf(...args).addParameters({ framework });
export const configure = (...args: any) => coreConfigure(framework, ...args);

const mount = vendorMount.bind(riot, '#root');
const compileNow = unboundCompileNow.bind(null, tag2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@ beforeEach(() => {

describe('render a riot element', () => {
it('should not work with nothing', () => {
// `render` should only have 1 argument but in every test, there is a magic context given too… 🤷
// @ts-expect-error
expect(render(null, context)).toBe(false);

expect(rootElement.innerHTML).toEqual('');
});

it('can work with some text', () => {
// @ts-expect-error
expect(render({ tags: ['<div><p>some tests</p></div>'] }, context)).toBe(true);

expect(rootElement.innerHTML).toEqual('<div><p>some tests</p></div>');
});

it('can work with raw code', () => {
// @ts-expect-error
expect(render("riot.tag2('root', '<div>raw code</div>', '', '', () => {})", context)).toBe(
true
);
Expand All @@ -43,6 +47,7 @@ describe('render a riot element', () => {
});

it('can work with compiled code', () => {
// @ts-expect-error
expect(render([{}], context)).toBe(true);
// does only work in true mode, and not in jest mode
});
Expand All @@ -58,6 +63,7 @@ describe('render a riot element', () => {
it('works with a json consisting in a tagName and opts', () => {
tag2('hello', '<p>Hello { opts.suffix }</p>', '', '', () => {});

// @ts-expect-error
expect(render({ tagName: 'hello', opts: { suffix: 'World' } }, context)).toBe(true);

expect(rootElement.innerHTML).toEqual('<p>Hello World</p>');
Expand All @@ -76,6 +82,7 @@ describe('render a riot element', () => {
],
template: '<Matriochka><div><Tag1>Content</Tag1></div></Matriochka>',
},
// @ts-expect-error
context
)
).toBe(true);
Expand All @@ -97,6 +104,7 @@ describe('render a riot element', () => {
template:
'<SimpleTest test={ "with a parameter" } value={"value is mapped to riotValue"}></SimpleTest>',
},
// @ts-expect-error
context
)
).toBe(true);
Expand All @@ -121,6 +129,7 @@ describe('render a riot element', () => {
this.hacked = true;
},
},
// @ts-expect-error
context
)
).toBe(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export default function renderMain({
name,
showMain = () => {},
showError = () => {},
}: {
storyFn: Function;
kind: string;
name: string;
showMain: () => any;
showError: (input: { title: string; description: string }) => void;
}) {
showMain();
unregister('#root');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mount } from 'riot';

export default function renderCompiledButUnmounted(component) {
export default function renderCompiledButUnmounted(component: any) {
mount('root', component.tagName, component.opts || {});
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import renderCompiledButUnmounted from './compiledButUnmounted';
import renderStringified from './stringified';
import renderRaw from './raw';

export function render(component) {
export function render(component: any) {
if (typeof component === 'string') {
renderRaw(component);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { mount, tag2 as tag } from 'riot';
import compiler from 'riot-compiler';
import { alreadyCompiledMarker, getRidOfRiotNoise } from '../compileStageFunctions';

export default function renderRaw(sourceCode) {
export default function renderRaw(sourceCode: string) {
const tag2 = tag;
// eslint-disable-next-line no-eval
eval(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import compiler from 'riot-compiler';
import { document } from 'global';
import { alreadyCompiledMarker, getRidOfRiotNoise, setConstructor } from '../compileStageFunctions';

function guessRootName(stringified) {
function guessRootName(stringified: string) {
const whiteSpaceLocation = stringified.indexOf(' ', stringified.indexOf('<') + 1);
const firstWhitespace = whiteSpaceLocation === -1 ? stringified.length : whiteSpaceLocation;
const supposedName = stringified.trim().match(/^<[^ >]+\/>$/)
Expand All @@ -17,7 +17,7 @@ function guessRootName(stringified) {
return matchingBuiltInTag === 'HTMLUnknownElement' ? supposedName : 'root';
}

function compileText(code, rootName) {
function compileText(code: string, rootName: string) {
const sourceCodeEndOfHtml =
(Math.min(code.indexOf('<style') + 1, code.indexOf('<script') + 1) || code.length + 1) - 1;
const sourceCodeReformatted =
Expand All @@ -32,6 +32,10 @@ export default function renderStringified({
tags,
template = `<${(tags[0] || []).boundAs || guessRootName(tags[0] || '')}/>`,
tagConstructor,
}: {
tags: any[];
template: string;
tagConstructor: any;
}) {
const tag2 = tag;
tags.forEach((input) => {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export function webpack(config) {
import { Configuration } from 'webpack';

export function webpack(config: Configuration) {
return {
...config,
module: {
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions app/riot/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare module '@storybook/core/*';
declare module 'global';
declare module 'riot-compiler';
declare module 'riot' {
const tag2: Function;
const mount: Function;
const unregister: Function;
}
7 changes: 7 additions & 0 deletions app/riot/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"types": ["jest", "webpack-env"]
}
}
106 changes: 53 additions & 53 deletions lib/cli/src/versions.json
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
{
"@storybook/addon-a11y": "6.2.0-alpha.9",
"@storybook/addon-actions": "6.2.0-alpha.9",
"@storybook/addon-backgrounds": "6.2.0-alpha.9",
"@storybook/addon-controls": "6.2.0-alpha.9",
"@storybook/addon-cssresources": "6.2.0-alpha.9",
"@storybook/addon-design-assets": "6.2.0-alpha.9",
"@storybook/addon-docs": "6.2.0-alpha.9",
"@storybook/addon-essentials": "6.2.0-alpha.9",
"@storybook/addon-events": "6.2.0-alpha.9",
"@storybook/addon-google-analytics": "6.2.0-alpha.9",
"@storybook/addon-graphql": "6.2.0-alpha.9",
"@storybook/addon-jest": "6.2.0-alpha.9",
"@storybook/addon-knobs": "6.2.0-alpha.9",
"@storybook/addon-links": "6.2.0-alpha.9",
"@storybook/addon-queryparams": "6.2.0-alpha.9",
"@storybook/addon-storyshots": "6.2.0-alpha.9",
"@storybook/addon-storyshots-puppeteer": "6.2.0-alpha.9",
"@storybook/addon-storysource": "6.2.0-alpha.9",
"@storybook/addon-toolbars": "6.2.0-alpha.9",
"@storybook/addon-viewport": "6.2.0-alpha.9",
"@storybook/addons": "6.2.0-alpha.9",
"@storybook/angular": "6.2.0-alpha.9",
"@storybook/api": "6.2.0-alpha.9",
"@storybook/aurelia": "6.2.0-alpha.9",
"@storybook/channel-postmessage": "6.2.0-alpha.9",
"@storybook/channel-websocket": "6.2.0-alpha.9",
"@storybook/channels": "6.2.0-alpha.9",
"@storybook/cli": "6.2.0-alpha.9",
"@storybook/client-api": "6.2.0-alpha.9",
"@storybook/client-logger": "6.2.0-alpha.9",
"@storybook/codemod": "6.2.0-alpha.9",
"@storybook/components": "6.2.0-alpha.9",
"@storybook/core": "6.2.0-alpha.9",
"@storybook/core-events": "6.2.0-alpha.9",
"@storybook/ember": "6.2.0-alpha.9",
"@storybook/html": "6.2.0-alpha.9",
"@storybook/marionette": "6.2.0-alpha.9",
"@storybook/marko": "6.2.0-alpha.9",
"@storybook/mithril": "6.2.0-alpha.9",
"@storybook/node-logger": "6.2.0-alpha.9",
"@storybook/postinstall": "6.2.0-alpha.9",
"@storybook/preact": "6.2.0-alpha.9",
"@storybook/rax": "6.2.0-alpha.9",
"@storybook/react": "6.2.0-alpha.9",
"@storybook/riot": "6.2.0-alpha.9",
"@storybook/router": "6.2.0-alpha.9",
"@storybook/server": "6.2.0-alpha.9",
"@storybook/source-loader": "6.2.0-alpha.9",
"@storybook/svelte": "6.2.0-alpha.9",
"@storybook/theming": "6.2.0-alpha.9",
"@storybook/ui": "6.2.0-alpha.9",
"@storybook/vue": "6.2.0-alpha.9",
"@storybook/web-components": "6.2.0-alpha.9"
"@storybook/addon-a11y": "6.2.0-alpha.10",
"@storybook/addon-actions": "6.2.0-alpha.10",
"@storybook/addon-backgrounds": "6.2.0-alpha.10",
"@storybook/addon-controls": "6.2.0-alpha.10",
"@storybook/addon-cssresources": "6.2.0-alpha.10",
"@storybook/addon-design-assets": "6.2.0-alpha.10",
"@storybook/addon-docs": "6.2.0-alpha.10",
"@storybook/addon-essentials": "6.2.0-alpha.10",
"@storybook/addon-events": "6.2.0-alpha.10",
"@storybook/addon-google-analytics": "6.2.0-alpha.10",
"@storybook/addon-graphql": "6.2.0-alpha.10",
"@storybook/addon-jest": "6.2.0-alpha.10",
"@storybook/addon-knobs": "6.2.0-alpha.10",
"@storybook/addon-links": "6.2.0-alpha.10",
"@storybook/addon-queryparams": "6.2.0-alpha.10",
"@storybook/addon-storyshots": "6.2.0-alpha.10",
"@storybook/addon-storyshots-puppeteer": "6.2.0-alpha.10",
"@storybook/addon-storysource": "6.2.0-alpha.10",
"@storybook/addon-toolbars": "6.2.0-alpha.10",
"@storybook/addon-viewport": "6.2.0-alpha.10",
"@storybook/addons": "6.2.0-alpha.10",
"@storybook/angular": "6.2.0-alpha.10",
"@storybook/api": "6.2.0-alpha.10",
"@storybook/aurelia": "6.2.0-alpha.10",
"@storybook/channel-postmessage": "6.2.0-alpha.10",
"@storybook/channel-websocket": "6.2.0-alpha.10",
"@storybook/channels": "6.2.0-alpha.10",
"@storybook/cli": "6.2.0-alpha.10",
"@storybook/client-api": "6.2.0-alpha.10",
"@storybook/client-logger": "6.2.0-alpha.10",
"@storybook/codemod": "6.2.0-alpha.10",
"@storybook/components": "6.2.0-alpha.10",
"@storybook/core": "6.2.0-alpha.10",
"@storybook/core-events": "6.2.0-alpha.10",
"@storybook/ember": "6.2.0-alpha.10",
"@storybook/html": "6.2.0-alpha.10",
"@storybook/marionette": "6.2.0-alpha.10",
"@storybook/marko": "6.2.0-alpha.10",
"@storybook/mithril": "6.2.0-alpha.10",
"@storybook/node-logger": "6.2.0-alpha.10",
"@storybook/postinstall": "6.2.0-alpha.10",
"@storybook/preact": "6.2.0-alpha.10",
"@storybook/rax": "6.2.0-alpha.10",
"@storybook/react": "6.2.0-alpha.10",
"@storybook/riot": "6.2.0-alpha.10",
"@storybook/router": "6.2.0-alpha.10",
"@storybook/server": "6.2.0-alpha.10",
"@storybook/source-loader": "6.2.0-alpha.10",
"@storybook/svelte": "6.2.0-alpha.10",
"@storybook/theming": "6.2.0-alpha.10",
"@storybook/ui": "6.2.0-alpha.10",
"@storybook/vue": "6.2.0-alpha.10",
"@storybook/web-components": "6.2.0-alpha.10"
}
9 changes: 7 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5115,6 +5115,11 @@
"@types/glob" "*"
"@types/node" "*"

"@types/riot@^3.6.2":
version "3.6.2"
resolved "https://registry.yarnpkg.com/@types/riot/-/riot-3.6.2.tgz#d3e0f384ffb1d718f5fd89b8a14e7d60c7f6940c"
integrity sha512-e/q9dwOgGXeouq1ipQMxwuukYf8lMZbhzbuvFqX4XhdQHyf6Dn4pOxwZGkXSCJ+HZ8c79fgAzpACO92MqbTi2g==

"@types/selenium-webdriver@^3.0.0":
version "3.0.17"
resolved "https://registry.yarnpkg.com/@types/selenium-webdriver/-/selenium-webdriver-3.0.17.tgz#50bea0c3c2acc31c959c5b1e747798b3b3d06d4b"
Expand Down Expand Up @@ -28766,7 +28771,7 @@ riot-cli@^4.0.2:
riot-compiler "^3.5.1"
rollup "^0.57.1"

riot-compiler@^3.5.1, riot-compiler@^3.5.2:
riot-compiler@^3.5.1, riot-compiler@^3.5.2, riot-compiler@^3.6.0:
version "3.6.0"
resolved "https://registry.yarnpkg.com/riot-compiler/-/riot-compiler-3.6.0.tgz#f65428cf25b940098e35ea9c8357ca91cae10280"
integrity sha512-P2MVj0T9ox0EFPTSSHJIAaBe6/fCnKln76BtPK6ezAlBW2+qKCDzzvkj3gwFvGEG1dYUHa2jQ/O6+FZNNe8CYw==
Expand Down Expand Up @@ -28807,7 +28812,7 @@ riot-tmpl@^3.0.8:
dependencies:
eslint-config-riot "^1.0.0"

riot@^3.13.0, riot@^3.3.1:
riot@^3.13.0, riot@^3.13.2, riot@^3.3.1:
version "3.13.2"
resolved "https://registry.yarnpkg.com/riot/-/riot-3.13.2.tgz#3f5fb86b0d2b864bdcc959b7f86e2f4c2f696726"
integrity sha512-L4WFJEhbTA0deDoZ1XxoVw7Tf96+xYc06aQ+4QM+IkYClD6mZ7E/9zN3Rd48uYMBvHQfHtbPvR0KEiu3pJyI2A==
Expand Down

0 comments on commit 13c190b

Please sign in to comment.