From 0bda5358bdda2c071d552c0b0c5c2da1664311e2 Mon Sep 17 00:00:00 2001 From: DC Date: Wed, 31 Aug 2016 14:13:43 -0700 Subject: [PATCH] Style: extra linting --- bin/extra-lint.js | 47 +++++++++++++++++++ package.json | 5 +- src/main/external-player.js | 8 +++- .../components/unsupported-media-modal.js | 5 +- .../components/update-available-modal.js | 5 +- src/renderer/controllers/media-controller.js | 5 +- .../controllers/playback-controller.js | 11 +++-- .../controllers/torrent-list-controller.js | 12 +++-- src/renderer/lib/cast.js | 3 +- src/renderer/main.js | 18 ++++--- src/renderer/webtorrent.js | 4 +- 11 files changed, 98 insertions(+), 25 deletions(-) create mode 100755 bin/extra-lint.js diff --git a/bin/extra-lint.js b/bin/extra-lint.js new file mode 100755 index 00000000..4ee4d8f0 --- /dev/null +++ b/bin/extra-lint.js @@ -0,0 +1,47 @@ +#!/usr/bin/env node + +const walkSync = require('walk-sync') +const fs = require('fs') +const path = require('path') + +let hasErrors = false + +// Find all Javascript source files +var files = walkSync('src', {globs: ['**/*.js']}) +console.log('Running extra-lint on ' + files.length + ' files...') + +// Read each file, line by line +files.forEach(function (file) { + var filepath = path.join('src', file) + var lines = fs.readFileSync(filepath, 'utf8').split('\n') + + lines.forEach(function (line, i) { + var error + + // Consistent JSX tag closing + if (line.match(/' {2}\/> *$/) || + line.match('[^ ]/> *$') || + line.match(' > *$')) { + error = 'JSX tag spacing' + } + + // No lines over 100 characters + if (line.length > 100) { + error = 'Line >100 chars' + } + + // TODO: No vars + // if (line.match(/^var /) || line.match(/ var /)) { + // error = 'Use const or let' + // } + + if (error) { + let name = path.basename(file) + console.log('%s:%d - %s:\n%s', name, i + 1, error, line) + hasErrors = true + } + }) +}) + +if (hasErrors) process.exit(1) +else console.log('Looks good!') diff --git a/package.json b/package.json index 89c7a85d..a12d8c27 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,8 @@ "plist": "^2.0.1", "rimraf": "^2.5.2", "run-series": "^1.1.4", - "standard": "*" + "standard": "*", + "walk-sync": "^0.3.1" }, "engines": { "node": ">=4.0.0" @@ -98,7 +99,7 @@ "package": "node ./bin/package.js", "prepublish": "npm run build", "start": "npm run build && electron .", - "test": "standard && depcheck --ignores=babel-cli,nodemon,gh-release --ignore-dirs=build,dist", + "test": "standard && depcheck --ignores=babel-cli,nodemon,gh-release --ignore-dirs=build,dist && node ./bin/extra-lint.js", "gh-release": "gh-release", "update-authors": "./bin/update-authors.sh", "watch": "nodemon --exec 'npm run start' --ext js,pug,css" diff --git a/src/main/external-player.js b/src/main/external-player.js index 57f4487f..844a3a3a 100644 --- a/src/main/external-player.js +++ b/src/main/external-player.js @@ -26,7 +26,13 @@ function spawn (path, url, title) { // Try to find and use VLC if external player is not specified vlcCommand(function (err, vlcPath) { if (err) return windows.main.dispatch('externalPlayerNotFound') - var args = ['--play-and-exit', '--video-on-top', '--quiet', `--meta-title=${JSON.stringify(title)}`, url] + var args = [ + '--play-and-exit', + '--video-on-top', + '--quiet', + `--meta-title=${JSON.stringify(title)}`, + url + ] spawnExternal(vlcPath, args) }) } diff --git a/src/renderer/components/unsupported-media-modal.js b/src/renderer/components/unsupported-media-modal.js index ad379b62..7a1ae61c 100644 --- a/src/renderer/components/unsupported-media-modal.js +++ b/src/renderer/components/unsupported-media-modal.js @@ -15,9 +15,10 @@ module.exports = class UnsupportedMediaModal extends React.Component { var playerName = playerPath ? path.basename(playerPath).split('.')[0] : 'VLC' + var onPlay = dispatcher('openExternalPlayer') var actionButton = state.modal.externalPlayerInstalled - ? () - : () + ? () + : () var playerMessage = state.modal.externalPlayerNotFound ? 'Couldn\'t run external player. Please make sure it\'s installed.' : '' diff --git a/src/renderer/components/update-available-modal.js b/src/renderer/components/update-available-modal.js index 49b8759b..b322a7ea 100644 --- a/src/renderer/components/update-available-modal.js +++ b/src/renderer/components/update-available-modal.js @@ -9,7 +9,10 @@ module.exports = class UpdateAvailableModal extends React.Component { return (

