Skip to content

Commit add6c6d

Browse files
ESLint: Forbid importing directories (#2377)
Motivation #2277
1 parent d786d90 commit add6c6d

18 files changed

+78
-38
lines changed

.eslintrc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ plugins:
1010
- import
1111

1212
rules:
13+
no-dir-import: error
14+
1315
##############################################################################
1416
# `eslint-plugin-flowtype` rule list based on `v4.6.x`
1517
# https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"test:ci": "yarn check --integrity && npm run prettier:check && npm run lint -- --no-cache && npm run check && npm run testonly:cover && npm run check:ts && npm run check:spelling && npm run build",
2929
"testonly": "mocha --full-trace src/**/__tests__/**/*-test.js",
3030
"testonly:cover": "nyc npm run testonly",
31-
"lint": "eslint --cache --ext .js,.ts src resources",
31+
"lint": "eslint --rulesdir './resources/eslint-rules' --cache --ext .js,.ts src resources",
3232
"benchmark": "node --noconcurrent_sweeping --expose-gc --predictable ./resources/benchmark.js",
3333
"prettier": "prettier --ignore-path .gitignore --write --list-different \"**/*.{js,ts,md,json,yml}\"",
3434
"prettier:check": "prettier --ignore-path .gitignore --check \"**/*.{js,ts,md,json,yml}\"",
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: checkImporPath,
11+
ExportNamedDeclaration: checkImporPath,
12+
};
13+
14+
function checkImporPath(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+
};

src/index.d.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export {
124124
// Validate GraphQL schema.
125125
validateSchema,
126126
assertValidSchema,
127-
} from './type';
127+
} from './type/index';
128128

129129
export {
130130
GraphQLType,
@@ -167,7 +167,7 @@ export {
167167
GraphQLScalarSerializer,
168168
GraphQLScalarValueParser,
169169
GraphQLScalarLiteralParser,
170-
} from './type';
170+
} from './type/index';
171171

172172
// Parse and operate on GraphQL language source files.
173173
export {
@@ -202,7 +202,7 @@ export {
202202
isTypeDefinitionNode,
203203
isTypeSystemExtensionNode,
204204
isTypeExtensionNode,
205-
} from './language';
205+
} from './language/index';
206206

207207
export {
208208
ParseOptions,
@@ -274,7 +274,7 @@ export {
274274
UnionTypeExtensionNode,
275275
EnumTypeExtensionNode,
276276
InputObjectTypeExtensionNode,
277-
} from './language';
277+
} from './language/index';
278278

279279
// Execute GraphQL queries.
280280
export {
@@ -285,13 +285,13 @@ export {
285285
getDirectiveValues,
286286
ExecutionArgs,
287287
ExecutionResult,
288-
} from './execution';
288+
} from './execution/index';
289289

290290
export {
291291
subscribe,
292292
createSourceEventStream,
293293
SubscriptionArgs,
294-
} from './subscription';
294+
} from './subscription/index';
295295

296296
// Validate GraphQL documents.
297297
export {
@@ -326,7 +326,7 @@ export {
326326
VariablesAreInputTypesRule,
327327
VariablesInAllowedPositionRule,
328328
ValidationRule,
329-
} from './validation';
329+
} from './validation/index';
330330

331331
// Create, format, and print GraphQL errors.
332332
export {
@@ -336,7 +336,7 @@ export {
336336
printError,
337337
formatError,
338338
GraphQLFormattedError,
339-
} from './error';
339+
} from './error/index';
340340

341341
// Utilities for operating on GraphQL type schema and parsed sources.
342342
export {
@@ -406,7 +406,7 @@ export {
406406
findDangerousChanges,
407407
// Report all deprecated usage within a GraphQL document.
408408
findDeprecatedUsages,
409-
} from './utilities';
409+
} from './utilities/index';
410410

411411
export {
412412
IntrospectionOptions,
@@ -434,4 +434,4 @@ export {
434434
BuildSchemaOptions,
435435
BreakingChange,
436436
DangerousChange,
437-
} from './utilities';
437+
} from './utilities/index';

src/index.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export {
125125
// Validate GraphQL schema.
126126
validateSchema,
127127
assertValidSchema,
128-
} from './type';
128+
} from './type/index';
129129

