Skip to content

Commit ae36c6a

Browse files
committedJul 1, 2021
Refactor code-style
1 parent 5ad7878 commit ae36c6a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+855
-912
lines changed
 

‎doc/options.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ import {engine} from 'unified-engine'
187187
import {remark} from 'remark'
188188
import remarkPresetLintRecommended from 'remark-preset-lint-recommended'
189189

190-
var streamIn = new PassThrough()
190+
const streamIn = new PassThrough()
191191

192192
engine(
193193
{
@@ -238,7 +238,7 @@ import {engine} from 'unified-engine'
238238
import {remark} from 'remark'
239239
import remarkPresetLintRecommended from 'remark-preset-lint-recommended'
240240

241-
var streamIn = new PassThrough()
241+
const streamIn = new PassThrough()
242242

243243
engine(
244244
{

‎lib/configuration.js

+35-34
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import path from 'path'
2-
import yaml from 'js-yaml'
3-
import json from 'parse-json'
2+
import jsYaml from 'js-yaml'
3+
import parseJson from 'parse-json'
44
import createDebug from 'debug'
55
import loadPlugin from 'load-plugin'
6-
import plain from 'is-plain-obj'
6+
import isPlainObj from 'is-plain-obj'
77
import fault from 'fault'
88
import {FindUp} from './find-up.js'
99

10-
var debug = createDebug('unified-engine:configuration')
10+
const debug = createDebug('unified-engine:configuration')
1111

12-
var own = {}.hasOwnProperty
12+
const own = {}.hasOwnProperty
1313

14-
var loaders = {
14+
const loaders = {
1515
'.json': loadJson,
1616
'.cjs': loadScriptOrModule,
1717
'.mjs': loadScriptOrModule,
@@ -20,12 +20,12 @@ var loaders = {
2020
'.yml': loadYaml
2121
}
2222

23-
var defaultLoader = loadJson
23+
const defaultLoader = loadJson
2424

2525
Configuration.prototype.load = load
2626

2727
export function Configuration(options) {
28-
var names = []
28+
const names = []
2929

3030
this.cwd = options.cwd
3131
this.packageField = options.packageField
@@ -58,13 +58,13 @@ export function Configuration(options) {
5858
filePath: options.rcPath,
5959
cwd: options.cwd,
6060
detect: options.detectConfig,
61-
names: names,
61+
names,
6262
create: this.create
6363
})
6464
}
6565

6666
function load(filePath, callback) {
67-
var self = this
67+
const self = this
6868

6969
self.findUp.load(filePath || path.resolve(this.cwd, 'stdin.js'), done)
7070

@@ -73,18 +73,18 @@ function load(filePath, callback) {
7373
return callback(error, file)
7474
}
7575

76-
self.create().then(function (result) {
76+
self.create().then((result) => {
7777
callback(null, result)
7878
}, callback)
7979
}
8080
}
8181

8282
async function create(buf, filePath) {
83-
var self = this
84-
var fn = (filePath && loaders[path.extname(filePath)]) || defaultLoader
85-
var options = {prefix: self.pluginPrefix, cwd: self.cwd}
86-
var result = {settings: {}, plugins: []}
87-
var contents
83+
const self = this
84+
const fn = (filePath && loaders[path.extname(filePath)]) || defaultLoader
85+
const options = {prefix: self.pluginPrefix, cwd: self.cwd}
86+
const result = {settings: {}, plugins: []}
87+
let contents
8888

8989
if (filePath) {
9090
contents = await fn.apply(self, arguments)
@@ -129,11 +129,11 @@ function loadScriptOrModule(_, filePath) {
129129
}
130130

131131
function loadYaml(buf, filePath) {
132-
return yaml.safeLoad(buf, {filename: path.basename(filePath)})
132+
return jsYaml.safeLoad(buf, {filename: path.basename(filePath)})
133133
}
134134

135135
function loadJson(buf, filePath) {
136-
var result = json(buf, filePath)
136+
let result = parseJson(buf, filePath)
137137

138138
if (path.basename(filePath) === 'package.json') {
139139
result = result[this.packageField]
@@ -152,7 +152,7 @@ async function merge(target, raw, options) {
152152
return target
153153

154154
async function addPreset(result) {
155-
var plugins = result.plugins
155+
const plugins = result.plugins
156156

157157
if (plugins === null || plugins === undefined) {
158158
// Empty.
@@ -168,11 +168,10 @@ async function merge(target, raw, options) {
168168
}
169169

170170
async function addEach(result) {
171-
var index = -1
172-
var value
171+
let index = -1
173172

174173
while (++index < result.length) {
175-
value = result[index]
174+
const value = result[index]
176175

177176
// Keep order sequential instead of parallel.
178177
// eslint-disable-next-line no-await-in-loop
@@ -183,12 +182,14 @@ async function merge(target, raw, options) {
183182
}
184183

185184
async function addIn(result) {
186-
var key
185+
let key
187186

188187
for (key in result) {
189-
// Keep order sequential instead of parallel.
190-
// eslint-disable-next-line no-await-in-loop
191-
await use(key, result[key])
188+
if (own.call(result, key)) {
189+
// Keep order sequential instead of parallel.
190+
// eslint-disable-next-line no-await-in-loop
191+
await use(key, result[key])
192+
}
192193
}
193194
}
194195

@@ -203,8 +204,8 @@ async function merge(target, raw, options) {
203204
}
204205

205206
async function addModule(id, value) {
206-
var fp = loadPlugin.resolve(id, {cwd: options.root, prefix: options.prefix})
207-
var result
207+
let fp = loadPlugin.resolve(id, {cwd: options.root, prefix: options.prefix})
208+
let result
208209

209210
if (fp) {
210211
result = await loadFromAbsolutePath(fp, options.root)
@@ -219,7 +220,7 @@ async function merge(target, raw, options) {
219220
Object.assign({}, options, {root: path.dirname(fp)})
220221
)
221222
}
222-
} catch (_) {
223+
} catch {
223224
throw fault(
224225
'Error: Expected preset or plugin, not %s, at `%s`',
225226
result,
@@ -236,7 +237,7 @@ async function merge(target, raw, options) {
236237
}
237238

238239
function addPlugin(result, value) {
239-
var entry = find(target.plugins, result)
240+
const entry = find(target.plugins, result)
240241

241242
if (entry) {
242243
reconfigure(entry, value)
@@ -247,15 +248,15 @@ async function merge(target, raw, options) {
247248
}
248249

249250
function reconfigure(entry, value) {
250-
if (plain(entry[1]) && plain(value)) {
251+
if (isPlainObj(entry[1]) && isPlainObj(value)) {
251252
value = Object.assign({}, entry[1], value)
252253
}
253254

254255
entry[1] = value
255256
}
256257

257258
function find(entries, plugin) {
258-
var index = -1
259+
let index = -1
259260

260261
while (++index < entries.length) {
261262
if (entries[index][0] === plugin) {
@@ -265,8 +266,8 @@ function find(entries, plugin) {
265266
}
266267

267268
function failingModule(id, error) {
268-
var cache = failingModule.cache || (failingModule.cache = {})
269-
var submodule = own.call(cache, id) ? cache[id] : (cache[id] = fail)
269+
const cache = failingModule.cache || (failingModule.cache = {})
270+
const submodule = own.call(cache, id) ? cache[id] : (cache[id] = fail)
270271
return submodule
271272
function fail() {
272273
throw error

0 commit comments

Comments
 (0)
Please sign in to comment.