Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jsdoc to linting rules #223

Merged
merged 5 commits into from
Dec 19, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions __tests__/lib/__snapshots__/build.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

exports[`build development should transform css 1`] = `86`;

exports[`build development should transform js 1`] = `157929`;
exports[`build development should transform js 1`] = `158112`;

exports[`build production should transform and minify js and css 1`] = `156127`;
exports[`build production should transform and minify js and css 1`] = `156219`;

exports[`build production should transform and minify js and css 2`] = `86`;
4 changes: 4 additions & 0 deletions __tests__/test-utils/mocks/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import uuid from 'uuid'
import png from '../../../mastarm.png'

/** A test component */
export default class MockTestComponentUniqueName {
static defaultProps = {
test: 'hi'
}

/**
* Render the component.
*/
render () {
return <div />
}
Expand Down
3 changes: 3 additions & 0 deletions lib/browserify.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const transform = require('./js-transform')

module.exports = browserifyIt

/**
* Bundle some js together with browserify
*/
function browserifyIt ({config, entry, env, minify}) {
return browserify(entry, {
basedir: process.cwd(),
Expand Down
6 changes: 6 additions & 0 deletions lib/css-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ const base64ify = postcss.plugin('postcss-base64ify', function () {
}
})

/**
* Extract the contents of a css url
*
* @param {string} value raw css
* @return {string} the contents of the url
*/
function getUrl (value) {
const reg = /url\((\s*)(['"]?)(.+?)\2(\s*)\)/g
const match = reg.exec(value)
Expand Down
7 changes: 7 additions & 0 deletions lib/eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
"prefer-const": ["warn", {
"destructuring": "all",
"ignoreReadBeforeAssign": false
}],
"require-jsdoc": ["error", {
"require": {
"FunctionDeclaration": true,
"MethodDefinition": true,
"ClassDeclaration": true
}
}]
},
"settings": {
Expand Down
6 changes: 6 additions & 0 deletions lib/flyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ module.exports = function (req, res) {
})
}

/**
* Send the default image and log an error
*/
function logAndSend ({err, res}) {
logger.error('flyle >> sending default image: ', err.message)
sendImg({
Expand All @@ -54,6 +57,9 @@ function logAndSend ({err, res}) {
})
}

/**
* Respond with an image
*/
function sendImg ({path, res}) {
res.writeHead(200, {
'Content-Type': 'image/png'
Expand Down
3 changes: 3 additions & 0 deletions lib/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ module.exports = function format (files) {
files = ['__mocks__', '__tests__', 'bin', 'lib', 'src', './']
}

/**
* Run the linter.lintText function as a promise with provide prettified text.
*/
function promisifiedLint (prettified) {
return new Promise((resolve, reject) => {
linter.lintText(prettified, {fix: true}, (err, standardized) => {
Expand Down
12 changes: 12 additions & 0 deletions lib/js-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ module.exports = function transform ({config, env}) {
]
}

/**
* Transform a svg file to a module containing a JSON string
*/
function svgToString (filename) {
if (!/\.svg$/i.test(filename)) {
return through()
Expand All @@ -44,6 +47,9 @@ function svgToString (filename) {
})
}

/**
* Transform an image to a module containing a base64 string.
*/
function imagesToBase64String (filename) {
if (!/\.png|\.jpg|\.jpeg|\.gif$/i.test(filename)) {
return through()
Expand All @@ -55,6 +61,9 @@ function imagesToBase64String (filename) {
})
}

/**
* Transform an html file to a module containing a JSON string
*/
function htmlTransform (filename) {
if (!/\.html$/i.test(filename)) {
return through()
Expand All @@ -66,6 +75,9 @@ function htmlTransform (filename) {
})
}

/**
* Transform an YAML file to a module containing a JSON string
*/
function yamlTransform (filename) {
if (!/\.yml|\.yaml$/i.test(filename)) {
return through()
Expand Down
4 changes: 3 additions & 1 deletion lib/lint-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ const IMPORT = /import ([a-zA-Z0-9_$]+)?,? ?(\{ ?(?:[a-zA-Z0-9_$]+(?: as [a-zA-Z
// first group and the name (if applicable) on the second.
const NAMED_IMPORTS = /([a-zA-Z0-9_$]+)(?: as ([a-zA-Z0-9_$]+))?/g

// parse a line to determine if it is an import of messages
/**
* parse a line to determine if it is an import of messages
*/
function parseImportLine (line) {
if (IS_IMPORT.test(line)) {
// could be either depending on whether default import was before or after named imports
Expand Down
11 changes: 11 additions & 0 deletions lib/load-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ module.exports = function load (cwd, config, environment) {
stylePath: findFile('style.css')
}

/**
* Try to find a config file.
* Look first in the configDirectory and then fallback to the defaultDirectory.
* If the file is still not found, return null.
*/
function findFile (filename) {
const file = configDirectory + '/' + filename
if (fs.existsSync(file)) return file
Expand All @@ -26,12 +31,18 @@ module.exports = function load (cwd, config, environment) {
return null
}

/**
* Return the JSON of a YAML file if it found, otherwise return an empty object.
*/
function loadYaml (filename) {
const file = findFile(`${filename}.yml`)
return file ? YAML.parse(fs.readFileSync(file, 'utf8')) : {}
}
}

/**
* If an environments key exists within a config object, use that data instead.
*/
function overrideWithEnvironment (object, environment) {
if (object.environments && object.environments[environment]) {
const newObject = Object.assign(
Expand Down
6 changes: 6 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ module.exports.logToSlack = ({channel, webhook}) => {
}
}

/**
* Emojify text outside of html
*/
function emojify (text) {
const strippedLinks = text.replace(/<[^|>]+\|([^>]+)>/g, '$1')
return nodeEmoji.emojify(strippedLinks)
}

/**
* Send a message to slack by making a request to a webhook.
*/
function notifySlack ({channel, text, webhook}) {
return fetch(webhook, {
method: 'POST',
Expand Down
6 changes: 6 additions & 0 deletions lib/prepublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ module.exports = function ({entries, env, outdir}) {
}, [])
}

/**
* Transform all of the files in a directory recursively.
*/
function transformDir ({config, entry, outdir}) {
return glob.sync(`${entry[0]}/**/*.js`).map(filename =>
transformFile({
Expand All @@ -31,6 +34,9 @@ function transformDir ({config, entry, outdir}) {
)
}

/**
* Transform a file with babel and also write the sourcemap for the file.
*/
function transformFile ({config, entry, outdir}) {
const filename = entry[0]
const filepath = entry[1] || `${outdir}/${filename}`
Expand Down
10 changes: 10 additions & 0 deletions lib/push-to-s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const logger = require('./logger')
module.exports = ({s3bucket, cloudfront}) => ({body, outfile}) =>
upload({body, s3bucket, cloudfront, outfile})

/**
* Upload the contents of a file to s3.
* Also, invalidate the respective cloudfront path if instructed to do so.
*/
function upload ({body, s3bucket, cloudfront, outfile}) {
const bucketUrl = `https://s3.amazonaws.com/${s3bucket}`
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -61,6 +65,9 @@ function upload ({body, s3bucket, cloudfront, outfile}) {
}
})

/**
* Helper function to log a successful upload to s3.
*/
function done () {
logger
.log(`:checkered_flag: *uploaded:* ${bucketLink} (${bytes})`)
Expand All @@ -69,6 +76,9 @@ function upload ({body, s3bucket, cloudfront, outfile}) {
})
}

/**
* Pretty print the size of the number of bytes.
*/
function bytesToSize (bytes) {
const sizes = ['bytes', 'kb', 'mb', 'gb', 'tb']
if (bytes === 0) return '0 byte'
Expand Down
15 changes: 15 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ exports.assertEntriesExist = function (entries) {
})
}

/**
* Calculate a list of globbed files based on a list of files, folders or glob patterns.
*/
exports.cliFileEntriesToArray = function (files, failOnMissingFile) {
const foundFiles = []
const missingFiles = []

/**
* Add the file to the foundFiles or missingFiles arrays based on their existance.
*/
function classifyFile (file) {
return stat(file).then(({err, stats}) => {
if (err) {
Expand All @@ -37,6 +43,9 @@ exports.cliFileEntriesToArray = function (files, failOnMissingFile) {
})
}

/**
* Use the `glob` function on a file, but return a promise
*/
function globPromise (file) {
return new Promise((resolve, reject) => {
glob(file, (err, files) => {
Expand All @@ -49,6 +58,12 @@ exports.cliFileEntriesToArray = function (files, failOnMissingFile) {
})
}

/**
* Glob a path.
* If the path is a file, return the path
* If the path is a directory, return the promisified glob of the directory
* Throw an error if the path does no exist
*/
function globFile (file) {
return stat(file).then(({err, stats}) => {
if (err) throw err
Expand Down