1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Fix all jshint warnings in core code

This commit is contained in:
Nick O'Leary 2014-07-01 23:46:25 +01:00
parent c18119f26e
commit 649c82f7d7
9 changed files with 108 additions and 60 deletions

View File

@ -44,11 +44,31 @@ module.exports = function(grunt) {
}, },
all: [ all: [
'Gruntfile.js', 'Gruntfile.js',
'public/red/**/*.js',
'red.js', 'red.js',
'red/**/*.js', 'red/**/*.js',
'nodes/**/*.js', 'nodes/**/*.js',
'public/red/**/*.js'
], ],
core: {
files: {
src: [
'Gruntfile.js',
'red.js',
'red/**/*.js'
]
}
},
nodes: {
files: {
src: [ 'nodes/**/*.js' ]
}
},
editor: {
files: {
src: [ 'public/red/**/*.js' ]
}
},
tests: { tests: {
files: { files: {
src: ['test/*.js'] src: ['test/*.js']
@ -65,6 +85,6 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.registerTask('default', ['jshint:tests','simplemocha']); grunt.registerTask('default', ['jshint:tests','simplemocha']);
grunt.registerTask('all', ['jshint:all','default']); grunt.registerTask('all', ['jshint:core','jshint:nodes','jshint:editor','default']);
}; };

View File

@ -90,7 +90,7 @@ function createLibrary(type) {
}).otherwise(function(err) { }).otherwise(function(err) {
util.log("[red] Error saving library entry '"+path+"' : "+err); util.log("[red] Error saving library entry '"+path+"' : "+err);
res.send(500); res.send(500);
});; });
}); });
}); });
} }

View File

@ -30,9 +30,9 @@ var log = module.exports = {
}, },
log: function(msg) { log: function(msg) {
for (var i in logHandlers) { logHandlers.each(function() {
logHandlers[i].emit("log",msg); logHandlers[i].emit("log",msg);
} });
} }
} }

View File

