/
no-empty-file.js
49 lines (42 loc) · 930 Bytes
/
no-empty-file.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
const MESSAGE_ID = 'no-empty-file';
const messages = {
[MESSAGE_ID]: 'Empty files are not allowed.',
};
const isEmpty = node =>
(
(node.type === 'Program' || node.type === 'BlockStatement')
&& node.body.every(currentNode => isEmpty(currentNode))
)
|| node.type === 'EmptyStatement'
|| (node.type === 'ExpressionStatement' && 'directive' in node);
/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const filename = context.getPhysicalFilename().toLowerCase();
if (!/\.(?:js|mjs|cjs|ts|mts|cts)$/.test(filename)) {
return {};
}
return {
Program(node) {
if (!isEmpty(node)) {
return;
}
return {
node,
messageId: MESSAGE_ID,
};
},
};
};
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Disallow empty files.',
},
schema: [],
messages,
},
};