mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
lint: force if braces, no lonely ifs and add store.js (#3685)
* lint: add curly and no-lonely-if * lint: add lib/app/store.js to eslint config
This commit is contained in:
parent
b2d608746d
commit
1ec5cf7b1c
@ -1,4 +1,5 @@
|
|||||||
app
|
app
|
||||||
|
!app/store.js
|
||||||
node_modules
|
node_modules
|
||||||
dist
|
dist
|
||||||
.nuxt
|
.nuxt
|
||||||
|
@ -53,6 +53,12 @@ module.exports = {
|
|||||||
'ignoreReadBeforeAssign': false
|
'ignoreReadBeforeAssign': false
|
||||||
}],
|
}],
|
||||||
|
|
||||||
|
// No single if in an "else" block
|
||||||
|
'no-lonely-if': 2,
|
||||||
|
|
||||||
|
// Force curly braces for control flow
|
||||||
|
curly: 2,
|
||||||
|
|
||||||
// No async function without await
|
// No async function without await
|
||||||
'require-await': 2,
|
'require-await': 2,
|
||||||
|
|
||||||
|
@ -23,63 +23,61 @@ if (indexFilename) {
|
|||||||
|
|
||||||
// If store is not an exported method = modules store
|
// If store is not an exported method = modules store
|
||||||
if (typeof storeData !== 'function') {
|
if (typeof storeData !== 'function') {
|
||||||
|
|
||||||
// Store modules
|
// Store modules
|
||||||
if (!storeData.modules) {
|
if (!storeData.modules) {
|
||||||
storeData.modules = {}
|
storeData.modules = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let filename of filenames) {
|
for (const filename of filenames) {
|
||||||
let name = filename.replace(/^\.\//, '').replace(/\.(<%= extensions %>)$/, '')
|
let name = filename.replace(/^\.\//, '').replace(/\.(<%= extensions %>)$/, '')
|
||||||
if (name === 'index') continue
|
if (name === 'index') continue
|
||||||
|
|
||||||
let namePath = name.split(/\//)
|
const namePath = name.split(/\//)
|
||||||
|
|
||||||
name = namePath[namePath.length-1]
|
name = namePath[namePath.length - 1]
|
||||||
if (name === 'state' || name === 'getters' || name === 'actions' || name === 'mutations') {
|
if (name === 'state' || name === 'getters' || name === 'actions' || name === 'mutations') {
|
||||||
let module = getModuleNamespace(storeData, namePath, true)
|
const module = getModuleNamespace(storeData, namePath, true)
|
||||||
appendModule(module, filename, name)
|
appendModule(module, filename, name)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
//if file is foo/index.js
|
// if file is foo/index.js
|
||||||
//it should save as foo
|
// it should save as foo
|
||||||
let isIndex = (name === 'index')
|
const isIndex = (name === 'index')
|
||||||
if (isIndex)
|
if (isIndex) {
|
||||||
namePath.pop()
|
namePath.pop()
|
||||||
|
}
|
||||||
|
|
||||||
let module = getModuleNamespace(storeData, namePath)
|
const module = getModuleNamespace(storeData, namePath)
|
||||||
let fileModule = getModule(filename)
|
const fileModule = getModule(filename)
|
||||||
name = namePath.pop()
|
name = namePath.pop()
|
||||||
module[name] = module[name] || {}
|
module[name] = module[name] || {}
|
||||||
|
|
||||||
//if file is foo.js, existing properties take priority
|
// if file is foo.js, existing properties take priority
|
||||||
//because it's the least specific case
|
// because it's the least specific case
|
||||||
if (!isIndex) {
|
if (!isIndex) {
|
||||||
module[name] = Object.assign({}, fileModule, module[name])
|
module[name] = Object.assign({}, fileModule, module[name])
|
||||||
module[name].namespaced = true
|
module[name].namespaced = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
//if file is foo/index.js we want to overwrite properties from foo.js
|
// if file is foo/index.js we want to overwrite properties from foo.js
|
||||||
//but not from appended mods like foo/actions.js
|
// but not from appended mods like foo/actions.js
|
||||||
var appendedMods = {}
|
const appendedMods = {}
|
||||||
if (module[name].appends) {
|
if (module[name].appends) {
|
||||||
appendedMods.appends = module[name].appends
|
appendedMods.appends = module[name].appends
|
||||||
for (let append of module[name].appends)
|
for (const append of module[name].appends) { appendedMods[append] = module[name][append] }
|
||||||
appendedMods[append] = module[name][append]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module[name] = Object.assign({}, module[name], fileModule, appendedMods)
|
module[name] = Object.assign({}, module[name], fileModule, appendedMods)
|
||||||
module[name].namespaced = true
|
module[name].namespaced = true
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// createStore
|
// createStore
|
||||||
export const createStore = storeData instanceof Function ? storeData : () => {
|
export const createStore = storeData instanceof Function ? storeData : () => {
|
||||||
return new Vuex.Store(Object.assign({
|
return new Vuex.Store(Object.assign({
|
||||||
strict: (process.env.NODE_ENV !== 'production'),
|
strict: (process.env.NODE_ENV !== 'production')
|
||||||
}, storeData, {
|
}, storeData, {
|
||||||
state: storeData.state instanceof Function ? storeData.state() : {}
|
state: storeData.state instanceof Function ? storeData.state() : {}
|
||||||
}))
|
}))
|
||||||
@ -100,18 +98,17 @@ function getModule (filename) {
|
|||||||
|
|
||||||
function getModuleNamespace (storeData, namePath, forAppend = false) {
|
function getModuleNamespace (storeData, namePath, forAppend = false) {
|
||||||
if (namePath.length === 1) {
|
if (namePath.length === 1) {
|
||||||
if (forAppend)
|
if (forAppend) { return storeData }
|
||||||
return storeData
|
|
||||||
return storeData.modules
|
return storeData.modules
|
||||||
}
|
}
|
||||||
let namespace = namePath.shift()
|
const namespace = namePath.shift()
|
||||||
storeData.modules[namespace] = storeData.modules[namespace] || {}
|
storeData.modules[namespace] = storeData.modules[namespace] || {}
|
||||||
storeData.modules[namespace].namespaced = true
|
storeData.modules[namespace].namespaced = true
|
||||||
storeData.modules[namespace].modules = storeData.modules[namespace].modules || {}
|
storeData.modules[namespace].modules = storeData.modules[namespace].modules || {}
|
||||||
return getModuleNamespace(storeData.modules[namespace], namePath, forAppend)
|
return getModuleNamespace(storeData.modules[namespace], namePath, forAppend)
|
||||||
}
|
}
|
||||||
|
|
||||||
function appendModule(module, filename, name) {
|
function appendModule (module, filename, name) {
|
||||||
const file = files(filename)
|
const file = files(filename)
|
||||||
module.appends = module.appends || []
|
module.appends = module.appends || []
|
||||||
module.appends.push(name)
|
module.appends.push(name)
|
||||||
|
@ -271,19 +271,17 @@ export const createRoutes = function createRoutes(files, srcDir, pagesDir) {
|
|||||||
child.children = child.children || []
|
child.children = child.children || []
|
||||||
parent = child.children
|
parent = child.children
|
||||||
route.path = ''
|
route.path = ''
|
||||||
|
} else if (key === 'index' && i + 1 === keys.length) {
|
||||||
|
route.path += i > 0 ? '' : '/'
|
||||||
} else {
|
} else {
|
||||||
if (key === 'index' && i + 1 === keys.length) {
|
route.path += '/' +
|
||||||
route.path += i > 0 ? '' : '/'
|
|
||||||
} else {
|
|
||||||
route.path += '/' +
|
|
||||||
(key === '_'
|
(key === '_'
|
||||||
? '*'
|
? '*'
|
||||||
: key.indexOf('_') === 0
|
: key.indexOf('_') === 0
|
||||||
? key.replace('_', ':')
|
? key.replace('_', ':')
|
||||||
: key)
|
: key)
|
||||||
if (key !== '_' && key.indexOf('_') === 0) {
|
if (key !== '_' && key.indexOf('_') === 0) {
|
||||||
route.path += '?'
|
route.path += '?'
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user