diff --git a/external/builder/.eslintrc b/external/builder/.eslintrc new file mode 100644 index 000000000..4a0b538f6 --- /dev/null +++ b/external/builder/.eslintrc @@ -0,0 +1,10 @@ +{ + "extends": [ + "../.eslintrc" + ], + + "rules": { + // ECMAScript 6 + "no-var": "error", + }, +} diff --git a/external/builder/builder.js b/external/builder/builder.js index 5a47d8d65..a3ed4e956 100644 --- a/external/builder/builder.js +++ b/external/builder/builder.js @@ -33,6 +33,11 @@ const fs = require("fs"), * //#endif */ function preprocess(inFilename, outFilename, defines) { + let lineNumber = 0; + function loc() { + return fs.realpathSync(inFilename) + ":" + lineNumber; + } + // TODO make this really read line by line. const lines = fs.readFileSync(inFilename).toString().split("\n"); const totalLines = lines.length; @@ -118,10 +123,7 @@ function preprocess(inFilename, outFilename, defines) { let state = STATE_NONE; const stack = []; const control = /^(?:\/\/|)?$)?/; - let lineNumber = 0; - var loc = function () { - return fs.realpathSync(inFilename) + ":" + lineNumber; - }; + while ((line = readLine()) !== null) { ++lineNumber; const m = control.exec(line); @@ -277,11 +279,11 @@ exports.preprocessCSS = preprocessCSS; * the first. */ function merge(defaults, defines) { - const ret = {}; - for (var key in defaults) { + const ret = Object.create(null); + for (const key in defaults) { ret[key] = defaults[key]; } - for (key in defines) { + for (const key in defines) { ret[key] = defines[key]; } return ret; diff --git a/external/builder/preprocessor2.js b/external/builder/preprocessor2.js index 5fa087154..b918b1df2 100644 --- a/external/builder/preprocessor2.js +++ b/external/builder/preprocessor2.js @@ -34,14 +34,14 @@ function handlePreprocessorAction(ctx, actionName, args, loc) { if (!arg || arg.type !== "Literal" || typeof arg.value !== "string") { throw new Error("No code for testing is given"); } - var isTrue = !!evalWithDefines(arg.value, ctx.defines); + const isTrue = !!evalWithDefines(arg.value, ctx.defines); return { type: "Literal", value: isTrue, loc }; case "eval": arg = args[0]; if (!arg || arg.type !== "Literal" || typeof arg.value !== "string") { throw new Error("No code for eval is given"); } - var result = evalWithDefines(arg.value, ctx.defines); + const result = evalWithDefines(arg.value, ctx.defines); if ( typeof result === "boolean" || typeof result === "string" || @@ -62,14 +62,14 @@ function handlePreprocessorAction(ctx, actionName, args, loc) { if (!arg || arg.type !== "Literal" || typeof arg.value !== "string") { throw new Error("Path to JSON is not provided"); } - var jsonPath = arg.value; + let jsonPath = arg.value; if (jsonPath.indexOf(ROOT_PREFIX) === 0) { jsonPath = path.join( ctx.rootPath, jsonPath.substring(ROOT_PREFIX.length) ); } - var jsonContent = fs.readFileSync(jsonPath).toString(); + const jsonContent = fs.readFileSync(jsonPath).toString(); const parsedJSON = acorn.parse("(" + jsonContent + ")", { ecmaVersion: ACORN_ECMA_VERSION, }); @@ -175,7 +175,7 @@ function postprocessNode(ctx, node) { case "string": case "boolean": case "number": - var equal = node.left.value === node.right.value; + const equal = node.left.value === node.right.value; return { type: "Literal", value: (node.operator[0] === "=") === equal, @@ -210,7 +210,7 @@ function postprocessNode(ctx, node) { } break; case "BlockStatement": - var subExpressionIndex = 0; + let subExpressionIndex = 0; while (subExpressionIndex < node.body.length) { switch (node.body[subExpressionIndex].type) { case "EmptyStatement": @@ -219,7 +219,7 @@ function postprocessNode(ctx, node) { continue; case "BlockStatement": // Block statements inside a block are moved to the parent one. - var subChildren = node.body[subExpressionIndex].body; + const subChildren = node.body[subExpressionIndex].body; Array.prototype.splice.apply( node.body, [subExpressionIndex, 1].concat(subChildren) @@ -240,7 +240,7 @@ function postprocessNode(ctx, node) { break; case "FunctionDeclaration": case "FunctionExpression": - var block = node.body; + const block = node.body; if ( block.body.length > 0 && block.body[block.body.length - 1].type === "ReturnStatement" && @@ -292,7 +292,7 @@ function fixComments(ctx, node) { function traverseTree(ctx, node) { // generic node processing for (const i in node) { - var child = node[i]; + const child = node[i]; if (typeof child === "object" && child !== null && child.type) { const result = traverseTree(ctx, child); if (result !== child) {