Skip to content

Commit

Permalink
ESLint: Forbid importing directories (#2377)
Browse files Browse the repository at this point in the history
Motivation #2277
  • Loading branch information
IvanGoncharov authored Jan 20, 2020
1 parent d786d90 commit add6c6d
Show file tree
Hide file tree
Showing 18 changed files with 78 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ plugins:
- import

rules:
no-dir-import: error

##############################################################################
# `eslint-plugin-flowtype` rule list based on `v4.6.x`
# https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"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",
"testonly": "mocha --full-trace src/**/__tests__/**/*-test.js",
"testonly:cover": "nyc npm run testonly",
"lint": "eslint --cache --ext .js,.ts src resources",
"lint": "eslint --rulesdir './resources/eslint-rules' --cache --ext .js,.ts src resources",
"benchmark": "node --noconcurrent_sweeping --expose-gc --predictable ./resources/benchmark.js",
"prettier": "prettier --ignore-path .gitignore --write --list-different \"**/*.{js,ts,md,json,yml}\"",
"prettier:check": "prettier --ignore-path .gitignore --check \"**/*.{js,ts,md,json,yml}\"",
Expand Down
38 changes: 38 additions & 0 deletions resources/eslint-rules/no-dir-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// @noflow

'use strict';

const fs = require('fs');
const path = require('path');

module.exports = function(context) {
return {
ImportDeclaration: checkImporPath,
ExportNamedDeclaration: checkImporPath,
};

function checkImporPath(node) {
const { source } = node;

// bail if the declaration doesn't have a source, e.g. "export { foo };"
if (!source) {
return;
}

const importPath = source.value;
if (importPath.startsWith('./') || importPath.startsWith('../')) {
const baseDir = path.dirname(context.getFilename());
const resolvedPath = path.resolve(baseDir, importPath);

if (
fs.existsSync(resolvedPath) &&
fs.statSync(resolvedPath).isDirectory()
) {
context.report({
node: source,
message: 'It is not allowed to import from directory',
});
}
}
}
};
20 changes: 10 additions & 10 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export {
// Validate GraphQL schema.
validateSchema,
assertValidSchema,
} from './type';
} from './type/index';

export {
GraphQLType,
Expand Down Expand Up @@ -167,7 +167,7 @@ export {
GraphQLScalarSerializer,
GraphQLScalarValueParser,
GraphQLScalarLiteralParser,
} from './type';
} from './type/index';

// Parse and operate on GraphQL language source files.
export {
Expand Down Expand Up @@ -202,7 +202,7 @@ export {
isTypeDefinitionNode,
isTypeSystemExtensionNode,
isTypeExtensionNode,
} from './language';
} from './language/index';

export {
ParseOptions,
Expand Down Expand Up @@ -274,7 +274,7 @@ export {
UnionTypeExtensionNode,
EnumTypeExtensionNode,
InputObjectTypeExtensionNode,
} from './language';
} from './language/index';

// Execute GraphQL queries.
export {
Expand All @@ -285,13 +285,13 @@ export {
getDirectiveValues,
ExecutionArgs,
ExecutionResult,
} from './execution';
} from './execution/index';

export {
subscribe,
createSourceEventStream,
SubscriptionArgs,
} from './subscription';
} from './subscription/index';

// Validate GraphQL documents.
export {
Expand Down Expand Up @@ -326,7 +326,7 @@ export {
VariablesAreInputTypesRule,
VariablesInAllowedPositionRule,
ValidationRule,
} from './validation';
} from './validation/index';

// Create, format, and print GraphQL errors.
export {
Expand All @@ -336,7 +336,7 @@ export {
printError,
formatError,
GraphQLFormattedError,
} from './error';
} from './error/index';

// Utilities for operating on GraphQL type schema and parsed sources.
export {
Expand Down Expand Up @@ -406,7 +406,7 @@ export {
findDangerousChanges,
// Report all deprecated usage within a GraphQL document.
findDeprecatedUsages,
} from './utilities';
} from './utilities/index';

export {
IntrospectionOptions,
Expand Down Expand Up @@ -434,4 +434,4 @@ export {
BuildSchemaOptions,
BreakingChange,
DangerousChange,
} from './utilities';
} from './utilities/index';
28 changes: 14 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export {
// Validate GraphQL schema.
validateSchema,
assertValidSchema,
} from './type';
} from './type/index';

export type {
GraphQLType,
Expand Down Expand Up @@ -168,7 +168,7 @@ export type {
GraphQLScalarSerializer,
GraphQLScalarValueParser,
GraphQLScalarLiteralParser,
} from './type';
} from './type/index';

// Parse and operate on GraphQL language source files.
export {
Expand Down Expand Up @@ -203,7 +203,7 @@ export {
isTypeDefinitionNode,
isTypeSystemExtensionNode,
isTypeExtensionNode,
} from './language';
} from './language/index';

export type {
ParseOptions,
Expand Down Expand Up @@ -275,7 +275,7 @@ export type {
UnionTypeExtensionNode,
EnumTypeExtensionNode,
InputObjectTypeExtensionNode,
} from './language';
} from './language/index';

// Execute GraphQL queries.
export {
Expand All @@ -284,12 +284,12 @@ export {
defaultTypeResolver,
responsePathAsArray,
getDirectiveValues,
} from './execution';
} from './execution/index';

export type { ExecutionArgs, ExecutionResult } from './execution';
export type { ExecutionArgs, ExecutionResult } from './execution/index';

export { subscribe, createSourceEventStream } from './subscription';
export type { SubscriptionArgs } from './subscription';
export { subscribe, createSourceEventStream } from './subscription/index';
export type { SubscriptionArgs } from './subscription/index';

