Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit cfe2e6a

Browse files
committed
ESLint: sync 'eslint-internal-rules' with 'graphql-js'
1 parent 5f72bc7 commit cfe2e6a

File tree

7 files changed

+69
-0
lines changed

7 files changed

+69
-0
lines changed

.eslintrc.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@ env:
66
node: true
77
reportUnusedDisableDirectives: true
88
plugins:
9+
- internal-rules
910
- flowtype
1011
- node
1112
- istanbul
1213
- import
1314

1415
rules:
16+
##############################################################################
17+
# Internal rules located in 'resources/eslint-internal-rules'.
18+
# See './resources/eslint-internal-rules/README.md'
19+
##############################################################################
20+
21+
internal-rules/no-dir-import: error
22+
1523
##############################################################################
1624
# `eslint-plugin-flowtype` rule list based on `v5.1.x`
1725
# https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"dtslint": "3.6.10",
6464
"eslint": "7.1.0",
6565
"eslint-plugin-flowtype": "5.1.3",
66+
"eslint-plugin-internal-rules": "file:./resources/eslint-internal-rules",
6667
"eslint-plugin-import": "2.20.2",
6768
"eslint-plugin-istanbul": "0.1.1",
6869
"eslint-plugin-node": "11.1.0",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Custom ESLint Rules
2+
3+
This is a dummy npm package that allows us to treat it as an `eslint-plugin-graphql-internal`.
4+
It's not actually published, nor are the rules here useful for users of graphql.
5+
6+
**If you modify this rule, you must re-run `npm install` for it to take effect.**
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @noflow
2+
3+
'use strict';
4+
5+
module.exports = {
6+
rules: {
7+
'no-dir-import': require('./no-dir-import'),
8+
},
9+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// @noflow
2+
3+
'use strict';
4+
5+
const fs = require('fs');
6+
const path = require('path');
7+
8+
module.exports = function (context) {
9+
return {
10+
ImportDeclaration: checkImportPath,
11+
ExportNamedDeclaration: checkImportPath,
12+
};
13+
14+
function checkImportPath(node) {
15+
const { source } = node;
16+
17+
// bail if the declaration doesn't have a source, e.g. "export { foo };"
18+
if (!source) {
19+
return;
20+
}
21+
22+
const importPath = source.value;
23+
if (importPath.startsWith('./') || importPath.startsWith('../')) {
24+
const baseDir = path.dirname(context.getFilename());
25+
const resolvedPath = path.resolve(baseDir, importPath);
26+
27+
if (
28+
fs.existsSync(resolvedPath) &&
29+
fs.statSync(resolvedPath).isDirectory()
30+
) {
31+
context.report({
32+
node: source,
33+
message: 'It is not allowed to import from directory',
34+
});
35+
}
36+
}
37+
}
38+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "eslint-plugin-graphql-internal",
3+
"version": "0.0.0"
4+
}

yarn.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,9 @@ [email protected]:
19831983
read-pkg-up "^2.0.0"
19841984
resolve "^1.12.0"
19851985

1986+
"eslint-plugin-internal-rules@file:./resources/eslint-internal-rules":
1987+
version "0.0.0"
1988+
19861989
19871990
version "0.1.1"
19881991
resolved "https://registry.yarnpkg.com/eslint-plugin-istanbul/-/eslint-plugin-istanbul-0.1.1.tgz#6289daca3844f6ae1fc23a29093d8554fc7dc352"

0 commit comments

Comments
 (0)