Skip to content

Commit

Permalink
refactor(jshint): reduce duplication & test all JS files
Browse files Browse the repository at this point in the history
  • Loading branch information
mgol committed Jul 8, 2014
1 parent 7d4f0d7 commit 36831ec
Show file tree
Hide file tree
Showing 41 changed files with 369 additions and 440 deletions.
2 changes: 2 additions & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/**
lib/htmlparser/**
5 changes: 5 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": ".jshintrc-base",
"node": true,
"globals": {}
}
19 changes: 19 additions & 0 deletions .jshintrc-base
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"bitwise": true,
"immed": true,
"newcap": true,
"noarg": true,
"noempty": true,
"nonew": true,
"trailing": true,
"maxlen": 200,
"boss": true,
"eqnull": true,
"expr": true,
"globalstrict": true,
"laxbreak": true,
"loopfunc": true,
"sub": true,
"undef": true,
"indent": 2
}
11 changes: 10 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var files = require('./angularFiles').files;
var util = require('./lib/grunt/utils.js');
var versionInfo = require('./lib/versions/version-info');
Expand Down Expand Up @@ -107,6 +109,9 @@ module.exports = function(grunt) {
options: {
jshintrc: true,
},
node: {
files: { src: ['*.js', 'lib/**/*.js'] },
},
tests: {
files: { src: 'test/**/*.js' },
},
Expand Down Expand Up @@ -260,7 +265,11 @@ module.exports = function(grunt) {
compress: {
build: {
options: {archive: 'build/' + dist +'.zip', mode: 'zip'},
src: ['**'], cwd: 'build', expand: true, dot: true, dest: dist + '/'
src: ['**'],
cwd: 'build',
expand: true,
dot: true,
dest: dist + '/'
}
},

Expand Down
4 changes: 3 additions & 1 deletion angularFiles.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
angularFiles = {
'use strict';

var angularFiles = {
'angularSrc': [
'src/minErr.js',
'src/Angular.js',
Expand Down
6 changes: 4 additions & 2 deletions changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// TODO(vojta): pre-commit hook for validating messages
// TODO(vojta): report errors, currently Q silence everything which really sucks

'use strict';

var child = require('child_process');
var fs = require('fs');
var util = require('util');
Expand Down Expand Up @@ -164,15 +166,15 @@ var writeChangelog = function(stream, commits, version) {
hash: commit.hash,
closes: []
});
};
}
});

stream.write(util.format(HEADER_TPL, version, version, currentDate()));
printSection(stream, 'Bug Fixes', sections.fix);
printSection(stream, 'Features', sections.feat);
printSection(stream, 'Performance Improvements', sections.perf);
printSection(stream, 'Breaking Changes', sections.breaks, false);
}
};


var getPreviousTag = function() {
Expand Down
6 changes: 5 additions & 1 deletion changelog.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* global describe: false, it: false, expect: false */

'use strict';

describe('changelog.js', function() {
var ch = require('./changelog');

Expand All @@ -13,7 +17,7 @@ describe('changelog.js', function() {
expect(msg.hash).toBe('9b1aff905b638aa274a5fc8f88662df446d374bd');
expect(msg.subject).toBe('broadcast $destroy event on scope destruction');
expect(msg.body).toBe('perf testing shows that in chrome this change adds 5-15% overhead\n' +
'when destroying 10k nested scopes where each scope has a $destroy listener\n')
'when destroying 10k nested scopes where each scope has a $destroy listener\n');
expect(msg.component).toBe('scope');
});

Expand Down
4 changes: 3 additions & 1 deletion compare-master-to-stable.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

'use strict';

var util = require('util');
var cp = require('child_process');

Expand Down Expand Up @@ -146,7 +148,7 @@ then(allInSeries(function (branch) {
return sha + (msg.toLowerCase().indexOf('fix') === -1 ? ' ' : ' * ') + msg;
});
branch.log = log.map(function (line) {
return line.substr(41)
return line.substr(41);
});
return branch;
});
Expand Down
153 changes: 79 additions & 74 deletions gdocs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

'use strict';

var http = require('http');
var https = require('https');
var fs = require('fs');
Expand Down Expand Up @@ -41,63 +43,63 @@ function help() {
console.log('gdocs.js --login <username>');
console.log('gdocs.js --fetch [<docs collection>]');
process.exit(-1);
};
}


function fetch(collection, url){
console.log('fetching a list of docs in collection ' + collection + '...');
request('GET', url, {
headers: {
'Gdata-Version': '3.0',
'Authorization': 'GoogleLogin auth=' + getAuthToken()
}
},
function(chunk){
var entries = chunk.split('<entry');
entries.shift();
entries.forEach(function(entry){
var title = entry.match(/<title>(.*?)<\/title>/)[1];
if (title.match(/\.ngdoc$/)) {
var exportUrl = entry.match(/<content type='text\/html' src='(.*?)'\/>/)[1];
download(collection, title, exportUrl);
};
});
headers: {
'Gdata-Version': '3.0',
'Authorization': 'GoogleLogin auth=' + getAuthToken()
}
);
},
function(chunk){
var entries = chunk.split('<entry');
entries.shift();
entries.forEach(function(entry){
var title = entry.match(/<title>(.*?)<\/title>/)[1];
if (title.match(/\.ngdoc$/)) {
var exportUrl = entry.match(/<content type='text\/html' src='(.*?)'\/>/)[1];
download(collection, title, exportUrl);
}
});
}
);
}