130130
export type {
131131
GraphQLType,
@@ -168,7 +168,7 @@ export type {
168168
GraphQLScalarSerializer,
169169
GraphQLScalarValueParser,
170170
GraphQLScalarLiteralParser,
171-
} from './type';
171+
} from './type/index';
172172

173173
// Parse and operate on GraphQL language source files.
174174
export {
@@ -203,7 +203,7 @@ export {
203203
isTypeDefinitionNode,
204204
isTypeSystemExtensionNode,
205205
isTypeExtensionNode,
206-
} from './language';
206+
} from './language/index';
207207

208208
export type {
209209
ParseOptions,
@@ -275,7 +275,7 @@ export type {
275275
UnionTypeExtensionNode,
276276
EnumTypeExtensionNode,
277277
InputObjectTypeExtensionNode,
278-
} from './language';
278+
} from './language/index';
279279

280280
// Execute GraphQL queries.
281281
export {
@@ -284,12 +284,12 @@ export {
284284
defaultTypeResolver,
285285
responsePathAsArray,
286286
getDirectiveValues,
287-
} from './execution';
287+
} from './execution/index';
288288

289-
export type { ExecutionArgs, ExecutionResult } from './execution';
289+
export type { ExecutionArgs, ExecutionResult } from './execution/index';
290290

291-
export { subscribe, createSourceEventStream } from './subscription';
292-
export type { SubscriptionArgs } from './subscription';
291+
export { subscribe, createSourceEventStream } from './subscription/index';
292+
export type { SubscriptionArgs } from './subscription/index';
293293

294294
// Validate GraphQL documents.
295295
export {
@@ -323,9 +323,9 @@ export {
323323
ValuesOfCorrectTypeRule,
324324
VariablesAreInputTypesRule,
325325
VariablesInAllowedPositionRule,
326-
} from './validation';
326+
} from './validation/index';
327327

328-
export type { ValidationRule } from './validation';
328+
export type { ValidationRule } from './validation/index';
329329

330330
// Create, format, and print GraphQL errors.
331331
export {
@@ -334,9 +334,9 @@ export {
334334
locatedError,
335335
printError,
336336
formatError,
337-
} from './error';
337+
} from './error/index';
338338

339-
export type { GraphQLFormattedError } from './error';
339+
export type { GraphQLFormattedError } from './error/index';
340340

341341
// Utilities for operating on GraphQL type schema and parsed sources.
342342
export {
@@ -406,7 +406,7 @@ export {
406406
findDangerousChanges,
407407
// Report all deprecated usage within a GraphQL document.
408408
findDeprecatedUsages,
409-
} from './utilities';
409+
} from './utilities/index';
410410

411411
export type {
412412
IntrospectionOptions,
@@ -434,4 +434,4 @@ export type {
434434
BuildSchemaOptions,
435435
BreakingChange,
436436
DangerousChange,
437-
} from './utilities';
437+
} from './utilities/index';

src/language/__tests__/parser-benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { parse } from '../parser';
44

5-
import { kitchenSinkQuery } from '../../__fixtures__';
5+
import { kitchenSinkQuery } from '../../__fixtures__/index';
66

77
export const name = 'Parse kitchen sink';
88
export const count = 1000;

src/language/__tests__/parser-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Source } from '../source';
1313
import { TokenKind } from '../tokenKind';
1414
import { parse, parseValue, parseType } from '../parser';
1515

16-
import { kitchenSinkQuery } from '../../__fixtures__';
16+
import { kitchenSinkQuery } from '../../__fixtures__/index';
1717

1818
import toJSONDeep from './toJSONDeep';
1919

src/language/__tests__/printer-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import dedent from '../../jsutils/dedent';
88
import { parse } from '../parser';
99
import { print } from '../printer';
1010

11-
import { kitchenSinkQuery } from '../../__fixtures__';
11+
import { kitchenSinkQuery } from '../../__fixtures__/index';
1212