// Validate GraphQL documents.
export {
Expand Down Expand Up @@ -323,9 +323,9 @@ export {
ValuesOfCorrectTypeRule,
VariablesAreInputTypesRule,
VariablesInAllowedPositionRule,
} from './validation';
} from './validation/index';

export type { ValidationRule } from './validation';
export type { ValidationRule } from './validation/index';

// Create, format, and print GraphQL errors.
export {
Expand All @@ -334,9 +334,9 @@ export {
locatedError,
printError,
formatError,
} from './error';
} from './error/index';

export type { GraphQLFormattedError } from './error';
export type { GraphQLFormattedError } from './error/index';

// Utilities for operating on GraphQL type schema and parsed sources.
export {
Expand Down Expand Up @@ -406,7 +406,7 @@ export {
findDangerousChanges,
// Report all deprecated usage within a GraphQL document.
findDeprecatedUsages,
} from './utilities';
} from './utilities/index';

export type {
IntrospectionOptions,
Expand Down Expand Up @@ -434,4 +434,4 @@ export type {
BuildSchemaOptions,
BreakingChange,
DangerousChange,
} from './utilities';
} from './utilities/index';
2 changes: 1 addition & 1 deletion src/language/__tests__/parser-benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

import { kitchenSinkQuery } from '../../__fixtures__';
import { kitchenSinkQuery } from '../../__fixtures__/index';

export const name = 'Parse kitchen sink';
export const count = 1000;
Expand Down
2 changes: 1 addition & 1 deletion src/language/__tests__/parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Source } from '../source';
import { TokenKind } from '../tokenKind';
import { parse, parseValue, parseType } from '../parser';

import { kitchenSinkQuery } from '../../__fixtures__';
import { kitchenSinkQuery } from '../../__fixtures__/index';

import toJSONDeep from './toJSONDeep';

Expand Down
2 changes: 1 addition & 1 deletion src/language/__tests__/printer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import dedent from '../../jsutils/dedent';
import { parse } from '../parser';
import { print } from '../printer';

import { kitchenSinkQuery } from '../../__fixtures__';
import { kitchenSinkQuery } from '../../__fixtures__/index';

describe('Printer: Query document', () => {
it('does not alter ast', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/language/__tests__/schema-parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { describe, it } from 'mocha';

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

import { kitchenSinkSDL } from '../../__fixtures__';
import { kitchenSinkSDL } from '../../__fixtures__/index';

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

Expand Down
2 changes: 1 addition & 1 deletion src/language/__tests__/schema-printer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import dedent from '../../jsutils/dedent';
import { parse } from '../parser';
import { print } from '../printer';

import { kitchenSinkSDL } from '../../__fixtures__';
import { kitchenSinkSDL } from '../../__fixtures__/index';

describe('Printer: SDL document', () => {
it('prints minimal ast', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/language/__tests__/visitor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Kind } from '../kinds';
import { parse } from '../parser';
import { visit, visitInParallel, BREAK, QueryDocumentKeys } from '../visitor';

import { kitchenSinkQuery } from '../../__fixtures__';
import { kitchenSinkQuery } from '../../__fixtures__/index';

function checkVisitorFnArgs(ast, args, isEdited) {
const [node, key, parent, path, ancestors] = args;
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/__tests__/buildASTSchema-benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { parse } from '../../language/parser';

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

import { bigSchemaSDL } from '../../__fixtures__';
import { bigSchemaSDL } from '../../__fixtures__/index';

const schemaAST = parse(bigSchemaSDL);

Expand Down
2 changes: 1 addition & 1 deletion src/utilities/__tests__/buildClientSchema-benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

import { bigSchemaIntrospectionResult } from '../../__fixtures__';
import { bigSchemaIntrospectionResult } from '../../__fixtures__/index';

export const name = 'Build Schema from Introspection';
export const count = 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { execute } from '../../execution/execute';
import { buildSchema } from '../buildASTSchema';
import { getIntrospectionQuery } from '../getIntrospectionQuery';

import { bigSchemaSDL } from '../../__fixtures__';
import { bigSchemaSDL } from '../../__fixtures__/index';

const schema = buildSchema(bigSchemaSDL, { assumeValid: true });
const document = parse(getIntrospectionQuery());
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/__tests__/stripIgnoredCharacters-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Lexer } from '../../language/lexer';

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

import { kitchenSinkQuery, kitchenSinkSDL } from '../../__fixtures__';
import { kitchenSinkQuery, kitchenSinkSDL } from '../../__fixtures__/index';

const ignoredTokens = [
// UnicodeBOM ::
Expand Down
2 changes: 1 addition & 1 deletion src/validation/__tests__/validateGQL-benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getIntrospectionQuery } from '../../utilities/getIntrospectionQuery';

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

import { bigSchemaSDL } from '../../__fixtures__';
import { bigSchemaSDL } from '../../__fixtures__/index';

const schema = buildSchema(bigSchemaSDL, { assumeValid: true });
const queryAST = parse(getIntrospectionQuery());
Expand Down
2 changes: 1 addition & 1 deletion src/validation/__tests__/validateInvalidGQL-benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { buildSchema } from '../../utilities/buildASTSchema';

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

import { bigSchemaSDL } from '../../__fixtures__';
import { bigSchemaSDL } from '../../__fixtures__/index';

const schema = buildSchema(bigSchemaSDL, { assumeValid: true });
const queryAST = parse(`
Expand Down
2 changes: 1 addition & 1 deletion src/validation/__tests__/validateSDL-benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { parse } from '../../language/parser';

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

import { bigSchemaSDL } from '../../__fixtures__';
import { bigSchemaSDL } from '../../__fixtures__/index';

const sdlAST = parse(bigSchemaSDL);

Expand Down

0 comments on commit add6c6d

Please sign in to comment.