function download(collection, name, url) {
console.log('Downloading:', name, '...');
request('GET', url + '&exportFormat=txt',
{
headers: {
'Gdata-Version': '3.0',
'Authorization': 'GoogleLogin auth=' + getAuthToken()
}
},
function(data){
data = data.replace('\ufeff', '');
data = data.replace(/\r\n/mg, '\n');
{
headers: {
'Gdata-Version': '3.0',
'Authorization': 'GoogleLogin auth=' + getAuthToken()
}
},
function(data){
data = data.replace('\ufeff', '');
data = data.replace(/\r\n/mg, '\n');

// strip out all text annotations
data = data.replace(/\[[a-zA-Z]{1,2}\]/mg, '');
// strip out all text annotations
data = data.replace(/\[[a-zA-Z]{1,2}\]/mg, '');

// strip out all docos comments
data = data.replace(/^[^\s_]+:\n\S+[\S\s]*$/m, '');
// strip out all docos comments
data = data.replace(/^[^\s_]+:\n\S+[\S\s]*$/m, '');

// fix smart-quotes
data = data.replace(/[“”]/g, '"');
data = data.replace(/[‘’]/g, "'");
// fix smart-quotes
data = data.replace(/[“”]/g, '"');
data = data.replace(/[‘’]/g, "'");


data = data + '\n';
data = data + '\n';

//this should be a bug in Google Doc API, hence need to remove this once the bug is fixed
data = data.replace(/\n\n/g, '\n');
//this should be a bug in Google Doc API, hence need to remove this once the bug is fixed
data = data.replace(/\n\n/g, '\n');

fs.writeFileSync('docs/content/' + collection + '/' + name, reflow(data, 100));
}
);
fs.writeFileSync('docs/content/' + collection + '/' + name, reflow(data, 100));
}
);
}

/**
Expand All @@ -111,34 +113,34 @@ function download(collection, name, url) {
*/
function login(username, password){
request('POST', 'https://www.google.com/accounts/ClientLogin',
{
data: {
Email: username,
Passwd: password,
accountType: 'GOOGLE',
service: 'writely',
'Gdata-version': '3.0'
},
headers: {
'Content-type': 'application/x-www-form-urlencoded'
}
{
data: {
Email: username,
Passwd: password,
accountType: 'GOOGLE',
service: 'writely',
'Gdata-version': '3.0'
},
function(chunk){
var token;
chunk.split('\n').forEach(function(line){
var parts = line.split('=');
if (parts[0] == 'Auth') {
token = parts[1];
}
});
if (token) {
fs.writeFileSync('tmp/gdocs.auth', token);
console.log("logged in, token saved in 'tmp/gdocs.auth'");
} else {
console.log('failed to log in');
headers: {
'Content-type': 'application/x-www-form-urlencoded'
}
},
function(chunk){
var token;
chunk.split('\n').forEach(function(line){
var parts = line.split('=');
if (parts[0] == 'Auth') {
token = parts[1];
}
});
if (token) {
fs.writeFileSync('tmp/gdocs.auth', token);
console.log("logged in, token saved in 'tmp/gdocs.auth'");
} else {
console.log('failed to log in');
}
);
}
);
}

function getAuthToken() {
Expand All @@ -152,17 +154,18 @@ function getAuthToken() {
}

function request(method, url, options, response) {
var url = url.match(/http(s?):\/\/(.+?)(\/.*)/);
url = url.match(/http(s?):\/\/(.+?)(\/.*)/);
var isHttps = url[1];
var request = (isHttps ? https : http).request({
var req = (isHttps ? https : http).request({
host: url[2],
port: (url[1] ? 443 : 80),
path: url[3],
method: method
}, function(res){
var data;
switch (res.statusCode) {
case 200:
var data = [];
data = [];
res.setEncoding('utf8');
res.on('end', function () { response(data.join('')); });
res.on('close', function () { response(data.join('')); }); // https
Expand All @@ -173,7 +176,7 @@ function request(method, url, options, response) {
console.log('Eror: Login credentials expired! Please login.');
break;
default:
var data = [];
data = [];
console.log('ERROR: ', res.statusCode);
console.log('REQUEST URL: ', url[0]);
console.log('REQUEST POST: ', options.data);
Expand All @@ -186,14 +189,14 @@ function request(method, url, options, response) {
}
});
for(var header in options.headers) {
request.setHeader(header, options.headers[header]);
req.setHeader(header, options.headers[header]);
}
if (options.data)
request.write(encodeData(options.data));
request.on('end', function() {
req.write(encodeData(options.data));
req.on('end', function() {
console.log('end');
});
request.end();
req.end();
}

function encodeData(obj) {
Expand All @@ -215,7 +218,9 @@ function askPassword(callback) {
stdin.on("data", function(c) {
c = c + "";
switch (c) {
case "\n": case "\r": case "\u0004":
case "\n":
case "\r":
case "\u0004":
stdio.setRawMode(false);
stdin.pause();
callback(password);
Expand All @@ -227,7 +232,7 @@ function askPassword(callback) {
password += c;
break;
}
})
});

}

Expand Down
2 changes: 2 additions & 0 deletions karma-docs.conf.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var sharedConfig = require('./karma-shared.conf');

module.exports = function(config) {
Expand Down
2 changes: 2 additions & 0 deletions karma-jqlite.conf.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var angularFiles = require('./angularFiles');
var sharedConfig = require('./karma-shared.conf');

Expand Down
2 changes: 2 additions & 0 deletions karma-jquery.conf.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var angularFiles = require('./angularFiles');
var sharedConfig = require('./karma-shared.conf');

Expand Down
2 changes: 2 additions & 0 deletions karma-modules.conf.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var angularFiles = require('./angularFiles');
var sharedConfig = require('./karma-shared.conf');

Expand Down
Loading

0 comments on commit 36831ec

Please sign in to comment.