Skip to content

Commit 3a993d8

Browse files
authored
Merge pull request #1104 from swagger-api/enforce-eslint
Enforce ESLint rules
2 parents d73a196 + 0057578 commit 3a993d8

File tree

18 files changed

+237
-232
lines changed

18 files changed

+237
-232
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"build": "NODE_ENV=production webpack -p --config ./webpack.config.js",
2020
"build-bundle": "NODE_ENV=production webpack -p --config ./webpack.bundle.config.js",
2121
"watch": "webpack --config webpack.config.js --watch --progress",
22-
"test": "NODE_ENV=test node ./node_modules/.bin/_mocha --recursive --compilers js:babel-core/register",
22+
"test": "npm run just-test && npm run lint",
23+
"just-test": "NODE_ENV=test node ./node_modules/.bin/_mocha --recursive --compilers js:babel-core/register",
2324
"test:watch": "npm run test -- -w",
2425
"lint": "eslint src/ test/",
2526
"deps-license": "license-checker --production --csv --out $npm_package_config_deps_check_dir/licenses.csv && license-checker --development --csv --out $npm_package_config_deps_check_dir/licenses-dev.csv",

src/execute.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const arrayOrEmpty = (ar) => {
1212
return Array.isArray(ar) ? ar : []
1313
}
1414

