Skip to content

Commit

Permalink
Refactors and adds Vue tests. (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Sep 7, 2021
1 parent c8a2c02 commit d7a0849
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 42 deletions.
14 changes: 7 additions & 7 deletions docs/rules/no-empty-file.md
Expand Up @@ -10,15 +10,15 @@ This rule is not fixable.

```

```
```js
\n
```

```
```js
\r
```

```
```js
\r\n
```

Expand All @@ -39,7 +39,8 @@ This rule is not fixable.
```

```js
{}
{
}
```

## Pass
Expand All @@ -54,13 +55,12 @@ const x = 0;
```

```js
;; const x = 0;
;;
const x = 0;
```

```js
{
;
'use strict';
const x = 0;
}
```
25 changes: 14 additions & 11 deletions rules/no-empty-file.js
Expand Up @@ -5,31 +5,34 @@ const messages = {
};

const isEmpty = node =>
node.type === 'EmptyStatement'
|| (node.type === 'ExpressionStatement' && 'directive' in node)
|| (node.type === 'BlockStatement' && node.body.every(currentNode => isEmpty(currentNode)));
(
(node.type === 'Program' || node.type === 'BlockStatement')
&& node.body.every(currentNode => isEmpty(currentNode))
)
|| node.type === 'EmptyStatement'
|| (node.type === 'ExpressionStatement' && 'directive' in node);

const create = () => ({
Program(node) {
if (node.body.every(currentNode => isEmpty(currentNode))) {
return {
node,
messageId: MESSAGE_ID,
};
if (!isEmpty(node)) {
return;
}

return {
node,
messageId: MESSAGE_ID,
};
},
});

const schema = [];

module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Disallow empty files.',
},
schema,
schema: [],
messages,
},
};
38 changes: 32 additions & 6 deletions test/no-empty-file.mjs
@@ -1,9 +1,7 @@
import outdent from 'outdent';
import {getTester} from './utils/test.mjs';
import {getTester, parsers} from './utils/test.mjs';

const {test} = getTester(import.meta);
const space = ' ';
const tab = ' ';

test.snapshot({
valid: [
Expand All @@ -16,16 +14,26 @@ test.snapshot({
`,
';;\'use strict\';',
'{\'use strict\';}',
'("use strict")',
'`use strict`',
'({})',
outdent`
#!/usr/bin/env node
console.log('done');
`,
'false',
'("")',
'NaN',
'undefined',
'null',
'[]',
'(() => {})()',
],
invalid: [
'',
space,
tab,
'\uFEFF',
' ',
'\t',
'\n',
'\r',
'\r\n',
Expand All @@ -35,13 +43,31 @@ test.snapshot({
`,
'// comment',
'/* comment */',
'#!/usr/bin/env node',
'\'use asm\';',
'\'use strict\';',
'"use strict"',
'""',
';',
';;',
'{}',
'{;;}',
'{{}}',
'#!/usr/bin/env node',
],
});

test.snapshot({
testerOptions: {
parser: parsers.vue,
},
valid: [
'<template></template>',
'<style></style>',
'<script></script>',
'<script>;</script>',
'<custom-block></custom-block>',
],
invalid: [
' ',
],
});
76 changes: 58 additions & 18 deletions test/snapshots/no-empty-file.mjs.md
Expand Up @@ -15,6 +15,16 @@ Generated by [AVA](https://avajs.dev).
`

## Invalid #2
1 | 

> Error 1/1
`␊
> 1 | ␊
| ^ Empty files are not allowed.␊
`

## Invalid #3
1 |

> Error 1/1
Expand All @@ -24,7 +34,7 @@ Generated by [AVA](https://avajs.dev).
| ^ Empty files are not allowed.␊
`

## Invalid #3
## Invalid #4
1 |

> Error 1/1
Expand All @@ -34,7 +44,7 @@ Generated by [AVA](https://avajs.dev).
| ^ Empty files are not allowed.␊
`

## Invalid #4
## Invalid #5
1 |
2 |

Expand All @@ -47,7 +57,7 @@ Generated by [AVA](https://avajs.dev).
| ^ Empty files are not allowed.␊
`

## Invalid #5
## Invalid #6
1 |
2 |

Expand All @@ -60,7 +70,7 @@ Generated by [AVA](https://avajs.dev).
| ^ Empty files are not allowed.␊
`

## Invalid #6
## Invalid #7
1 |
2 |

Expand All @@ -73,7 +83,7 @@ Generated by [AVA](https://avajs.dev).
| ^ Empty files are not allowed.␊
`

## Invalid #7
## Invalid #8
1 |
2 |

Expand All @@ -86,7 +96,7 @@ Generated by [AVA](https://avajs.dev).
| ^ Empty files are not allowed.␊
`

## Invalid #8
## Invalid #9
1 | // comment

> Error 1/1
Expand All @@ -96,7 +106,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^ Empty files are not allowed.␊
`

## Invalid #9
## Invalid #10
1 | /* comment */

> Error 1/1
Expand All @@ -106,7 +116,17 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^ Empty files are not allowed.␊
`

## Invalid #10
## Invalid #11
1 | #!/usr/bin/env node

> Error 1/1
`␊
> 1 | #!/usr/bin/env node␊
| ^^^^^^^^^^^^^^^^^^^ Empty files are not allowed.␊
`

## Invalid #12
1 | 'use asm';

> Error 1/1
Expand All @@ -116,7 +136,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^ Empty files are not allowed.␊
`

## Invalid #11
## Invalid #13
1 | 'use strict';

> Error 1/1
Expand All @@ -126,7 +146,27 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^ Empty files are not allowed.␊
`

## Invalid #12
## Invalid #14
1 | "use strict"

> Error 1/1
`␊
> 1 | "use strict"␊
| ^^^^^^^^^^^^ Empty files are not allowed.␊
`

## Invalid #15
1 | ""

> Error 1/1
`␊
> 1 | ""␊
| ^^ Empty files are not allowed.␊
`

## Invalid #16
1 | ;

> Error 1/1
Expand All @@ -136,7 +176,7 @@ Generated by [AVA](https://avajs.dev).
| ^ Empty files are not allowed.␊
`

## Invalid #13
## Invalid #17
1 | ;;

> Error 1/1
Expand All @@ -146,7 +186,7 @@ Generated by [AVA](https://avajs.dev).
| ^^ Empty files are not allowed.␊
`

## Invalid #14
## Invalid #18
1 | {}

> Error 1/1
Expand All @@ -156,7 +196,7 @@ Generated by [AVA](https://avajs.dev).
| ^^ Empty files are not allowed.␊
`

## Invalid #15
## Invalid #19
1 | {;;}

> Error 1/1
Expand All @@ -166,7 +206,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^ Empty files are not allowed.␊
`

## Invalid #16
## Invalid #20
1 | {{}}

> Error 1/1
Expand All @@ -176,12 +216,12 @@ Generated by [AVA](https://avajs.dev).
| ^^^^ Empty files are not allowed.␊
`

## Invalid #17
1 | #!/usr/bin/env node
## Invalid #1
1 |

> Error 1/1
`␊
> 1 | #!/usr/bin/env node
| ^^^^^^^^^^^^^^^^^^^ Empty files are not allowed.␊
> 1 |
| ^^^ Empty files are not allowed.␊
`
Binary file modified test/snapshots/no-empty-file.mjs.snap
Binary file not shown.

0 comments on commit d7a0849

Please sign in to comment.