Skip to content

Instantly share code, notes, and snippets.

@a-x-
Last active September 7, 2016 12:17
Show Gist options
  • Select an option

  • Save a-x-/4bcac78166e7bdcaf0ff81ec2204ea79 to your computer and use it in GitHub Desktop.

Select an option

Save a-x-/4bcac78166e7bdcaf0ff81ec2204ea79 to your computer and use it in GitHub Desktop.
Поиск блоков link, button и др. без {mods:{theme,size}} и с потенциально неправильными {elem,mods} #bemhtml #lego #bem ➔ ➔ ➔ MOVED: https://github.com/a-x-/find-ast-objects
#!/usr/env node
// env: VERBOSE
// usage: VERBOSE=1 node find-elem-mods-warns.js | less
// usage: subl -n $(node find-elem-mods-warns.js)
const acorn = require('acorn')
const fs = require('fs')
const Glob = require('glob')
const escodegen = require('escodegen')
const estraverse = require('estraverse')
const _ = require('lodash')
const read = name => fs.readFileSync(name, 'utf8')
const glob = (glob, each) => Glob(glob, (err, files) => files && files.filter(file => file).map(each))
const globPattern = '*/blocks-*/**/*.js'
const findElemModsWarns = module.exports = function findElemModsWarns(globPattern) {
glob(globPattern, file => {
if (file.match(/i18n|deps/)) return
const code = read(file)
const ast = acorn.parse(code, {
locations: true,
})
estraverse.traverse(ast, {
enter: (node, parent) => {
if (node.type !== 'ObjectExpression') return
if (_(node.properties).map('key.name').intersection(['elem', 'mods']).size() !== 2) return
if (_(node.properties).map('key.name').includes('elemMods')) return
// catch it!
console.log(`${file}:${node.loc.start.line}`)
const from = node.loc.start.line - 1
const to = node.loc.end.line
process.env.VERBOSE && console.log(code.split(/\n/).slice(from, to).join('\n'))
}
})
})
}
module.parent || findElemModsWarns(globPattern)
#!/usr/env node
// env: VERBOSE
// usage: VERBOSE=1 node find-mods-errors.js | less
// usage: subl -n $(node find-mods-errors.js)
// todo: add levels: err | warn (mods:mods)
const acorn = require('acorn')
const fs = require('fs')
const Glob = require('glob')
const escodegen = require('escodegen')
const estraverse = require('estraverse')
const _ = require('lodash')
const read = name => fs.readFileSync(name, 'utf8')
const glob = (glob, each) => Glob(glob, (err, files) => files && files.filter(file => file).map(each))
module.exports = function findModsErrors(globPattern, themeDepsBlocks, targetMod) {
glob(globPattern, file => {
if (file.match(/i18n|deps/)) return
const code = read(file)
const ast = acorn.parse(code, {
locations: true,
})
estraverse.traverse(ast, {
enter: (node, parent) => {
if (node.type !== 'ObjectExpression') return
if (!node.properties.filter(p => p.key.name === 'block' && _.includes(themeDepsBlocks, p.value.value)).length) return
if (_(node.properties).map('key.name').intersection(['modName', 'elem']).size()) return
if (_(node.properties).map('key.name').includes('mods')) {
const hasTheme = _.chain(node.properties)
.find(['key.name', 'mods'])
.get('value.properties')
.find(['key.name', targetMod])
.value()
if (hasTheme) return
}
// catch it!
console.log(`${file}:${node.loc.start.line}`)
const from = node.loc.start.line - 1
const to = node.loc.end.line
process.env.VERBOSE && console.log(code.split(/\n/).slice(from, to).join('\n'))
}
})
})
}
#!/usr/env node
// env: VERBOSE
// usage: VERBOSE=1 node find-none-theme-link-blocks.js | less
// usage: subl -n $(node find-none-theme-link-blocks.js)
const globPattern = '*/blocks-*/**/*.js'
const themeDepsBlocks = [
'button',
'button2',
'radiobox',
]
const targetMod = 'size'
require('./find-mods-errors')(globPattern, themeDepsBlocks, targetMod)
#!/usr/env node
// env: VERBOSE
// usage: VERBOSE=1 node find-none-theme-mod-blocks.js | less
// usage: subl -n $(node find-none-theme-mod-blocks.js)
const globPattern = '*/blocks-*/**/*.js'
const themeDepsBlocks = [
'button',
'button2',
'check-button',
'link',
'radio-button',
'radiobox',
'tabs',
'tumbler',
]
const targetMod = 'theme'
require('./find-mods-errors')(globPattern, themeDepsBlocks, targetMod)
#!/usr/env node
// env: VERBOSE
// usage: VERBOSE=1 node find-elem-mods-warns.js | less
// usage: subl -n $(node find-elem-mods-warns.js)
const acorn = require('acorn')
const fs = require('fs')
const Glob = require('glob')
const estraverse = require('estraverse')
const _ = require('lodash')
const read = name => fs.readFileSync(name, 'utf8')
const glob = (glob, each) => Glob(glob, (err, files) => files && files.filter(file => file).map(each))
const globPattern = '*/blocks-*/**/*.js'
const targetBlocks = [
'button',
'button2',
'link',
]
const target = 'target'
const findTargetAttrsErrors = module.exports = function findTargetAttrsErrors(globPattern, targetBlocks, target) {
glob(globPattern, file => {
if (file.match(/i18n|deps/)) return
const code = read(file)
const ast = acorn.parse(code, {
locations: true,
})
estraverse.traverse(ast, {
enter: (node, parent) => {
if (node.type !== 'ObjectExpression') return
if (!node.properties.filter(p => p.key.name === 'block' && _.includes(targetBlocks, p.value.value)).length) return
if (!_(node.properties).map('key.name').includes('attrs')) return
const hasAttrsTarget = _.chain(node.properties)
.find(['key.name', 'attrs'])
.get('value.properties')
.find(['key.name', target])
.value()
if (!hasAttrsTarget) return
// catch it!
console.log(`${file}:${node.loc.start.line}`)
const from = node.loc.start.line - 1
const to = node.loc.end.line
process.env.VERBOSE && console.log(code.split(/\n/).slice(from, to).join('\n'))
}
})
})
}
module.parent || findTargetAttrsErrors(globPattern, targetBlocks, target)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment