From 35c904c16d2a74738231254d865655bb6e591dcc Mon Sep 17 00:00:00 2001 From: Aire-One Date: Sun, 25 Feb 2024 19:31:51 +0100 Subject: [PATCH] feat(visitors): implement remove_duplicate_fields --- src/awesomewmdtl/init.tl | 4 +++ .../visitors/remove_duplicate_fields.tl | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/awesomewmdtl/visitors/remove_duplicate_fields.tl diff --git a/src/awesomewmdtl/init.tl b/src/awesomewmdtl/init.tl index 0771000..726faaa 100644 --- a/src/awesomewmdtl/init.tl +++ b/src/awesomewmdtl/init.tl @@ -15,6 +15,7 @@ local module_descendants = require("awesomewmdtl.visitors.module_descend local type Node = require("awesomewmdtl.types.Node") local node_fixer = require("awesomewmdtl.visitors.node_fixer") local property = require("awesomewmdtl.property") +local remove_duplicate_fields = require("awesomewmdtl.visitors.remove_duplicate_fields") local scraper = require("awesomewmdtl.scraper") local type_mapping = require("awesomewmdtl.visitors.type_mapping") local utils = require("awesomewmdtl.utils") @@ -111,6 +112,9 @@ for _,root in dag.iter_global_nodes(module_dag) do end for root in dag.iter_modules(module_dag) do + ast.in_order_visitor(root, function(node: Node) + remove_duplicate_fields.visit(node) + end) ast.in_order_visitor(root, function(node: Node) node_fixer.visit(node, root) end) diff --git a/src/awesomewmdtl/visitors/remove_duplicate_fields.tl b/src/awesomewmdtl/visitors/remove_duplicate_fields.tl new file mode 100644 index 0000000..9300052 --- /dev/null +++ b/src/awesomewmdtl/visitors/remove_duplicate_fields.tl @@ -0,0 +1,31 @@ +local type Node = require("awesomewmdtl.types.Node") +local utils = require("awesomewmdtl.utils") + +local has_item = utils.has_item + +-- This is quick hack to remove duplicate fields from a module +-- We can have duplicated fields in a module, because we currently +-- don't make difference between module record and instance record. +-- So static module fields and instance level fields are mixed together. + +local record Remove_Duplicate_Fields + visit: function(node: Node) +end + +function Remove_Duplicate_Fields.visit(node: Node) + if node.token ~= "module" then + return + end + + local fields: { string } = {} + + for i, n in ipairs(node.children) do + if has_item(fields, n.name) then + table.remove(node.children, i) + else + table.insert(fields, n.name) + end + end +end + +return Remove_Duplicate_Fields