Skip to content

Commit

Permalink
Add support for eslint when initializing cloud functions (firebase#242)
Browse files Browse the repository at this point in the history
* added .eslintrc file, lint option in package.json, and lint rules in eslint.json
  • Loading branch information
tinaliang authored Dec 4, 2017
1 parent 90064ff commit 39f94fa
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 3 deletions.
25 changes: 22 additions & 3 deletions lib/init/features/functions/javascript.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
'use strict';

var _ = require('lodash');
var fs = require('fs');
var path = require('path');

var npmDependencies = require('./npm-dependencies');
var prompt = require('../../../prompt');

var TEMPLATE_ROOT = path.resolve(__dirname, '../../../../templates/init/functions/javascript/');
var INDEX_TEMPLATE = fs.readFileSync(path.join(TEMPLATE_ROOT, 'index.js'), 'utf8');
var PACKAGE_TEMPLATE = fs.readFileSync(path.join(TEMPLATE_ROOT, 'package.json'), 'utf8');
var PACKAGE_LINTING_TEMPLATE = fs.readFileSync(path.join(TEMPLATE_ROOT, 'package.lint.json'), 'utf8');
var PACKAGE_NO_LINTING_TEMPLATE = fs.readFileSync(path.join(TEMPLATE_ROOT, 'package.nolint.json'), 'utf8');
var ESLINT_TEMPLATE = fs.readFileSync(path.join(TEMPLATE_ROOT, 'eslint.json'), 'utf8');

module.exports = function(setup, config) {
return config.askWriteProjectFile('functions/package.json', PACKAGE_TEMPLATE).then(function() {
return config.askWriteProjectFile('functions/index.js', INDEX_TEMPLATE);
return prompt(setup.functions, [
{
name: 'lint',
type: 'confirm',
message: 'Do you want to use ESLint to catch probable bugs and enforce style?',
default: true
}
]).then(function() {
if (setup.functions.lint) {
_.set(setup, 'config.functions.predeploy', 'npm --prefix functions run lint');
return config.askWriteProjectFile('functions/package.json', PACKAGE_LINTING_TEMPLATE).then(function() {
config.askWriteProjectFile('functions/.eslintrc.json', ESLINT_TEMPLATE);
});
}
return config.askWriteProjectFile('functions/package.json', PACKAGE_NO_LINTING_TEMPLATE);
}).then(function() {
return config.askWriteProjectFile('functions/src/index.js', INDEX_TEMPLATE);
}).then(function() {
return npmDependencies.askInstallDependencies(setup, config);
});
Expand Down
79 changes: 79 additions & 0 deletions templates/init/functions/javascript/eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"parserOptions": {
// Required for certain syntax usages
"ecmaVersion": 6
},
"plugins": [
"promise"
],
"rules": {
// Warn against comparing against -0
"no-compare-neg-zero": 1,

// Warn against assignment operators in conditional expressions
"no-cond-assign": 1,

// Warn against comma operators
"no-sequences": 1,

// Warn against constant expressions in conditions
"no-constant-condition": 1,

// Warn against control characters in regular expressions
"no-control-regex": 1,

// Warn against duplicate arguments in function definitions
"no-dupe-args": 1,

// Warn against duplicate keys in object literals
"no-dupe-keys": 1,

// Warn against duplicate case labels
"no-duplicate-case": 1,

// Warn against empty block statements
"no-empty": 1,

// Warn against empty character classes in regular expressions
"no-empty-character-class": 1,

// Warn against reassigning exceptions in catch clauses
"no-ex-assign": 1,

// Warn against unnecessary boolean casts
"no-extra-boolean-cast": 1,

// Warn against unnecessary semicolons
"no-extra-semi": 1,

// Warn against reassigning function declarations
"no-func-assign": 1,

// Warn against variable or function declarations in nested blocks
"no-inner-declarations": 1,

// Warn against invalid regular expression strings in RegExp constructors
"no-invalid-regexp": 1,

// Warn against irregular whitespace outside of strings and comments
"no-irregular-whitespace": 1,

// Warn against calling global object properties as functions
"no-obj-calls": 1,

// Warn against confusing multiline expressions
"no-unexpected-multiline": 1,

// Warn against unreachable code after return, throw, continue, and break statements
"no-unreachable": 1,

// Warn against reassigning function parameters
"no-param-reassign": 1,

// Return inside each then() to create readable and reusable Promise chains.
"promise/always-return": 2,

// Prefer async/await to the callback pattern
"promise/prefer-await-to-callbacks": 1
}
}
21 changes: 21 additions & 0 deletions templates/init/functions/javascript/package.lint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "./node_modules/.bin/eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase experimental:functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"firebase-admin": "~5.4.2",
"firebase-functions": "^0.7.1"
},
"devDependencies": {
"eslint": "^4.12.0",
"eslint-plugin-promise": "^3.6.0"
},
"private": true
}

0 comments on commit 39f94fa

Please sign in to comment.