Seite wählen

NETWAYS Blog

Weekly Snap: OSMC last tickets, JSHint & Graphic Design Job

31 Oct – 4 November turned over a new month with a tool to ensure javascript code quality, a job opening for a graphic design student and a case of disappearing OSMC tickets.
Amidst preparations, Pamela warned that tickets to the OSMC are selling out fast. With just 3 weeks to go, this year’s Open Source Monitoring Conference on 29 – 30 November has already filled all places to the intensive workshops on the conference eve. A few GOLD conference packages are available, but register quick as available rooms at the conference hotel, Holiday Inn are vanishing fast. As in previous years, Thomas Krenn will support the OSMC with a hardware exhibition and as a first, the evening dinner and drinks will be held at Alex – atop Nuremberg’s renowned Christmas market.
Pamela then continued to call out for a graphic design student, interested in part time work to produce print and web material, available for an immediate start. Visit the ‘Jobs’ area of our website for more information and to apply.
Lastly, Eric shared his tip to ensure code quality in javascript with JSHint. Equipped with a javascript implementation such as Rhino, JSHint can be used off as well as online. When both are extracted to a folder, code validation can begin. Using an example bit of code with two common mistakes that bring IE7 to its knees, Eric offered a script to automatically insert missing semicolons and remove excess commas. The rest he leaves to you!

Codequalität in JavaScript mit JSHint

Um die Qualität von eigenem oder fremden Code zu überprüfen, gibt es für nahezu jede Sprache entsprechende Tools. Validierung von JavaScript ist zum Beispiel mit Hilfe von JSHint möglich, welches online oder offline eingesetzt werden kann. Für den lokalen Gebrauch werden JSHint und eine JavaScript-Implementation wie Rhino benötigt. Sind beide in einem Ordner entpackt, kann mit der Überprüfung begonnen werden.
Ein Beispielaufruf anhand einer Testdatei, bemängelt zwei Fehler, welche häufig auftreten und zum Beispiel den IE7 in die Knie zwingen:


# cat test.js
var hash = {
    a: 'b',
}
# java -jar js.jar env/rhino.js test.js
Extra comma. (line: 2, character: 11)
> a: 'b',
Missing semicolon. (line: 3, character: 2)
> }

Um automatisch fehlende Semikolons hinzuzufügen und überflüssige Kommas zu entfernen, kann folgendes Skript eingesetzt werden:


#!/bin/sh
test -f $1 && js=$1 ||
js=`find $1 -type f -name '*.js'`
IFS='
'
for file in $js
do
    for comma in `java -jar js.jar env/rhino.js $file | grep 'Extra comma.'`
    do
        line=`echo $comma | awk {'print $4'} | cut -f 1 -d ,`
        column=`echo $comma | awk {'print $6'} | cut -f 1 -d \)`
        column=`echo "$column-1" | bc`
        sed -ri "$line ~ s/^(.{$column}),/\1/" $file
    done
    for semicolon in `java -jar js.jar env/rhino.js $file | grep 'Missing semicolon.'`
    do
        line=`echo $semicolon | awk {'print $4'} | cut -f 1 -d ,`
        column=`echo $semicolon | awk {'print $6'} | cut -f 1 -d \)`
        column=`echo "$column-1" | bc`
        sed -ri "$line ~ s/^(.{$column})/\1;/" $file
    done
done
unset IFS

Ein bisschen Arbeit bleibt einem natürlich selbst überlassen, denn JSHint meckert bei vielen weiteren Fehlern und Verstößen gegen gewisse Guidelines ;-).

Eric Lippmann
Eric Lippmann
CTO

Eric kam während seines ersten Lehrjahres zu NETWAYS und hat seine Ausbildung bereits 2011 sehr erfolgreich abgeschlossen. Seit Beginn arbeitet er in der Softwareentwicklung und dort an den unterschiedlichen NETWAYS Open Source Lösungen, insbesondere inGraph und im Icinga Team an Icinga Web. Darüber hinaus zeichnet er für viele Kundenentwicklungen in der Finanz- und Automobilbranche verantwortlich.