@ -66,7 +66,7 @@ Node.prototype.send = function(msg) {
} else if (!util.isArray(msg)) { } else if (!util.isArray(msg)) {
msg = [msg]; msg = [msg];
} }
for (var i in this.wires) { for (var i=0;i<this.wires.length;i++) {
var wires = this.wires[i]; var wires = this.wires[i];
if (i < msg.length) { if (i < msg.length) {
if (msg[i] != null) { if (msg[i] != null) {
@ -85,10 +85,10 @@ Node.prototype.send = function(msg) {
// } // }
//} else { //} else {
// Multiple recipients, must send message copies // Multiple recipients, must send message copies
for (var j in wires) { for (var j=0;j<wires.length;j++) {
var node = flows.get(wires[j]); var node = flows.get(wires[j]);
if (node) { if (node) {
for (var k in msgs) { for (var k=0;k<msgs.length;k++) {
var mm = msgs[k]; var mm = msgs[k];
// Temporary fix for #97 // Temporary fix for #97
// TODO: remove this http-node-specific fix somehow // TODO: remove this http-node-specific fix somehow
@ -121,17 +121,23 @@ Node.prototype.receive = function(msg) {
Node.prototype.log = function(msg) { Node.prototype.log = function(msg) {
var o = {level:'log',id:this.id, type:this.type, msg:msg}; var o = {level:'log',id:this.id, type:this.type, msg:msg};
if (this.name) o.name = this.name; if (this.name) {
o.name = this.name;
}
this.emit("log",o); this.emit("log",o);
} }
Node.prototype.warn = function(msg) { Node.prototype.warn = function(msg) {
var o = {level:'warn',id:this.id, type:this.type, msg:msg}; var o = {level:'warn',id:this.id, type:this.type, msg:msg};
if (this.name) o.name = this.name; if (this.name) {
o.name = this.name;
}
this.emit("log",o); this.emit("log",o);
} }
Node.prototype.error = function(msg) { Node.prototype.error = function(msg) {
var o = {level:'error',id:this.id, type:this.type, msg:msg}; var o = {level:'error',id:this.id, type:this.type, msg:msg};
if (this.name) o.name = this.name; if (this.name) {
o.name = this.name;
}
this.emit("log",o); this.emit("log",o);
} }
/** /**

View File

@ -29,7 +29,7 @@ function getCredDef(type) {
} }
function isRegistered(type) { function isRegistered(type) {
return getCredDef(type) != undefined; return getCredDef(type) !== undefined;
} }
function restPOST(type) { function restPOST(type) {
@ -47,17 +47,19 @@ function restPOST(type) {
var definition = getCredDef(nodeType); var definition = getCredDef(nodeType);
for (var cred in definition) { for (var cred in definition) {
if (newCreds[cred] == undefined) { if (definition.hasOwnProperty(cred)) {
if (newCreds[cred] === undefined) {
continue; continue;
} }
if (definition[cred].type == "password" && newCreds[cred] == '__PWRD__') { if (definition[cred].type == "password" && newCreds[cred] == '__PWRD__') {
continue; continue;
} }
if (newCreds[cred] == '') { if (newCreds[cred] === '') {
delete credentials[cred]; delete credentials[cred];
} }
credentials[cred] = newCreds[cred]; credentials[cred] = newCreds[cred];
} }
}
Credentials.add(nodeID, credentials); Credentials.add(nodeID, credentials);
res.send(200); res.send(200);
}); });
@ -70,7 +72,7 @@ function restGET(type) {
var nodeID = req.params.id; var nodeID = req.params.id;
var credentials = Credentials.get(nodeID); var credentials = Credentials.get(nodeID);
if (credentials == undefined) { if (credentials === undefined) {
res.json({}); res.json({});
return; return;
} }
@ -78,13 +80,15 @@ function restGET(type) {
var sendCredentials = {}; var sendCredentials = {};
for (var cred in definition) { for (var cred in definition) {
if (definition.hasOwnProperty(cred)) {
if (definition[cred].type == "password") { if (definition[cred].type == "password") {
var key = 'has' + cred; var key = 'has' + cred;
sendCredentials[key] = credentials[cred] != null && credentials[cred] != ''; sendCredentials[key] = credentials[cred] != null && credentials[cred] !== '';
continue; continue;
} }
sendCredentials[cred] = credentials[cred] || ''; sendCredentials[cred] = credentials[cred] || '';
} }
}
res.json(sendCredentials); res.json(sendCredentials);
}); });
@ -128,12 +132,14 @@ module.exports = {
clean: function (getNode) { clean: function (getNode) {
var deletedCredentials = false; var deletedCredentials = false;
for (var c in credentials) { for (var c in credentials) {
if (credentials.hasOwnProperty(c)) {
var n = getNode(c); var n = getNode(c);
if (!n) { if (!n) {
deletedCredentials = true; deletedCredentials = true;
delete credentials[c]; delete credentials[c];
} }
} }
}
if (deletedCredentials) { if (deletedCredentials) {
storage.saveCredentials(credentials); storage.saveCredentials(credentials);
} }

View File

@ -35,7 +35,7 @@ events.on('type-registered',function(type) {
missingTypes.splice(i,1); missingTypes.splice(i,1);
util.log("[red] Missing type registered: "+type); util.log("[red] Missing type registered: "+type);
} }
if (missingTypes.length == 0) { if (missingTypes.length === 0) {
parseConfig(); parseConfig();
} }
} }
@ -43,20 +43,22 @@ events.on('type-registered',function(type) {
var parseConfig = function() { var parseConfig = function() {
var i;
var nt;
missingTypes = []; missingTypes = [];
for (var i in activeConfig) { for (i=0;i<activeConfig.length;i++) {
var type = activeConfig[i].type; var type = activeConfig[i].type;
// TODO: remove workspace in next release+1 // TODO: remove workspace in next release+1
if (type != "workspace" && type != "tab") { if (type != "workspace" && type != "tab") {
var nt = typeRegistry.get(type); nt = typeRegistry.get(type);
if (!nt && missingTypes.indexOf(type) == -1) { if (!nt && missingTypes.indexOf(type) == -1) {
missingTypes.push(type); missingTypes.push(type);
} }
} }
}; }
if (missingTypes.length > 0) { if (missingTypes.length > 0) {
util.log("[red] Waiting for missing types to be registered:"); util.log("[red] Waiting for missing types to be registered:");
for (var i in missingTypes) { for (i=0;i<missingType.length;i++) {
util.log("[red] - "+missingTypes[i]); util.log("[red] - "+missingTypes[i]);
} }
return; return;
@ -64,11 +66,11 @@ var parseConfig = function() {
util.log("[red] Starting flows"); util.log("[red] Starting flows");
events.emit("nodes-starting"); events.emit("nodes-starting");
for (var i in activeConfig) { for (i=0;i<activeConfig.length;i++) {
var nn = null; var nn = null;
// TODO: remove workspace in next release+1 // TODO: remove workspace in next release+1
if (activeConfig[i].type != "workspace" && activeConfig[i].type != "tab") { if (activeConfig[i].type != "workspace" && activeConfig[i].type != "tab") {
var nt = typeRegistry.get(activeConfig[i].type); nt = typeRegistry.get(activeConfig[i].type);
if (nt) { if (nt) {
try { try {
nn = new nt(activeConfig[i]); nn = new nt(activeConfig[i]);
@ -124,6 +126,7 @@ var flowNodes = module.exports = {
events.emit("nodes-stopping"); events.emit("nodes-stopping");
var promises = []; var promises = [];
for (var n in nodes) { for (var n in nodes) {
if (nodes.hasOwnProperty(n)) {
try { try {
var p = nodes[n].close(); var p = nodes[n].close();
if (p) { if (p) {
@ -133,6 +136,7 @@ var flowNodes = module.exports = {
nodes[n].error(err); nodes[n].error(err);
} }
} }
}
when.settle(promises).then(function() { when.settle(promises).then(function() {
events.emit("nodes-stopped"); events.emit("nodes-stopped");
nodes = {}; nodes = {};
@ -142,8 +146,10 @@ var flowNodes = module.exports = {
}, },
each: function(cb) { each: function(cb) {
for (var n in nodes) { for (var n in nodes) {
if (nodes.hasOwnProperty(n)) {
cb(nodes[n]); cb(nodes[n]);
} }
}
}, },
addLogHandler: function(handler) { addLogHandler: function(handler) {
logHandlers.push(handler); logHandlers.push(handler);

View File

@ -90,13 +90,15 @@ function loadNodesFromModule(moduleDir,pkg) {
var promises = []; var promises = [];
var iconDirs = []; var iconDirs = [];
for (var n in nodes) { for (var n in nodes) {
if (nodes.hasOwnProperty(n)) {
promises.push(loadNode(moduleDir,nodes[n],pkg.name+":"+n)); promises.push(loadNode(moduleDir,nodes[n],pkg.name+":"+n));
var iconDir = path.join(moduleDir,path.dirname(nodes[n]),"icons"); var iconDir = path.join(moduleDir,path.dirname(nodes[n]),"icons");
if (iconDirs.indexOf(iconDir) == -1) { if (iconDirs.indexOf(iconDir) == -1) {
if (fs.existsSync(iconDir)) { if (fs.existsSync(iconDir)) {
events.emit("node-icon-dir",iconDir); events.emit("node-icon-dir",iconDir);
iconDirs.push(iconDir); iconDirs.push(iconDir);
}; }
}
} }
} }
return promises; return promises;
@ -258,8 +260,10 @@ function registerConfig(config) {
var openTag = "<"+el.name; var openTag = "<"+el.name;
var closeTag = "</"+el.name+">"; var closeTag = "</"+el.name+">";
if (el.attribs) { if (el.attribs) {
for (var i in el.attribs) { for (var j in el.attribs) {
openTag += " "+i+'="'+el.attribs[i]+'"'; if (el.attribs.hasOwnProperty(j)) {
openTag += " "+i+'="'+el.attribs[j]+'"';
}
} }
} }
openTag += ">"; openTag += ">";
@ -288,8 +292,8 @@ var typeRegistry = module.exports = {
result += node_configs[i]; result += node_configs[i];
} }
result += '<script type="text/javascript">'; result += '<script type="text/javascript">';
for (var i=0;i<node_scripts.length;i++) { for (var j=0;j<node_scripts.length;j++) {
result += node_scripts[i]; result += node_scripts[j];
} }
result += '</script>'; result += '</script>';
return result; return result;

View File

@ -127,8 +127,10 @@ function writeFile(root,path,meta,body,res) {
var fn = fspath.join(root,path); var fn = fspath.join(root,path);
var headers = ""; var headers = "";
for (var i in meta) { for (var i in meta) {
if (meta.hasOwnProperty(i)) {
headers += "// "+i+": "+meta[i]+"\n"; headers += "// "+i+": "+meta[i]+"\n";
} }
}
mkdirp(fspath.dirname(fn), function (err) { mkdirp(fspath.dirname(fn), function (err) {
fs.writeFile(fn,headers+body,function(err) { fs.writeFile(fn,headers+body,function(err) {
//TODO: handle error //TODO: handle error
@ -255,7 +257,9 @@ var localfilesystem = {
var rootPath = fspath.join(libDir,type,path); var rootPath = fspath.join(libDir,type,path);
return promiseDir(root).then(function () { return promiseDir(root).then(function () {
return nodeFn.call(fs.lstat, rootPath).then(function(stats) { return nodeFn.call(fs.lstat, rootPath).then(function(stats) {
if (stats.isFile()) return getFileBody(root,path); if (stats.isFile()) {
return getFileBody(root,path);
}
if (path.substr(-1) == '/') { if (path.substr(-1) == '/') {
path = path.substr(0,path.length-1); path = path.substr(0,path.length-1);
} }
@ -286,8 +290,10 @@ var localfilesystem = {
var fn = fspath.join(libDir, type, path); var fn = fspath.join(libDir, type, path);
var headers = ""; var headers = "";
for (var i in meta) { for (var i in meta) {
if (meta.hasOwnProperty(i)) {
headers += "// "+i+": "+meta[i]+"\n"; headers += "// "+i+": "+meta[i]+"\n";
} }
}
return promiseDir(fspath.dirname(fn)).then(function () { return promiseDir(fspath.dirname(fn)).then(function () {
nodeFn.call(fs.writeFile, fn, headers+body); nodeFn.call(fs.writeFile, fn, headers+body);
}); });

View File

@ -49,7 +49,7 @@ function setupUI(settings) {
if (iconCache[req.params.icon]) { if (iconCache[req.params.icon]) {
res.sendfile(iconCache[req.params.icon]); res.sendfile(iconCache[req.params.icon]);
} else { } else {
for (var p in icon_paths) { for (var p=0;p<icon_paths.length;p++) {
var iconPath = path.join(icon_paths[p],req.params.icon); var iconPath = path.join(icon_paths[p],req.params.icon);
if (fs.existsSync(iconPath)) { if (fs.existsSync(iconPath)) {
res.sendfile(iconPath); res.sendfile(iconPath);