Fix multiple code style issues

This commit is contained in:
Mostafa Eweda 2012-09-04 13:36:53 +02:00
commit 441ca92f9a

View file

@ -1,138 +1,129 @@
var fs = require("fs"); var fs = require("fs");
var path = require("path");
if (!fs.existsSync) if (!fs.existsSync)
fs.existsSync = path.existsSync; fs.existsSync = require("path").existsSync;
require("amd-loader"); require("amd-loader");
function generateTestData() { function generateTestData() {
var root = Array(5).join("../") + "/demo/kitchen-sink/docs" var root = Array(5).join("../") + "/demo/kitchen-sink/docs";
var docs = fs.readdirSync(root) var docs = fs.readdirSync(root);
var specialDocs = fs.readdirSync(".") var specialDocs = fs.readdirSync(".");
var modes = fs.readdirSync("../").filter(function(x){ var modes = fs.readdirSync("../").filter(function(x){
return /(\w+)_highlight_rules.js$/.test(x) return /^\w+_highlight_rules.js$/.test(x);
}).map(function(x) { }).map(function(x) {
return x.replace(/_highlight_rules.js$/, "") return x.replace(/_highlight_rules.js$/, "");
}); });
console.log(docs) console.log("Docs:", docs.join());
console.log(modes) console.log("Modes:", modes);
docs.forEach(function(docName){ docs.forEach(function(docName){
var p = docName.toLowerCase().split("."), modeName var p = docName.toLowerCase().split(".");
if (modes.indexOf(p[0]) != -1) { var modeName;
modeName = p[0] if (modes.indexOf(p[0]) != -1)
} else if (modes.indexOf(p[1]) != -1) { modeName = p[0];
modeName = p[1] else if (modes.indexOf(p[1]) != -1)
} else { modeName = p[1];
modeName = {"txt": "text", cpp: "c_cpp"}[p[1]] else
} modeName = {"txt": "text", cpp: "c_cpp"}[p[1]];
if (!modeName) { var filePath = "text_" + modeName + ".txt";
return console.error(p)
}
var filePath = "text_" + modeName + ".txt"
if (specialDocs.indexOf(filePath) == -1) { if (specialDocs.indexOf(filePath) == -1) {
filePath = root + "/" + docName filePath = root + "/" + docName;
} }
var text = fs.readFileSync(filePath, "utf8") var text = fs.readFileSync(filePath, "utf8");
var Mode = require("../" + modeName).Mode; var Mode = require("../" + modeName).Mode;
var tokenizer = new Mode().getTokenizer(); var tokenizer = new Mode().getTokenizer();
var state = "start" var state = "start";
var data = text.split(/\n|\r|\r\n]/).map(function(line) { var data = text.split(/\n|\r|\r\n/).map(function(line) {
var tokens = tokenizer.getLineTokens(line, state) var tokens = tokenizer.getLineTokens(line, state);
state = tokens.state state = tokens.state;
tokens = tokens.tokens tokens = tokens.tokens;
return { return {
state: state, state: state,
values: tokens.map(function(x) {return x.value}), values: tokens.map(function(x) {return x.value;}),
types: tokens.map(function(x) {return x.type}) types: tokens.map(function(x) {return x.type;})
} };
}) });
fs.writeFileSync("tokens_" + modeName + ".json", JSON.stringify(data, null, 1), "utf8") fs.writeFileSync("tokens_" + modeName + ".json", JSON.stringify(data, null, 1), "utf8");
}) });
} }
function test(startAt) { function test(startAt) {
var docs = fs.readdirSync(".").filter(function(x){ var docs = fs.readdirSync(".").filter(function(x){
return /^tokens_\w+.json/.test(x) return /^tokens_\w+.json$/.test(x);
}); });
if (startAt && startAt > 1) if (startAt && startAt > 1)
docs = docs.slice(startAt - 1) docs = docs.slice(startAt - 1);
docs.forEach(function(x, i){ docs.forEach(function(x, i){
var modeName = x.match(/tokens_(.*).json/)[1] var modeName = x.match(/tokens_(.*).json/)[1];
console.log(padNumber(i+1, 3) + ") testing: \u001b[33m" + modeName + "\u001b[0m") console.log(padNumber(i+1, 3) + ") testing: \u001b[33m" + modeName + "\u001b[0m");
var text = fs.readFileSync("./" + x, "utf8") var text = fs.readFileSync("./" + x, "utf8");
var data = JSON.parse(text) var data = JSON.parse(text);
var Mode = require("../" + modeName).Mode; var Mode = require("../" + modeName).Mode;
var tokenizer = new Mode().getTokenizer(); var tokenizer = new Mode().getTokenizer();
var state = "start" var state = "start";
data.forEach(function(lineData) { data.forEach(function(lineData) {
var line = lineData.values.join("") var line = lineData.values.join("");
var tokens = tokenizer.getLineTokens(line, state) var tokens = tokenizer.getLineTokens(line, state);
var values = tokens.tokens.map(function(x) {return x.value}) var values = tokens.tokens.map(function(x) {return x.value;});
var types = tokens.tokens.map(function(x) {return x.type}) var types = tokens.tokens.map(function(x) {return x.type;});
var success = true var success = true;
var report = var report =
testEqual([ testEqual([
lineData.state, tokens.state, lineData.state, tokens.state,
lineData.types, types, lineData.types, types,
lineData.values, values]) lineData.values, values]);
state = lineData.state state = lineData.state;
}) });
}) });
console.log("\u001b[32m" + "all ok" + "\u001b[0m") console.log("\u001b[32m" + "all ok" + "\u001b[0m");
function testEqual(a) { function testEqual(a) {
var err var err;
if (a[0] !== a[1]) { if (a[0] !== a[1]) {
console.log(a[0],a[1]) console.log(a[0],a[1]);
err = 1 err = 1;
} }
if ( a[2] + "" !== a[3] + "" if ( a[2] + "" !== a[3] + "" || a[4] + "" !== a[5] + "") {
|| a[4] + "" !== a[5] + "" arrayDiff(a[2],a[3]);
) { arrayDiff(a[4],a[5]);
arrayDiff(a[2],a[3]) err = 1;
arrayDiff(a[4],a[5])
err = 1
} }
if (err) { if (err) {
throw "error" throw "error";
} }
} }
function arrayDiff(a1, a2) { function arrayDiff(a1, a2) {
var l = Math.max(a1.length, a2.length); var l = Math.max(a1.length, a2.length);
var out = [] var out = [];
for (var i = 0; i < l; i++) { for (var i = 0; i < l; i++) {
out.push("\n", padNumber(i+1, 3), ") ") out.push("\n", padNumber(i+1, 3), ") ");
if (a1[i] !== a2[i]) { if (a1[i] !== a2[i])
out.push("\u001b[31m", a1[i], " != ", a2[i], "\u001b[0m") out.push("\u001b[31m", a1[i], " != ", a2[i], "\u001b[0m");
} else { else
out.push(a1[i]) out.push(a1[i]);
}
} }
console.log(out.join("")) console.log(out.join(""));
} }
function padNumber(num, digits) { function padNumber(num, digits) {
return (" " + num).slice(-digits) return (" " + num).slice(-digits);
} }
} }
if (process.argv[2] == "gen") { if (process.argv[2] == "gen")
generateTestData(process.argv.splice(3)) generateTestData(process.argv.splice(3));
} else { else
test(parseInt(process.argv[2],10) || 0) test(parseInt(process.argv[2],10) || 0);
}