Skip to content

Commit

Permalink
fix(lens): match default case
Browse files Browse the repository at this point in the history
  • Loading branch information
artalar committed Oct 11, 2023
1 parent 5299e6d commit 27061af
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 32 deletions.
2 changes: 1 addition & 1 deletion packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"sandbox": "vite",
"prepublishOnly": "npm run build && npm run test",
"build": "microbundle -f esm,cjs",
"test": "ts-node src/**/*.test.ts",
"test": "ts-node src/tests/index.ts",
"test:watch": "tsx watch src/**/*.test.ts"
},
"author": "pivaszbs",
Expand Down
10 changes: 1 addition & 9 deletions packages/eslint-plugin/src/rules/atom-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,13 @@ function validAtomVariable(node: CallExpression, correctName: string) {
return validateLiteral(node.arguments[1], correctName);
}

if (isTemplateLiteral(node.arguments[1])) {
return validateTemplateLiteral(node.arguments[1], correctName);
}

return false;
return true;
}

function validateLiteral(node: Literal, correctName: string) {
return node.value === correctName;
}

function validateTemplateLiteral(node: TemplateLiteral, correctName: string) {
return true;
}

function reportUnCorrectName(config: {
messageId: 'unCorrectName' | 'missingName'
context: Rule.RuleContext
Expand Down
34 changes: 27 additions & 7 deletions packages/eslint-plugin/src/tests/rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ const tester = new RuleTester({
tester.run('reatom/atom-rule', atomRule, {
valid: [
{
code: "import { atom } from '@reatom/framework'; const count = 'count';const countAtom = atom(0, `${count}Atom`);",
code: `
import { atom } from '@reatom/framework';
const count = 'count';
const countAtom = atom(0, \`\${count}Atom\`);
`,
},
{
code: `
import { atom } from '@reatom/framework';
const domain = (name) => 'some.' + name;
const countAtom = atom(0, domain\`count\`);
`,
},
{
code: `
Expand Down Expand Up @@ -153,15 +164,21 @@ tester.run('reatom/action-rule', actionRule, {
valid: [
{
code: `
import { action } from '@reatom/framework'
const doSome = action("doSome");
`,
import { action } from '@reatom/framework'
const doSome = action("doSome");
`,
},
{
code: `
import { action } from '@reatom/framework'
const doSome = action(() => {}, "doSome");
`,
import { action } from '@reatom/framework'
const doSome = action(() => {}, "doSome");
`,
},
{
code: `
import { action } from '@reatom/framework'
const doSome = action(() => {}, \`\${domain}.doSome\`);
`,
},
{
code: `const doSome = action();`,
Expand All @@ -175,6 +192,9 @@ tester.run('reatom/action-rule', actionRule, {
{
code: `const doSome = action(() => {}, "do");`,
},
{
code: `const doSome = action(() => {}, \`\${domain}.doSome\`);`,
},
],
invalid: [
{
Expand Down
7 changes: 7 additions & 0 deletions packages/lens/src/match.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,11 @@ test('is method', () => {
;`👍` //?
})

test('default should checks in the end', () => {
const ctx = createTestCtx()

assert.is(ctx.get(match(true).default(false).truthy(true)), true)
;`👍` //?
})

test.run()
31 changes: 16 additions & 15 deletions packages/lens/src/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,30 @@ interface Match<Expression = any, State = never, Default = undefined>
default<T = never>(
statement?: T | Atom<T> | ((ctx: CtxSpy, expression: Expression) => T),
): Match<Expression, State, T>

cases: Array<{
clause: (ctx: Ctx, expression: Expression) => boolean
statement:
| {}
| Atom
| ((ctx: Ctx, expression: Expression) => State | Default)
}>
}

export function match<T>(
expression: T | Atom<T> | ((ctx: CtxSpy) => T),
name = __count('match'),
): Match<T> {
const cases: Match<T>['cases'] = []
const cases: Array<{
clause: (ctx: Ctx, expression: T) => boolean
statement: {} | Atom | ((ctx: Ctx, expression: T) => any)
}> = []
let _truthy: (typeof cases)[number]
let _falsy: (typeof cases)[number]
let _default: (typeof cases)[number]

const theAtom = atom((ctxSpy) => {
const value = isAtom(expression)
? ctxSpy.spy(expression)
: typeof expression === 'function'
? (expression as Fn)(ctxSpy)
: expression
const ctx = merge(ctxSpy, { spy: undefined })
const list = [...cases, _truthy, _falsy, _default].filter(Boolean)

for (const { clause, statement } of cases) {
for (const { clause, statement } of list) {
if (clause(ctx, value)) {
return isAtom(statement)
? ctxSpy.spy(statement)
Expand All @@ -76,18 +76,19 @@ export function match<T>(
return theAtom
},
truthy(statement: any) {
cases.push({ clause: (ctx, value) => !!value, statement })
throwReatomError(_truthy, 'the case is already defined')
_truthy = { clause: (ctx, value) => !!value, statement }
return theAtom
},
falsy(statement: any) {
cases.push({ clause: (ctx, value) => !value, statement })
throwReatomError(_falsy, 'the case is already defined')
_falsy = { clause: (ctx, value) => !value, statement }
return theAtom
},
default(statement = () => throwReatomError(true, 'no match') as never) {
cases.push({ clause: (ctx, value) => true, statement })
throwReatomError(_default, 'the case is already defined')
_default = { clause: (ctx, value) => true, statement }
return theAtom
},

cases,
}) as Match<T>
}

1 comment on commit 27061af

@vercel
Copy link

@vercel vercel bot commented on 27061af Oct 11, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.