-
-
Notifications
You must be signed in to change notification settings - Fork 353
/
no-empty-file.js
57 lines (46 loc) · 1.21 KB
/
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
50
51
52
53
54
55
56
57
'use strict';
const {isEmptyNode} = require('./ast/index.js');
const MESSAGE_ID = 'no-empty-file';
const messages = {
[MESSAGE_ID]: 'Empty files are not allowed.',
};
const isDirective = node => node.type === 'ExpressionStatement' && 'directive' in node;
const isEmpty = node => isEmptyNode(node, isDirective);
const isTripleSlashDirective = node =>
node.type === 'Line' && node.value.startsWith('/');
const hasTripeSlashDirectives = comments =>
comments.some(currentNode => isTripleSlashDirective(currentNode));
/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const filename = context.getPhysicalFilename();
if (!/\.(?:js|mjs|cjs|jsx|ts|mts|cts|tsx)$/i.test(filename)) {
return;
}
return {
Program(node) {
if (node.body.some(node => !isEmpty(node))) {
return;
}
const sourceCode = context.getSourceCode();
const comments = sourceCode.getAllComments();
if (hasTripeSlashDirectives(comments)) {
return;
}
return {
node,
messageId: MESSAGE_ID,
};
},
};
};
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Disallow empty files.',
},
messages,
},
};