forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-imports-in-directory.js
52 lines (48 loc) · 1.35 KB
/
no-imports-in-directory.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
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
const path = require('path');
/**
* @type {import('eslint').Rule.RuleModule}
*/
module.exports = {
meta : {
type : 'problem',
docs : {
description : 'Ban files in a directory from importing specific modules',
category : 'Possible Errors',
},
messages : {
invalidImport : 'It is banned to import this module from this file\'s directory',
},
schema : [{
type : 'object',
properties : {
bannedImportPaths : {
type : 'array',
},
},
additionalProperties : false,
}]
},
create : function(context) {
const filename = context.filename ?? context.getFilename();
const bannedPaths = context.options[0].bannedImportPaths || [];
const fileNameOfFileBeingChecked = path.resolve(filename);
return {
'ImportDeclaration'(node) {
const importPath = path.resolve(path.dirname(fileNameOfFileBeingChecked), node.source.value);
for (const banned of bannedPaths) {
if (importPath.includes(banned)) {
context.report({
node,
messageId: 'invalidImport'
});
break;
}
}
}
};
}
};