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

feat: move all JS patterns to this repo #1

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
53 changes: 53 additions & 0 deletions .grit/patterns/js/add_type_caught_errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
title: Add Type To Caught Errors
---

# Add Type To Caught Errors

Add `any` type annotation to caught errors. It is a common source of tsc errors.

tags: #js, #ts

```grit
engine marzano(1.0)
language js

catch_clause($parameter, $type) where {
$type <: .,
$parameter => `$parameter: any`
}
```

## Basic example

```ts
function foo() {
try {
console.log('tada');
} catch (e) {
console.log('oops');
}
}

try {
console.log('tada');
} catch (e: Foo) {
console.log('oops');
}
```

```ts
function foo() {
try {
console.log('tada');
} catch (e: any) {
console.log('oops');
}
}

try {
console.log('tada');
} catch (e: Foo) {
console.log('oops');
}
```
108 changes: 108 additions & 0 deletions .grit/patterns/js/chai_to_jest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
---
title: Chai to Jest
---

Convert Chai test assertions to Jest.

tags: #migration, #js

```grit
engine marzano(0.1)
language js

pattern expect_like($o) {
or {`expect($o)`, `expect($o).not`}
}

pattern have_like() {
or { `have` , `has` }
}

pattern x_to($x) {
or { `$x.to`, $x } where {
$x <: expect_like($o)
}
}

pattern include_like() {
or { `include`, `includes`, `contain`, `contains` }
}

pattern x_to_be($x) {
or {`$x.to.be`, `$x.to`, $x }
}

or {
// pure chai -- assert
`assert.match($s, $p, $_)` => `expect($s).toMatch($p)`,
`assert.isFalse($x, $_)` => `expect($x).toBe(false)`,
`assert.isTrue($x, $_)` => `expect($x).toBe(true)`,
`assert.isNull($x, $_)` => `expect($x).toBe(null)`,
`assert.toBe($x, $a, $_)` => `expect($x).toBe($a)`,
`assert.isDefined($x, $_)` => `expect($x).toBeDefined()`,
`assert.include($haystack, $needle, $_)` => `expect($haystack).toContain($needle)`,
`assert.strictEqual($a, $b, $_)` => `expect($a).toBe($b)`,
`assert.deepEqual($a, $b, $_)` => `expect($a).toEqual($b)`,
`assert.equal($a, $b, $_)` => `expect($a).toEqual($b)`,
`assert($x, $_)` => `expect($x).toBeTruthy()`,

// equality
`$xtobe.deep.equal($v)` where { $xtobe <: x_to_be($x) } => `$x.toEqual($v)`,
`$xtobe.equal($v)` where { $xtobe <: x_to_be($x) } => `$x.toBe($v)`, // this is wrong in jest-codemods
`$xtobe.equals($v)` where { $xtobe <: x_to_be($x) } => `$x.toBe($v)`,
`$xtobe.eql($v)` where { $xtobe <: x_to_be($x) } => `$x.toEqual($v)`,
`$xtobe.eq($v)` where { $xtobe <: x_to_be($x) } => `$x.toEqual($v)`,
`$xtobe.greaterThan($n)` where { $xtobe <: x_to_be($x) } => `$x.toBeGreaterThan($n)`,
`$xtobe.null` where { $xtobe <: x_to_be($x) } => `$x.toBeNull()`,
`$xtobe.be($v)` where { $xtobe <: x_to_be($x) } => `$x.toBe($v)`, // leave it with .be to avoid false positives
`$xtobe.true` where { $xtobe <: x_to_be($x) } => `$x.toBe(true)`,
`$xtobe.false` where { $xtobe <: x_to_be($x) } => `$x.toBe(false)`,
`$xtobe.exist` where { $xtobe <: x_to_be($x) } => `$x.toEqual(expect.anything())`,
`$xtobe.undefined` where { $xtobe <: x_to_be($x) } => `$x.toBeUndefined()`,
`$xtobe.empty` where { $xtobe <: x_to_be($x) } => `$x.toHaveLength(0)`,
`$xtobe.a('function')` where { $xtobe <: x_to_be($x) } => `$x.toBeInstanceOf(Function)`,
`$xtobe.instanceOf($t)` where { $xtobe <: x_to_be($x) } => `$x.toBeInstanceOf($t)`,

`$xtobe.object` where {
$xtobe <: x_to_be($x),
$x <: `expect($o)`
} => `expect(typeof $o === 'object').toBeTruthy()`,

`$xto.deep.$include($v, $_)` where { $xto <: x_to($x), $include <: include_like() }=> `$x.toMatchObject($v)`,
`$xto.$include($e)` where { $xto <: x_to($x), $e <: object(), $include <: include_like() } => `$x.toMatchObject($e)`,
`$xto.$include($e)` where { $xto <: x_to($x), $include <: include_like() } => `$x.toContain($e)`,

`$xto.match($p, $_)` where { $xto <: x_to($x) } => `$x.toMatch($p)`, // leave it with to.match to avoid false positives

`$xto.$h.length($n)` where { $xto <: x_to($x), $x <: have_like() } => `$x.toHaveLength($n)`,
`$xto.$h.lengthOf($n)` where { $xto <: x_to($x), $x <: have_like() } => `$x.toHaveLength($n)`,
`$xto.$h.property($p)` where { $xto <: x_to($x), $x <: have_like() } => `$x.toHaveProperty($p)`,
`$xto.$h.keys($oneKey)` where {
$xto <: x_to($x),
$x <: have_like(),
$oneKey <: not or { `[ $_ ]`, [ ... ] }
} => `$x.toHaveProperty($oneKey)`
}
```

## Convert assertions

```javascript
describe('sum', () => {
it('should work for positive numbers', () => {
const object = { nine: 9 };
expect(object).contains({ nine: 9 });
assert.equal(1 + 2, 3, '1 plus 2 is 3');
}).timeout(1000);
});
```

```typescript
describe('sum', () => {
it('should work for positive numbers', () => {
const object = { nine: 9 };
expect(object).toMatchObject({ nine: 9 });
expect(1 + 2).toEqual(3);
}).timeout(1000);
});
```
Loading
Loading