15-
const OperationNotFoundError = createError('OperationNotFoundError', function(message, extra, oriError) {
15+
const OperationNotFoundError = createError('OperationNotFoundError', function (message, extra, oriError) {
1616
this.originalError = oriError
1717
Object.assign(this, extra || {})
1818
})
@@ -117,7 +117,7 @@ export function buildRequest({
117117
const builder = parameterBuilders[parameter.in]
118118
let value
119119

120-
if(parameter.in === 'body' && parameter.schema && parameter.schema.properties) {
120+
if (parameter.in === 'body' && parameter.schema && parameter.schema.properties) {
121121
value = parameters
122122
}
123123

@@ -142,14 +142,18 @@ export function buildRequest({
142142
if (req.body || req.form) {
143143
if (requestContentType) {
144144
req.headers['content-type'] = requestContentType
145-
} else if (Array.isArray(operation.consumes)) {
145+
}
146+
else if (Array.isArray(operation.consumes)) {
146147
req.headers['content-type'] = operation.consumes[0]
147-
} else if (Array.isArray(spec.consumes)) {
148+
}
149+
else if (Array.isArray(spec.consumes)) {
148150
req.headers['content-type'] = spec.consumes[0]
149-
} else if (operation.parameters.filter((p)=> p.type === "file").length) {
150-
req.headers['content-type'] = "multipart/form-data"
151-
} else if (operation.parameters.filter((p)=> p.in === "formData").length) {
152-
req.headers['content-type'] = "application/x-www-form-urlencoded"
151+
}
152+
else if (operation.parameters.filter(p => p.type === 'file').length) {
153+
req.headers['content-type'] = 'multipart/form-data'
154+
}
155+
else if (operation.parameters.filter(p => p.in === 'formData').length) {
156+
req.headers['content-type'] = 'application/x-www-form-urlencoded'
153157
}
154158
}
155159

src/helpers.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import isObject from 'lodash/isObject'
22

33
const toLower = str => String.prototype.toLowerCase.call(str)
4-
const escapeString = str => {
4+
const escapeString = (str) => {
55
return str.replace(/[^\w]/gi, '_')
66
}
77

88
// Strategy for determining operationId
99
export function opId(operation, pathName, method = '') {
1010
const idWithoutWhitespace = (operation.operationId || '').replace(/\s/g, '')
11-
if(idWithoutWhitespace.length) {
11+
if (idWithoutWhitespace.length) {
1212
return escapeString(operation.operationId)
13-
} else {
14-
return idFromPathMethod(pathName, method)
1513
}
14+
return idFromPathMethod(pathName, method)
1615
}
1716

1817

@@ -124,19 +123,18 @@ export function normalizeSwagger(parsedSpec) {
124123
Object.keys(map).forEach((op) => {
125124
if (map[op].length > 1) {
126125
map[op].forEach((o, i) => {
127-
o.__originalOperationId = o.__originalOperationId || o.operationId
128-
o.operationId = `${op}${i+1}`
126+
o.__originalOperationId = o.__originalOperationId || o.operationId
127+
o.operationId = `${op}${i + 1}`
129128
})
130-
} else {
131-
// Ensure we always add the normalized operation ID if one already exists ( potentially different, given that we normalize our IDs)
129+
}
130+
else if (typeof operation.operationId !== 'undefined') {
131+
// Ensure we always add the normalized operation ID if one already exists
132+
// ( potentially different, given that we normalize our IDs)
132133
// ... _back_ to the spec. Otherwise, they might not line up
133-
if(typeof operation.operationId !== 'undefined') {
134-
let obj = map[op][0]
135-
obj.__originalOperationId = obj.__originalOperationId || operation.operationId
136-
obj.operationId = op
137-
}
134+
const obj = map[op][0]
135+
obj.__originalOperationId = obj.__originalOperationId || operation.operationId
136+
obj.operationId = op
138137
}
139-
140138
})
141139
}
142140

src/http.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default function http(url, request = {}) {
3737
delete request.headers['Content-Type']
3838
}
3939

40-
return fetch(request.url, request).then((res) => {
40+
return fetch(request.url, request).then((res) => { // eslint-disable-line no-undef
4141
const serialized = self.serializeRes(res, url, request).then((_res) => {
4242
if (request.responseInterceptor) {
4343
_res = request.responseInterceptor(_res) || _res
@@ -65,7 +65,7 @@ export default function http(url, request = {}) {
6565
}
6666

6767
// exported for testing
68-
export const shouldDownloadAsText = (contentType = "") => /json|xml|yaml|text/.test(contentType)
68+
export const shouldDownloadAsText = (contentType = '') => /json|xml|yaml|text/.test(contentType)
6969

7070
// Serialize the response, returns a promise with headers and the body part of the hash
7171
export function serializeRes(oriRes, url, {loadSpec = false} = {}) {
@@ -128,7 +128,7 @@ export function serializeHeaders(headers = {}) {
128128

129129
function isFile(obj) {
130130
if (typeof File !== 'undefined') {
131-
return obj instanceof File
131+
return obj instanceof File // eslint-disable-line no-undef
132132
}
133133
return obj !== null && typeof obj === 'object' && typeof obj.pipe === 'function'
134134
}

src/index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import cloneDeep from 'lodash/cloneDeep'
22
import assign from 'lodash/assign'
3+
import Url from 'url'
34
import Http, {makeHttp, serializeRes, serializeHeaders} from './http'
45
import Resolver, {clearCache} from './resolver'
5-
import url from 'url'
66
import {makeApisTagOperation} from './interfaces'
77
import {execute, buildRequest, PARAMETER_BUILDERS} from './execute'
88
import {opId} from './helpers'
@@ -78,18 +78,18 @@ Swagger.prototype = {
7878
}
7979
}
8080

81-
Swagger.prototype.applyDefaults = function() {
82-
var spec = this.spec
83-
var specUrl = this.url
84-
if(specUrl && specUrl.startsWith('http')) {
85-
var parsed = url.parse(specUrl)
86-
if(!spec.host) {
81+
Swagger.prototype.applyDefaults = function () {
82+
const spec = this.spec
83+
const specUrl = this.url
84+
if (specUrl && specUrl.startsWith('http')) {
85+
const parsed = Url.parse(specUrl)
86+
if (!spec.host) {
8787
spec.host = parsed.host
8888
}
89-
if(!spec.schemes) {
90-
spec.schemes = [parsed.protocol.replace(':','')]
89+
if (!spec.schemes) {
90+
spec.schemes = [parsed.protocol.replace(':', '')]
9191
}
92-
if(!spec.basePath) {
92+
if (!spec.basePath) {
9393
spec.basePath = '/'
9494
}
9595
}

src/interfaces.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,23 @@ export function mapTagOperations({spec, cb = nullFn, defaultTag = 'default'}) {
8181
const id = opId(operation, pathName, method)
8282
const cbResult = cb({spec, pathName, method, operation, operationId: id})
8383

84-
if(operationIdCounter[id]) {
85-
operationIdCounter[id] = operationIdCounter[id] + 1
84+
if (operationIdCounter[id]) {
85+
operationIdCounter[id]++
8686
tagObj[`${id}${operationIdCounter[id]}`] = cbResult
87-
} else if (typeof tagObj[id] !== 'undefined') {
87+
}
88+
else if (typeof tagObj[id] !== 'undefined') {
8889
// Bump counter ( for this operationId )
89-
let originalCounterValue = (operationIdCounter[id] || 1)
90+
const originalCounterValue = (operationIdCounter[id] || 1)
9091
operationIdCounter[id] = originalCounterValue + 1
9192
// Append _x to the operationId
9293
tagObj[`${id}${operationIdCounter[id]}`] = cbResult
9394

9495
// Rename the first operationId
95-
let temp = tagObj[id]
96+
const temp = tagObj[id]
9697
delete tagObj[id]
9798
tagObj[`${id}${originalCounterValue}`] = temp
98-
} else {
99+
}
100+
else {
99101
// Assign callback result ( usually a bound function )
100102
tagObj[id] = cbResult
101103
}

src/resolver.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export function clearCache() {
2323
plugins.refs.clearCache()
2424
}
2525

26-
export default function resolve({http, fetch, spec, url, baseDoc, mode, allowMetaPatches = true, modelPropertyMacro, parameterMacro}) {
26+
export default function resolve({
27+
http, fetch, spec, url, baseDoc, mode, allowMetaPatches = true,
28+
modelPropertyMacro, parameterMacro
29+
}) {
2730
// @TODO Swagger-UI uses baseDoc instead of url, this is to allow both
2831
// need to fix and pick one.
2932
baseDoc = baseDoc || url

src/specmap/lib/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ function applyPatch(obj, patch, opts) {
4848
deepExtend(valPatch.value, patch.value)
4949

5050
// deepExtend doesn't merge arrays, so we will do it manually
51-
for ( let prop in patch.value ) {
52-
if ( patch.value.hasOwnProperty(prop) ) {
51+
for (const prop in patch.value) {
52+
if (Object.prototype.hasOwnProperty.call(patch.value, prop)) {
5353
const propVal = patch.value[prop]
54-
if ( Array.isArray(propVal) ) {
55-
let existing = origValPatchValue[prop] || []
56-
valPatch.value[prop] = existing.concat( propVal )
54+
if (Array.isArray(propVal)) {
55+
const existing = origValPatchValue[prop] || []
56+
valPatch.value[prop] = existing.concat(propVal)
5757
}
5858
}
5959
}

src/specmap/lib/parameters.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@ export default {
44
key: 'parameters',
55
plugin: (parameters, key, fullPath, specmap, patch) => {
66
if (Array.isArray(parameters) && parameters.length) {
7-
let val = Object.assign([], parameters)
8-
let opPath = fullPath.slice(0, -1)
9-
let op = Object.assign({}, lib.getIn(specmap.spec, opPath))
7+
const val = Object.assign([], parameters)
8+
const opPath = fullPath.slice(0, -1)
9+
const op = Object.assign({}, lib.getIn(specmap.spec, opPath))
1010

11-
parameters.forEach((param, i)=>{
11+
parameters.forEach((param, i) => {
1212
try {
1313
val[i].default = specmap.parameterMacro(op, param)
14-
} catch(e) {
14+
}
15+
catch (e) {
1516
const err = new Error(e)
1617
err.fullPath = fullPath
1718
return err
1819
}
1920
})
2021

21-
const patch = lib.replace(fullPath, val)
22-
23-
return patch
22+
return lib.replace(fullPath, val)
2423
}
2524

2625
return lib.replace(fullPath, parameters)

src/specmap/lib/properties.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@ import lib from '../lib'
33
export default {
44
key: 'properties',
55
plugin: (properties, key, fullPath, specmap) => {
6-
let val = Object.assign({}, properties)
6+
const val = Object.assign({}, properties)
77

8-
for (let k in properties) {
8+
for (const k in properties) {
99
try {
1010
val[k].default = specmap.modelPropertyMacro(val[k])
11-
} catch(e) {
11+
}
12+
catch (e) {
1213
const err = new Error(e)
1314
err.fullPath = fullPath // This is an array
1415
return err
1516
}
16-
1717
}
1818

1919
const patch = lib.replace(fullPath, val)
2020

2121
return patch
22-
2322
}
2423
}

0 commit comments

Comments
 (0)