A new version of WebTorrent is available: v{state.modal.version}

-

We have an auto-updater for Windows and Mac. We don't have one for Linux yet, so you'll have to download the new version manually.

+

+ We have an auto-updater for Windows and Mac. + We don't have one for Linux yet, so you'll have to download the new version manually. +

diff --git a/src/renderer/controllers/media-controller.js b/src/renderer/controllers/media-controller.js index 128878a2..2994e4c2 100644 --- a/src/renderer/controllers/media-controller.js +++ b/src/renderer/controllers/media-controller.js @@ -47,7 +47,10 @@ module.exports = class MediaController { openExternalPlayer () { var state = this.state var mediaURL = Playlist.getCurrentLocalURL(this.state) - ipcRenderer.send('openExternalPlayer', state.saved.prefs.externalPlayerPath, mediaURL, state.window.title) + ipcRenderer.send('openExternalPlayer', + state.saved.prefs.externalPlayerPath, + mediaURL, + state.window.title) state.playing.location = 'external' } diff --git a/src/renderer/controllers/playback-controller.js b/src/renderer/controllers/playback-controller.js index b1bdada5..f1461b91 100644 --- a/src/renderer/controllers/playback-controller.js +++ b/src/renderer/controllers/playback-controller.js @@ -148,13 +148,16 @@ module.exports = class PlaybackController { rate += 0.25 } else if (direction < 0 && rate > 0.25 && rate <= 2) { rate -= 0.25 - } else if (direction < 0 && rate === 0.25) { /* when we set playback rate at 0 in html 5, playback hangs ;( */ + } else if (direction < 0 && rate === 0.25) { + // When we set playback rate at 0 in html 5, playback hangs ;( rate = -1 } else if (direction > 0 && rate === -1) { rate = 0.25 - } else if ((direction > 0 && rate >= 1 && rate < 16) || (direction < 0 && rate > -16 && rate <= -1)) { + } else if ((direction > 0 && rate >= 1 && rate < 16) || + (direction < 0 && rate > -16 && rate <= -1)) { rate *= 2 - } else if ((direction < 0 && rate > 1 && rate <= 16) || (direction > 0 && rate >= -16 && rate < -1)) { + } else if ((direction < 0 && rate > 1 && rate <= 16) || + (direction > 0 && rate >= -16 && rate < -1)) { rate /= 2 } state.playing.playbackRate = rate @@ -342,7 +345,7 @@ module.exports = class PlaybackController { var result = state.playing.result // 'success' or 'error' if (result === 'success') telemetry.logPlayAttempt('success') // first frame displayed else if (result === 'error') telemetry.logPlayAttempt('error') // codec missing, etc - else if (result === undefined) telemetry.logPlayAttempt('abandoned') // user exited before first frame + else if (result === undefined) telemetry.logPlayAttempt('abandoned') // user gave up waiting else console.error('Unknown state.playing.result', state.playing.result) // Reset the window contents back to the home screen diff --git a/src/renderer/controllers/torrent-list-controller.js b/src/renderer/controllers/torrent-list-controller.js index a218a4d0..8ca3f0f1 100644 --- a/src/renderer/controllers/torrent-list-controller.js +++ b/src/renderer/controllers/torrent-list-controller.js @@ -17,8 +17,8 @@ module.exports = class TorrentListController { this.state = state } - // Adds a torrent to the list, starts downloading/seeding. TorrentID can be a - // magnet URI, infohash, or torrent file: https://github.com/feross/webtorrent#clientaddtorrentid-opts-function-ontorrent-torrent- + // Adds a torrent to the list, starts downloading/seeding. + // TorrentID can be a magnet URI, infohash, or torrent file: https://git.io/vik9M addTorrent (torrentId) { if (torrentId.path) { // Use path string instead of W3C File object @@ -149,7 +149,7 @@ module.exports = class TorrentListController { // remove torrent and poster file deleteFile(TorrentSummary.getTorrentPath(summary)) - deleteFile(TorrentSummary.getPosterPath(summary)) // TODO: will the css path hack affect windows? + deleteFile(TorrentSummary.getPosterPath(summary)) // optionally delete the torrent data if (deleteData) moveItemToTrash(summary) @@ -159,7 +159,8 @@ module.exports = class TorrentListController { State.saveThrottled(this.state) } - this.state.location.clearForward('player') // prevent user from going forward to a deleted torrent + // prevent user from going forward to a deleted torrent + this.state.location.clearForward('player') sound.play('DELETE') } @@ -288,7 +289,8 @@ function saveTorrentFileAs (torrentSummary) { { name: 'All Files', extensions: ['*'] } ] } - electron.remote.dialog.showSaveDialog(electron.remote.getCurrentWindow(), opts, function (savePath) { + var win = electron.remote.getCurrentWindow() + electron.remote.dialog.showSaveDialog(win, opts, function (savePath) { if (!savePath) return // They clicked Cancel var torrentPath = TorrentSummary.getTorrentPath(torrentSummary) fs.readFile(torrentPath, function (err, torrentFile) { diff --git a/src/renderer/lib/cast.js b/src/renderer/lib/cast.js index 8fdf6884..8b07cf6b 100644 --- a/src/renderer/lib/cast.js +++ b/src/renderer/lib/cast.js @@ -350,7 +350,8 @@ function toggleMenu (location) { // Never cast to two devices at the same time if (state.playing.location !== 'local') { - throw new Error('You can\'t connect to ' + location + ' when already connected to another device') + throw new Error('You can\'t connect to ' + location + + ' when already connected to another device') } // Find all cast devices of the given type diff --git a/src/renderer/main.js b/src/renderer/main.js index f2571455..5a3a7a91 100644 --- a/src/renderer/main.js +++ b/src/renderer/main.js @@ -179,12 +179,18 @@ const dispatchHandlers = { 'showCreateTorrent': (paths) => controllers.torrentList.showCreateTorrent(paths), 'createTorrent': (options) => controllers.torrentList.createTorrent(options), 'toggleTorrent': (infoHash) => controllers.torrentList.toggleTorrent(infoHash), - 'toggleTorrentFile': (infoHash, index) => controllers.torrentList.toggleTorrentFile(infoHash, index), - 'confirmDeleteTorrent': (infoHash, deleteData) => controllers.torrentList.confirmDeleteTorrent(infoHash, deleteData), - 'deleteTorrent': (infoHash, deleteData) => controllers.torrentList.deleteTorrent(infoHash, deleteData), - 'toggleSelectTorrent': (infoHash) => controllers.torrentList.toggleSelectTorrent(infoHash), - 'openTorrentContextMenu': (infoHash) => controllers.torrentList.openTorrentContextMenu(infoHash), - 'startTorrentingSummary': (torrentKey) => controllers.torrentList.startTorrentingSummary(torrentKey), + 'toggleTorrentFile': (infoHash, index) => + controllers.torrentList.toggleTorrentFile(infoHash, index), + 'confirmDeleteTorrent': (infoHash, deleteData) => + controllers.torrentList.confirmDeleteTorrent(infoHash, deleteData), + 'deleteTorrent': (infoHash, deleteData) => + controllers.torrentList.deleteTorrent(infoHash, deleteData), + 'toggleSelectTorrent': (infoHash) => + controllers.torrentList.toggleSelectTorrent(infoHash), + 'openTorrentContextMenu': (infoHash) => + controllers.torrentList.openTorrentContextMenu(infoHash), + 'startTorrentingSummary': (torrentKey) => + controllers.torrentList.startTorrentingSummary(torrentKey), // Playback 'playFile': (infoHash, index) => controllers.playback.playFile(infoHash, index), diff --git a/src/renderer/webtorrent.js b/src/renderer/webtorrent.js index e125424e..1ebb4b73 100644 --- a/src/renderer/webtorrent.js +++ b/src/renderer/webtorrent.js @@ -100,8 +100,8 @@ function init () { console.timeEnd('init') } -// Starts a given TorrentID, which can be an infohash, magnet URI, etc. Returns WebTorrent object -// See https://github.com/feross/webtorrent/blob/master/docs/api.md#clientaddtorrentid-opts-function-ontorrent-torrent- +// Starts a given TorrentID, which can be an infohash, magnet URI, etc. +// Returns a WebTorrent object. See https://git.io/vik9M function startTorrenting (torrentKey, torrentID, path, fileModtimes, selections) { console.log('starting torrent %s: %s', torrentKey, torrentID)