1313
describe('Printer: Query document', () => {
1414
it('does not alter ast', () => {

src/language/__tests__/schema-parser-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { describe, it } from 'mocha';
55

66
import dedent from '../../jsutils/dedent';
77

8-
import { kitchenSinkSDL } from '../../__fixtures__';
8+
import { kitchenSinkSDL } from '../../__fixtures__/index';
99

1010
import { parse } from '../parser';
1111

src/language/__tests__/schema-printer-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import dedent from '../../jsutils/dedent';
88
import { parse } from '../parser';
99
import { print } from '../printer';
1010

11-
import { kitchenSinkSDL } from '../../__fixtures__';
11+
import { kitchenSinkSDL } from '../../__fixtures__/index';
1212

1313
describe('Printer: SDL document', () => {
1414
it('prints minimal ast', () => {

src/language/__tests__/visitor-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Kind } from '../kinds';
99
import { parse } from '../parser';
1010
import { visit, visitInParallel, BREAK, QueryDocumentKeys } from '../visitor';
1111

12-
import { kitchenSinkQuery } from '../../__fixtures__';
12+
import { kitchenSinkQuery } from '../../__fixtures__/index';
1313

1414
function checkVisitorFnArgs(ast, args, isEdited) {
1515
const [node, key, parent, path, ancestors] = args;

src/utilities/__tests__/buildASTSchema-benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { parse } from '../../language/parser';
44

55
import { buildASTSchema } from '../buildASTSchema';
66

7-
import { bigSchemaSDL } from '../../__fixtures__';
7+
import { bigSchemaSDL } from '../../__fixtures__/index';
88

99
const schemaAST = parse(bigSchemaSDL);
1010

src/utilities/__tests__/buildClientSchema-benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { buildClientSchema } from '../buildClientSchema';
44

5-
import { bigSchemaIntrospectionResult } from '../../__fixtures__';
5+
import { bigSchemaIntrospectionResult } from '../../__fixtures__/index';
66

77
export const name = 'Build Schema from Introspection';
88
export const count = 10;

src/utilities/__tests__/introspectionFromSchema-benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { execute } from '../../execution/execute';
66
import { buildSchema } from '../buildASTSchema';
77
import { getIntrospectionQuery } from '../getIntrospectionQuery';
88

9-
import { bigSchemaSDL } from '../../__fixtures__';
9+
import { bigSchemaSDL } from '../../__fixtures__/index';
1010

1111
const schema = buildSchema(bigSchemaSDL, { assumeValid: true });
1212
const document = parse(getIntrospectionQuery());

src/utilities/__tests__/stripIgnoredCharacters-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Lexer } from '../../language/lexer';
1212

1313
import { stripIgnoredCharacters } from '../stripIgnoredCharacters';
1414

15-
import { kitchenSinkQuery, kitchenSinkSDL } from '../../__fixtures__';
15+
import { kitchenSinkQuery, kitchenSinkSDL } from '../../__fixtures__/index';
1616

1717
const ignoredTokens = [
1818
// UnicodeBOM ::

src/validation/__tests__/validateGQL-benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { getIntrospectionQuery } from '../../utilities/getIntrospectionQuery';
66

77
import { validate } from '../validate';
88

9-
import { bigSchemaSDL } from '../../__fixtures__';
9+
import { bigSchemaSDL } from '../../__fixtures__/index';
1010

1111
const schema = buildSchema(bigSchemaSDL, { assumeValid: true });
1212
const queryAST = parse(getIntrospectionQuery());

src/validation/__tests__/validateInvalidGQL-benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { buildSchema } from '../../utilities/buildASTSchema';
55

66
import { validate } from '../validate';
77

8-
import { bigSchemaSDL } from '../../__fixtures__';
8+
import { bigSchemaSDL } from '../../__fixtures__/index';
99

1010
const schema = buildSchema(bigSchemaSDL, { assumeValid: true });
1111
const queryAST = parse(`

src/validation/__tests__/validateSDL-benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { parse } from '../../language/parser';
44

55
import { validateSDL } from '../validate';
66

7-
import { bigSchemaSDL } from '../../__fixtures__';
7+
import { bigSchemaSDL } from '../../__fixtures__/index';
88

99
const sdlAST = parse(bigSchemaSDL);
1010

0 commit comments

Comments
 (0)