From 75cb4409dda8afb8c07217e7b64f0c1c343ad4df Mon Sep 17 00:00:00 2001 From: goosy Date: Sat, 16 Nov 2024 16:45:31 +0800 Subject: [PATCH] =?UTF-8?q?fix=20code=20for=20Biome=E2=80=99s=20linter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/example.entry.js | 12 ++-- src/converter.js | 120 ++++++++++++++++++++------------------- src/gooparse.js | 90 ++++++++++++++--------------- 3 files changed, 113 insertions(+), 109 deletions(-) diff --git a/example/example.entry.js b/example/example.entry.js index 5cd2c4b..e6e28e1 100644 --- a/example/example.entry.js +++ b/example/example.entry.js @@ -1,12 +1,12 @@ -import {convertRules} from "../src/index.js"; +import { convertRules } from "../src/index.js"; const rules = [{ "name": "station_GD8", "tags": { "station_name": "GD8", "stationID": 8078, "peoples": [ - {"name": '张三', "age": 19, }, - {"name": '李四', "age": 32, }, + { "name": '张三', "age": 19 }, + { "name": '李四', "age": 32 }, ] }, }, { @@ -15,8 +15,8 @@ const rules = [{ "station_name": "GD9", "stationID": 8079, "peoples": [ - {"name": '王五', "age": 39, }, - {"name": '赵六', "age": 29, }, + { "name": '王五', "age": 39 }, + { "name": '赵六', "age": 29 }, ] }, }]; @@ -29,7 +29,7 @@ people: {{endfor}}_ `; -for (const {name, content} of convertRules(rules, template)){ +for (const { name, content } of convertRules(rules, template)) { console.log(`${name}\n=======`); console.log(content); } diff --git a/src/converter.js b/src/converter.js index e5170bb..cb77f93 100644 --- a/src/converter.js +++ b/src/converter.js @@ -40,15 +40,17 @@ function parseMemberExpression(tags, es_expression) { } const global_tags = { - range(start, end, step) { + range(...argus) { + let [start, end, step] = argus; if (start === undefined) return []; if (end === undefined) { end = start start = 0 } - let direct = start < end; - step = step === undefined ? (direct ? 1 : -1) : step; - let ret = [], index = start; + const direct = start < end; + step ??= direct ? 1 : -1; + const ret = []; + let index = start; while (direct ? index < end : index > end) { ret.push(index); index += step; @@ -66,40 +68,40 @@ const global_tags = { */ function computeESExpression(tags, es_expression) { // 'Identifier' 'AssignmentExpression' 'BinaryExpression' 'Literal' - if (es_expression.type == "Literal") return es_expression.value; - if (es_expression.type == "Identifier") { + if (es_expression.type === "Literal") return es_expression.value; + if (es_expression.type === "Identifier") { return { ...global_tags, ...tags }[es_expression.name]; // The identifier must be in tags, otherwise undefined is returned } // obj.foo - if (es_expression.type == "MemberExpression") { + if (es_expression.type === "MemberExpression") { const { obj, property } = parseMemberExpression(tags, es_expression); return obj[property]; } // obj?.foo - if (es_expression.type == "ChainExpression") { + if (es_expression.type === "ChainExpression") { const { obj, property } = parseMemberExpression(tags, es_expression.expression); if (obj === null || obj === undefined) return undefined; return obj[property]; } // +expr -expr ~expr !expr - if (es_expression.type == 'UnaryExpression') { - let result = computeESExpression(tags, es_expression.argument); - if (es_expression.operator == '+') return +result; - if (es_expression.operator == '-') return -result; - if (es_expression.operator == '~') return ~result; - if (es_expression.operator == '!') return !result; + if (es_expression.type === 'UnaryExpression') { + const result = computeESExpression(tags, es_expression.argument); + if (es_expression.operator === '+') return +result; + if (es_expression.operator === '-') return -result; + if (es_expression.operator === '~') return ~result; + if (es_expression.operator === '!') return !result; } // expr1 operator expr2 if ( - es_expression.type == 'BinaryExpression' || - es_expression.type == 'LogicalExpression' + es_expression.type === 'BinaryExpression' || + es_expression.type === 'LogicalExpression' ) { - let left = computeESExpression(tags, es_expression.left); - let right = computeESExpression(tags, es_expression.right); + const left = computeESExpression(tags, es_expression.left); + const right = computeESExpression(tags, es_expression.right); switch (es_expression.operator) { case '+': return left + right; @@ -112,10 +114,12 @@ function computeESExpression(tags, es_expression) { case '%': return left % right; case '==': + // biome-ignore lint/suspicious/noDoubleEquals: This is the interpreter return left == right; case '===': return left === right; case '!=': + // biome-ignore lint/suspicious/noDoubleEquals: This is the interpreter return left != right; case '!==': return left !== right; @@ -139,7 +143,7 @@ function computeESExpression(tags, es_expression) { } // expr1, expr2, ..., exprN - if (es_expression.type == 'SequenceExpression') { + if (es_expression.type === 'SequenceExpression') { return es_expression.expressions.reduce( (str, exp) => str + computeESExpression(tags, exp), "" @@ -147,7 +151,7 @@ function computeESExpression(tags, es_expression) { } // expr1 ? expr2 : expr3 - if (es_expression.type == 'ConditionalExpression') { + if (es_expression.type === 'ConditionalExpression') { const test = computeESExpression(tags, es_expression.test); const consequent = computeESExpression(tags, es_expression.consequent); const alternate = computeESExpression(tags, es_expression.alternate); @@ -163,10 +167,10 @@ function computeESExpression(tags, es_expression) { // expr1 %= expr2 // expr1 ??= expr2 if ( - es_expression.type == 'AssignmentExpression' + es_expression.type === 'AssignmentExpression' ) { - let left = es_expression.left.name; - let right = computeESExpression(tags, es_expression.right); + const left = es_expression.left.name; + const right = computeESExpression(tags, es_expression.right); switch (es_expression.operator) { case '=': tags[left] = right; @@ -196,20 +200,20 @@ function computeESExpression(tags, es_expression) { } // foo() obj.foo() obj["foo"]() - if (es_expression.type == "CallExpression") { + if (es_expression.type === "CallExpression") { const argus = []; - es_expression.arguments.forEach(argu => { - if (argu.type == "SpreadElement") { + for (const argu of es_expression.arguments) { + if (argu.type === "SpreadElement") { argus.push(...computeESExpression(tags, argu.argument)); } else { argus.push(computeESExpression(tags, argu)); } - }); + } const callee = es_expression.callee; - if (callee.type == "Identifier") { + if (callee.type === "Identifier") { return computeESExpression(tags, callee)(...argus); } - if (callee.type == "MemberExpression") { + if (callee.type === "MemberExpression") { const { obj, property } = parseMemberExpression(tags, callee); return obj[property](...argus); } @@ -217,33 +221,33 @@ function computeESExpression(tags, es_expression) { } // [expr1, expr2, ...] - if (es_expression.type == "ArrayExpression") { + if (es_expression.type === "ArrayExpression") { const ret = []; - es_expression.elements.forEach(el => { - if (el.type == "SpreadElement") { + for (const el of es_expression.elements) { + if (el.type === "SpreadElement") { ret.push(...computeESExpression(tags, el.argument)); } else { ret.push(computeESExpression(tags, el)); } - }); + } return ret; } // {a: expr1, expr2, ...expr3} - if (es_expression.type == "ObjectExpression") { + if (es_expression.type === "ObjectExpression") { const ret = {}; - es_expression.properties.forEach(prop => { - if (prop.type == "SpreadElement") { + for (const prop of es_expression.properties) { + if (prop.type === "SpreadElement") { Object.assign(ret, computeESExpression(tags, prop.argument)); } - if (prop.type == "Property") { - let key = prop.computed + if (prop.type === "Property") { + const key = prop.computed ? computeESExpression(tags, prop.key) : prop.key.name; const value = computeESExpression(tags, prop.value); ret[key] = value; } - }); + } return ret; } @@ -252,16 +256,16 @@ function computeESExpression(tags, es_expression) { } function convert_FOR_Goonode(tags, node) { - let key, - value, - list, - content = '', - left = node.expression.left, - right = computeESExpression(tags, node.expression.right); + let key; + let value; + let list; + let content = ''; + const left = node.expression.left; + const right = computeESExpression(tags, node.expression.right); if (!right) throw Error("wrong for statement!"); - let isArray = Array.isArray(right); + const isArray = Array.isArray(right); // {{for v in object}} - if (left.type == 'Identifier') { + if (left.type === 'Identifier') { value = left.name; list = Object.values(right); for (const item of list) { @@ -273,12 +277,12 @@ function convert_FOR_Goonode(tags, node) { return content; } // {{for k, v in object}} - if (left.type == "ArrayExpression") { + if (left.type === "ArrayExpression") { key = left[0].name; value = left[1].name; list = Object.entries(right); for (let [k, v] of list) { - k = isArray ? parseInt(k) : k; + k = isArray ? Number.parseInt(k) : k; content += convert_dom({ ...tags, [key]: k, @@ -291,11 +295,11 @@ function convert_FOR_Goonode(tags, node) { } function convert_IF_Goonode(tags, node) { - let truenode = node.contents.find(node => { - if (node.type == "if" || node.type == "elseif") { // After node.text conversion evaluation, decide whether to render the if body + const truenode = node.contents.find(node => { + if (node.type === "if" || node.type === "elseif") { // After node.text conversion evaluation, decide whether to render the if body return computeESExpression(tags, node.expression); } - if (node.type == "else") return true; + if (node.type === "else") return true; return false; }); if (truenode) { @@ -312,16 +316,16 @@ function convert_IF_Goonode(tags, node) { */ function convert_dom(tags, dom) { let content = ''; - dom.contents.forEach(node => { - if (node.type == "raw") { + for (const node of dom.contents) { + if (node.type === "raw") { content += node.text; - } else if (node.type == "expression") { + } else if (node.type === "expression") { content += computeESExpression(tags, node.expression); - } else if (node.type == "ifs") { + } else if (node.type === "ifs") { content += convert_IF_Goonode(tags, node); - } else if (node.type == "for") { + } else if (node.type === "for") { content += convert_FOR_Goonode(tags, node); } - }) + } return content; } \ No newline at end of file diff --git a/src/gooparse.js b/src/gooparse.js index f4c42fc..83c6e44 100644 --- a/src/gooparse.js +++ b/src/gooparse.js @@ -19,67 +19,67 @@ function isLimitedExpression(es_expression) { ['Identifier', 'Literal'].includes(type) ) return true; if ( - type == 'AssignmentExpression' && + type === 'AssignmentExpression' && ['=', '+=', '-=', '*=', '**=', '/=', '%=', '??='].includes(operator) && - es_expression.left.type == "Identifier" && + es_expression.left.type === "Identifier" && isLimitedExpression(es_expression.right) ) return true; if ( - type == 'UnaryExpression' && + type === 'UnaryExpression' && ['+', '-', '~', '!'].includes(operator) && isLimitedExpression(es_expression.argument) ) return true; if ( - type == 'BinaryExpression' && + type === 'BinaryExpression' && ['in', '+', '-', '*', '/', '%', '==', '===', '!=', '!==', '<', '>', '<=', '>='].includes(operator) && isLimitedExpression(es_expression.left) && isLimitedExpression(es_expression.right) ) return true; if ( - type == "ChainExpression" && + type === "ChainExpression" && isLimitedExpression(es_expression.expression) ) return true; if ( - type == "MemberExpression" && + type === "MemberExpression" && isLimitedExpression(es_expression.object) && isLimitedExpression(es_expression.property) ) return true; if ( - type == 'SequenceExpression' && + type === 'SequenceExpression' && !es_expression.expressions.find(e => !isLimitedExpression(e)) ) return true; if ( - type == 'LogicalExpression' && - (operator == '||' || operator == '&&' || operator == '??') && + type === 'LogicalExpression' && + (operator === '||' || operator === '&&' || operator === '??') && isLimitedExpression(es_expression.left) && isLimitedExpression(es_expression.right) ) return true; - if ( // ternary operator - type == "ConditionalExpression" && + if ( // ternary operator ? : + type === "ConditionalExpression" && isLimitedExpression(es_expression.test) && isLimitedExpression(es_expression.consequent) && isLimitedExpression(es_expression.alternate) ) return true; function is_spread_element(expr) { // spread operator - return expr.type == "SpreadElement" && isLimitedExpression(expr.argument); + return expr.type === "SpreadElement" && isLimitedExpression(expr.argument); } - if ( // call operator - type == "CallExpression" && + if ( // call 运算符 + type === "CallExpression" && ['Identifier', 'MemberExpression'].includes(es_expression.callee.type) && isLimitedExpression(es_expression.callee) && es_expression.arguments.every(argu => is_spread_element(argu) || isLimitedExpression(argu)) ) return true; if ( // [expr1, expr2, ...] - type == "ArrayExpression" && + type === "ArrayExpression" && es_expression.elements.every(element => { return is_spread_element(element) || isLimitedExpression(element); }) ) return true; if ( // {a: expr1, expr2, ...expr3} - type == "ObjectExpression" && + type === "ObjectExpression" && es_expression.properties.every(prop => { if (is_spread_element(prop)) return true; - if (prop.type == "Property") { + if (prop.type === "Property") { return isLimitedExpression(prop.key) && isLimitedExpression(prop.value); } return false; @@ -97,12 +97,12 @@ function isLimitedExpression(es_expression) { */ function parse_es_expression(code) { const error = SyntaxError("expression syntax error"); - if (typeof code != 'string') throw error; + if (typeof code !== 'string') throw error; const ast = parse(code, { ecmaVersion: 2023 }); - if (ast.type != "Program") throw error; + if (ast.type !== "Program") throw error; if (ast.body.length === 0) return null; - if (ast.body.length != 1) throw error; - if (ast.body[0].type != 'ExpressionStatement') throw error; + if (ast.body.length !== 1) throw error; + if (ast.body[0].type !== 'ExpressionStatement') throw error; const es_expression = ast.body[0].expression; if (!isLimitedExpression(es_expression)) throw error; return es_expression; @@ -115,14 +115,14 @@ function parse_es_expression(code) { function parse_esforloop_expression(code) { const error = SyntaxError("for expression syntax error"); let expression = parse_es_expression(code) - if (expression.type == 'BinaryExpression') return expression; + if (expression.type === 'BinaryExpression') return expression; const expressions = expression.expressions; if (!expressions) throw error; - if (expressions.length != 2) throw error; - if (expressions[0].type != 'Identifier') throw error; - if (expressions[1].type != 'BinaryExpression') throw error; - if (expressions[1].left.type != 'Identifier') throw error; - if (expressions[1].operator != 'in') throw error; + if (expressions.length !== 2) throw error; + if (expressions[0].type !== 'Identifier') throw error; + if (expressions[1].type !== 'BinaryExpression') throw error; + if (expressions[1].left.type !== 'Identifier') throw error; + if (expressions[1].operator !== 'in') throw error; expression = expressions[1]; expression.left = [expressions[0], expression.left]; @@ -152,7 +152,7 @@ function GTCode2Goonode(GTCode) { if (gtcode.startsWith('if')) { const error = SyntaxError("if expression syntax error!"); const text = gtcode.replace(/^if\s+/, ''); - if (text == gtcode) throw error; + if (text === gtcode) throw error; const expression = parse_es_expression(text); if (!expression) throw error; return { @@ -171,8 +171,8 @@ function GTCode2Goonode(GTCode) { // meet {{elseif expression }} if (gtcode.startsWith('elseif')) { const error = SyntaxError("elseif expression syntax error!"); - let text = gtcode.replace(/^elseif\s+/, ''); - if (text == gtcode) throw error; + const text = gtcode.replace(/^elseif\s+/, ''); + if (text === gtcode) throw error; const expression = parse_es_expression(text); if (!expression) throw error; return { @@ -208,7 +208,7 @@ function GTCode2Goonode(GTCode) { if (gtcode.startsWith('for')) { const error = SyntaxError("for expression syntax error!"); const text = gtcode.replace(/^for\s+/, ''); - if (text == gtcode) throw error; + if (text === gtcode) throw error; const expression = parse_esforloop_expression(text); return { type: "for", @@ -250,7 +250,7 @@ function stack_push(code) { current_node.contents.push(code); code.parents = current_node; current_node = code; - if (code.type == "ifs") { + if (code.type === "ifs") { current_node.contents[0].parents = current_node; current_node = current_node.contents[0]; } @@ -267,19 +267,19 @@ function stack_pop() { function GT_tree_append(code) { // gen ifs[] if[] - if (code.type == "ifs") { - // current node ►node[...]◄ + if (code.type === "ifs") { + // current node ►node[...]◄ stack_push(code); // current node node[... ifs[►if[]◄] ] return; } // gen elseif[] - if (code.type == "elseif" || code.type == "else") { + if (code.type === "elseif" || code.type === "else") { // current node ifs[...►(if|elseif)[...]◄ ] // Cannot match the previous {{if}} or {{elseif}}, an error will be reported const type = current_node.type; - if (type != "if" && type != "elseif") throw SyntaxError("wrong pair of IF!"); + if (type !== "if" && type !== "elseif") throw SyntaxError("wrong pair of IF!"); // pop stack_pop(); // current node ►ifs[...(if|elseif)[...]]◄ @@ -291,16 +291,16 @@ function GT_tree_append(code) { } // endif - if (code.type == "endif") { + if (code.type === "endif") { // current node node[...ifs[...►(if|elseif|else)[...]◄]] // Cannot match the previous {{if}} or {{elseif}} or {{else}}, an error will be reported const type = current_node.type; - if (type != "if" && type != "elseif" && type != "else") throw SyntaxError("wrong pair of IF!"); + if (type !== "if" && type !== "elseif" && type !== "else") throw SyntaxError("wrong pair of IF!"); // Pop off the stack. If not in the ifs queue, an error will be reported. stack_pop(); // current node node[...►ifs[...if|elseif|else[...]]◄] - if (current_node.type != "ifs") throw SyntaxError("wrong pair of IF!"); + if (current_node.type !== "ifs") throw SyntaxError("wrong pair of IF!"); // Pop again stack_pop(); // current node ►node[...ifs[...if|elseif|else[...]]]◄ @@ -308,29 +308,29 @@ function GT_tree_append(code) { } // for[] - if (code.type == "for") { + if (code.type === "for") { // ►node[...]◄ stack_push(code); // current node node[...►for[]◄] return; } // endfor - if (code.type == "endfor") { + if (code.type === "endfor") { // current node node[...►for[...]◄] - if (current_node.type != "for") throw SyntaxError("wrong pair of FOR!"); + if (current_node.type !== "for") throw SyntaxError("wrong pair of FOR!"); stack_pop(); // current node ►node[...for[...]]◄ return; } // expression|raw[] - if (code.type == "expression" || code.type == "raw") { + if (code.type === "expression" || code.type === "raw") { // current node ►node[...]◄ current_node.contents.push(code); // 仅附加内容,当前节点不变 ►node[...(expression|raw)[] ]◄ return; } // // - if (code.type == "comment") { + if (code.type === "comment") { return; } throw SyntaxError("wrong node"); @@ -410,7 +410,7 @@ export function parseToDOM(template) { contents: [] }); - if (current_node != document) { + if (current_node !== document) { throw SyntaxError(`${current_node.type} tag mismatch`); }