Skip to content

Commit b2699d9

Browse files
authored
Merge pull request #18 from 4Catalyzer/prettier
Use Prettier
2 parents 1c9d552 + b7014a7 commit b2699d9

File tree

5 files changed

+1268
-57
lines changed

5 files changed

+1268
-57
lines changed

.eslintrc

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
{
2-
"extends": "4catalyzer"
2+
"extends": [
3+
"4catalyzer",
4+
"prettier"
5+
],
6+
"plugins": [
7+
"prettier"
8+
],
9+
"rules": {
10+
"prettier/prettier": "error"
11+
}
312
}

package.json

+17
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,23 @@
99
"scripts": {
1010
"build": "rimraf lib && babel src -d lib",
1111
"lint": "eslint src test",
12+
"precommit": "lint-staged",
1213
"prepublish": "npm run build",
1314
"tdd": "jest --watch",
1415
"test": "npm run lint && npm run testonly -- --coverage",
1516
"testonly": "jest --runInBand --verbose"
1617
},
18+
"lint-staged": {
19+
"*.js": [
20+
"eslint --fix",
21+
"git add"
22+
]
23+
},
24+
"prettier": {
25+
"printWidth": 79,
26+
"singleQuote": true,
27+
"trailingComma": "all"
28+
},
1729
"jest": {
1830
"collectCoverageFrom": [
1931
"src/**"
@@ -50,9 +62,14 @@
5062
"codecov": "^3.0.0",
5163
"eslint": "^4.10.0",
5264
"eslint-config-4catalyzer": "^0.3.3",
65+
"eslint-config-prettier": "^2.9.0",
5366
"eslint-plugin-import": "^2.8.0",
67+
"eslint-plugin-prettier": "^2.6.0",
5468
"graphql": "^0.11.7",
69+
"husky": "^0.14.3",
5570
"jest": "^21.2.1",
71+
"lint-staged": "^7.0.0",
72+
"prettier": "^1.10.2",
5673
"rimraf": "^2.6.2"
5774
}
5875
}

src/index.js

+39-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import {
2-
getVisitFn, GraphQLError, GraphQLNonNull, GraphQLList, GraphQLObjectType,
2+
getVisitFn,
3+
GraphQLError,
4+
GraphQLNonNull,
5+
GraphQLList,
6+
GraphQLObjectType,
37
} from 'graphql';
48
import * as IntrospectionTypes from 'graphql/type/introspection';
59
import warning from 'warning';
@@ -33,9 +37,8 @@ export class CostCalculator {
3337
return;
3438
}
3539

36-
cost += costFactor * fragmentCalculator.calculateCost(
37-
fragmentCalculators,
38-
);
40+
cost +=
41+
costFactor * fragmentCalculator.calculateCost(fragmentCalculators);
3942
});
4043

4144
this.cost = cost;
@@ -44,14 +47,17 @@ export class CostCalculator {
4447
}
4548

4649
export class ComplexityVisitor {
47-
constructor(context, {
48-
scalarCost = 1,
49-
objectCost = 0,
50-
listFactor = 10,
51-
52-
// Special list factor to make schema queries not have huge costs.
53-
introspectionListFactor = 2,
54-
}) {
50+
constructor(
51+
context,
52+
{
53+
scalarCost = 1,
54+
objectCost = 0,
55+
listFactor = 10,
56+
57+
// Special list factor to make schema queries not have huge costs.
58+
introspectionListFactor = 2,
59+
},
60+
) {
5561
this.context = context;
5662

5763
this.scalarCost = scalarCost;
@@ -105,8 +111,9 @@ export class ComplexityVisitor {
105111
if (type instanceof GraphQLNonNull) {
106112
return this.getTypeCostFactor(type.ofType);
107113
} else if (type instanceof GraphQLList) {
108-
const typeListFactor = this.isIntrospectionList(type) ?
109-
this.introspectionListFactor : this.listFactor;
114+
const typeListFactor = this.isIntrospectionList(type)
115+
? this.introspectionListFactor
116+
: this.listFactor;
110117
return typeListFactor * this.getTypeCostFactor(type.ofType);
111118
}
112119

@@ -141,8 +148,9 @@ export class ComplexityVisitor {
141148
return this.getTypeCost(type.ofType);
142149
}
143150

144-
return type instanceof GraphQLObjectType ?
145-
this.objectCost : this.scalarCost;
151+
return type instanceof GraphQLObjectType
152+
? this.objectCost
153+
: this.scalarCost;
146154
}
147155

148156
getDirectiveValue(directiveName) {
@@ -153,33 +161,34 @@ export class ComplexityVisitor {
153161
return null;
154162
}
155163

156-
const directive = astNode.directives.find(({ name }) => (
157-
name.value === directiveName
158-
));
164+
const directive = astNode.directives.find(
165+
({ name }) => name.value === directiveName,
166+
);
159167
if (!directive) {
160168
return null;
161169
}
162170

163-
const valueArgument = directive.arguments.find(argument => (
164-
argument.name.value === 'value'
165-
));
171+
const valueArgument = directive.arguments.find(
172+
argument => argument.name.value === 'value',
173+
);
166174

167175
if (!valueArgument) {
168176
const fieldName = fieldDef.name;
169177
const parentTypeName = this.context.getParentType().name;
170178

171179
throw new Error(
172180
`No \`value\` argument defined in \`@${directiveName}\` directive ` +
173-
`on \`${fieldName}\` field on \`${parentTypeName}\`.`,
181+
`on \`${fieldName}\` field on \`${parentTypeName}\`.`,
174182
);
175183
}
176184

177185
return parseFloat(valueArgument.value.value);
178186
}
179187

180188
getCalculator() {
181-
return this.currentFragment === null ?
182-
this.rootCalculator : this.fragmentCalculators[this.currentFragment];
189+
return this.currentFragment === null
190+
? this.rootCalculator
191+
: this.fragmentCalculators[this.currentFragment];
183192
}
184193

185194
enterFragmentSpread(node) {
@@ -216,9 +225,8 @@ export function createComplexityLimitRule(
216225
'formatErrorMessage is ignored when createError is specified.',
217226
);
218227

219-
formatErrorMessage = ( // eslint-disable-line no-param-reassign
220-
formatErrorMessage || complexityLimitExceededErrorMessage
221-
);
228+
formatErrorMessage = // eslint-disable-line no-param-reassign
229+
formatErrorMessage || complexityLimitExceededErrorMessage;
222230

223231
return function ComplexityLimit(context) {
224232
const visitor = new ComplexityVisitor(context, options);
@@ -246,9 +254,9 @@ export function createComplexityLimitRule(
246254

247255
if (cost > maxCost) {
248256
context.reportError(
249-
createError ?
250-
createError(cost, node) :
251-
new GraphQLError(formatErrorMessage(cost), [node]),
257+
createError
258+
? createError(cost, node)
259+
: new GraphQLError(formatErrorMessage(cost), [node]),
252260
);
253261
}
254262
}

test/ComplexityVisitor.test.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,7 @@ describe('ComplexityVisitor', () => {
188188

189189
expect(() => {
190190
visit(ast, visitWithTypeInfo(sdlTypeInfo, visitor));
191-
}).toThrow(
192-
/`@cost` directive on `missingCostValue` field on `Query`/,
193-
);
191+
}).toThrow(/`@cost` directive on `missingCostValue` field on `Query`/);
194192
});
195193
});
196194

0 commit comments

Comments
 (0)