Files
meteor/scripts/new-rule.js
2015-10-21 21:26:07 +02:00

222 lines
5.0 KiB
JavaScript

/* eslint-disable no-console */
const fs = require('fs')
const readlineSync = require('readline-sync')
const colors = require('colors/safe')
console.log('Scaffolding new rule. Please give the following details.')
const authorName = readlineSync.question(colors.green('What is your name? '))
const ruleId = readlineSync.question(colors.green('What is the rule ID? '))
const desc = readlineSync.question(colors.green('Type a short description of this rule: '))
const failingExample = readlineSync.question(colors.green('Type a short example of the code that will fail: '))
const escapedFailingExample = failingExample.replace(`'`, `\\'`)
const doc = `# ${desc} (${ruleId})
Please describe the origin of the rule here.
## Rule Details
This rule aims to...
The following patterns are considered warnings:
\`\`\`js
// fill me in
\`\`\`
The following patterns are not warnings:
\`\`\`js
// fill me in
\`\`\`
### Options
If there are any options, describe them here. Otherwise, delete this section.
## When Not To Use It
Give a short description of when it would be appropriate to turn off this rule.
## Further Reading
If there are other links that describe the issue this rule addresses, please include them here in a bulleted list.
`
const rule = `/**
* @fileoverview ${desc}
* @author ${authorName}
* @copyright 2015 ${authorName}. All rights reserved.
* See LICENSE file in root directory for full license.
*/
// -----------------------------------------------------------------------------
// Rule Definition
// -----------------------------------------------------------------------------
import {NON_METEOR} from '../util/environment'
module.exports = getMeta => context => {
const {env} = getMeta(context)
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
// any helper functions should go here or else delete this section
// ---------------------------------------------------------------------------
// Public
// ---------------------------------------------------------------------------
if (env === NON_METEOR) {
return {}
}
return {
// give me methods
}
}
module.exports.schema = [
// fill in your schema
]
`
const test = `/**
* @fileoverview ${desc}
* @author ${authorName}
* @copyright 2015 ${authorName}. All rights reserved.
* See LICENSE file in root directory for full license.
*/
// -----------------------------------------------------------------------------
// Requirements
// -----------------------------------------------------------------------------
import {NON_METEOR, UNIVERSAL, CLIENT, SERVER} from '../../../dist/util/environment'
const rule = require('../../../dist/rules/${ruleId}')
const RuleTester = require('eslint').RuleTester
const commonValidCode = [
]
const commonInvalidCode = [
]
// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------
const ruleTester = new RuleTester()
ruleTester.run('${ruleId}', rule(() => ({env: SERVER})), {
valid: [
...commonValidCode
],
invalid: [
...commonInvalidCode,
{
code: '${escapedFailingExample}',
errors: [
{message: 'The error message', type: 'MemberExpression'}
]
}
]
})
ruleTester.run('${ruleId}', rule(() => ({env: CLIENT})), {
valid: [
...commonValidCode
],
invalid: [
...commonInvalidCode
]
})
ruleTester.run('${ruleId}', rule(() => ({env: UNIVERSAL})), {
valid: [
...commonValidCode
],
invalid: [
...commonInvalidCode
{
code: '${escapedFailingExample}',
errors: [
{message: 'The error message', type: 'MemberExpression'}
]
}
]
})
ruleTester.run('${ruleId}', rule(() => ({env: NON_METEOR})), {
valid: [
...commonValidCode,
...commonInvalidCode
],
invalid: []
})
`
const docFileName = `docs/rules/${ruleId}.md`
const ruleFileName = `lib/rules/${ruleId}.js`
const testFileName = `tests/lib/rules/${ruleId}.js`
const writeOptions = {
encoding: 'utf8',
flag: 'wx'
}
try {
fs.writeFileSync(ruleFileName, rule, writeOptions)
fs.writeFileSync(testFileName, test, writeOptions)
fs.writeFileSync(docFileName, doc, writeOptions)
console.log('')
console.log(colors.green('✓ ') + colors.white('create ' + ruleFileName))
console.log(colors.green('✓ ') + colors.white('create ' + testFileName))
console.log(colors.green('✓ ') + colors.white('create ' + docFileName))
} catch (e) {
if (e.code === 'EEXIST') {
console.log(colors.red(`Aborting because rule already exists (${e.path})`))
// clean up already created files
switch (e.path) {
case ruleFileName:
break
case testFileName:
fs.unlinkSync(ruleFileName)
break
case docFileName:
fs.unlinkSync(ruleFileName)
fs.unlinkSync(testFileName)
break
default:
break
}
} else {
console.log(e)
}
}