diff --git a/README.md b/README.md index 7f3bb8d..1a574af 100644 --- a/README.md +++ b/README.md @@ -456,13 +456,13 @@ options: * xml-deprecated-qweb-directive - https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.2.20/test_repo/test_module/website_templates.xml#L7 Deprecated QWeb directive `t-esc-options`. Use `t-options` instead - - https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.2.20/test_repo/test_module/website_templates.xml#L37 Deprecated QWeb directive `t-field-options`. Use `t-options` instead + - https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.2.20/test_repo/test_module/website_templates.xml#L42 Deprecated QWeb directive `t-field-options`. Use `t-options` instead * xml-deprecated-qweb-directive-15 - https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.2.20/test_repo/odoo18_module/views/deprecated_qweb_directives15.xml#L6 Deprecated QWeb directive `t-esc`. Use `t-out` instead - https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.2.20/test_repo/odoo18_module/views/deprecated_qweb_directives15.xml#L7 Deprecated QWeb directive `t-raw`. Use `t-out` instead - - https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.2.20/test_repo/odoo18_module/views/deprecated_qweb_directives15.xml#L13 Deprecated QWeb directive `t-esc`. Use `t-out` instead + - https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.2.20/test_repo/odoo18_module/views/deprecated_qweb_directives15.xml#L15 Deprecated QWeb directive `t-esc`. Use `t-out` instead * xml-deprecated-tree-attribute @@ -523,14 +523,14 @@ options: * xml-not-valid-char-link - - https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.2.20/test_repo/test_module/website_templates.xml#L59 The resource in in src/href contains a not valid character - - https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.2.20/test_repo/test_module/website_templates.xml#L61 The resource in in src/href contains a not valid character + - https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.2.20/test_repo/test_module/website_templates.xml#L64 The resource in in src/href contains a not valid character + - https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.2.20/test_repo/test_module/website_templates.xml#L66 The resource in in src/href contains a not valid character * xml-oe-structure-missing-id - https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.2.20/test_repo/test_module/website_templates.xml#L9 Consider removing the class `oe_structure` or adding a proper id to the tag. The id must contain `oe_structure` - https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.2.20/test_repo/test_module/website_templates.xml#L13 Consider removing the class `oe_structure` or adding a proper id to the tag. The id must contain `oe_structure` - - https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.2.20/test_repo/test_module/website_templates.xml#L41 Consider removing the class `oe_structure` or adding a proper id to the tag. The id must contain `oe_structure` + - https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.2.20/test_repo/test_module/website_templates.xml#L46 Consider removing the class `oe_structure` or adding a proper id to the tag. The id must contain `oe_structure` * xml-record-missing-id diff --git a/src/oca_pre_commit_hooks/node_xml.py b/src/oca_pre_commit_hooks/node_xml.py index a1726cf..15bdb34 100644 --- a/src/oca_pre_commit_hooks/node_xml.py +++ b/src/oca_pre_commit_hooks/node_xml.py @@ -29,7 +29,7 @@ def _read_node(self): # noqa:C901 pylint:disable=too-complex if (node_previous := self.node.getprevious()) is not None: search_start_line = node_previous.sourceline + 1 elif (node_parent := self.node.getparent()) is not None: - search_start_line = node_parent.sourceline + 1 + search_start_line = node_parent.sourceline else: search_start_line = 2 # first element and it is the root @@ -40,12 +40,14 @@ def _read_node(self): # noqa:C901 pylint:disable=too-complex with open(self.filename, "rb") as f_content: all_lines = list((i, line) for i, line in enumerate(f_content, start=1)) + search_start_line = min(search_start_line, search_end_line) + # Find the actual node start by looking for the tag node_start_idx = None for idx, (no_line, line) in enumerate(all_lines): if search_start_line <= no_line <= search_end_line: stripped_line = line.lstrip() - if stripped_line.startswith(b"<" + node_tag): + if b"<" + node_tag in stripped_line: node_start_idx = idx self.start_sourceline = no_line break @@ -82,7 +84,7 @@ def _read_node(self): # noqa:C901 pylint:disable=too-complex node_end_idx = idx self.end_sourceline = no_line break - if b"/>" in stripped_line and not stripped_line.startswith(b"<"): + if b"/>" in stripped_line and b"<" not in stripped_line: # Self-closing continuation node_end_idx = idx self.end_sourceline = no_line diff --git a/test_repo/odoo18_module/views/deprecated_qweb_directives15.xml b/test_repo/odoo18_module/views/deprecated_qweb_directives15.xml index 8e1e855..d989026 100644 --- a/test_repo/odoo18_module/views/deprecated_qweb_directives15.xml +++ b/test_repo/odoo18_module/views/deprecated_qweb_directives15.xml @@ -1,6 +1,6 @@ - + diff --git a/tests/test_checks.py b/tests/test_checks.py index 65a413a..cca714c 100644 --- a/tests/test_checks.py +++ b/tests/test_checks.py @@ -35,10 +35,10 @@ "xml-dangerous-qweb-replace-low-priority": 9, "xml-deprecated-data-node": 8, "xml-deprecated-openerp-node": 4, - "xml-deprecated-qweb-directive-15": 3, + "xml-deprecated-qweb-directive-15": 4, "xml-deprecated-qweb-directive": 2, "xml-deprecated-tree-attribute": 3, - "xml-double-quotes-py": 3, + "xml-double-quotes-py": 4, "xml-duplicate-fields": 3, "xml-duplicate-record-id": 2, "xml-not-valid-char-link": 2, @@ -258,7 +258,8 @@ def test_autofix(self): with open(t_out, "rb") as f: content = f.read() - assert b"t-out" not in content, "The deprecated t-out was previously fixed" + assert content.count(b"t-esc") > 1, "The deprecated t-esc was previously fixed" + assert content.count(b"t-raw") > 1, "The deprecated t-raw was previously fixed" self.checks_run(self.file_paths, autofix=True, no_exit=True, no_verbose=False) @@ -343,4 +344,6 @@ def test_autofix(self): with open(t_out, "rb") as f: content = f.read() - assert b"t-out" in content, "The deprecated t-out was not fixed" + # comments contain 1 valid deprecated + assert content.count(b"t-esc") == 1, "The deprecated t-esc was not fixed" + assert content.count(b"t-raw") == 1, "The deprecated t-esc was not fixed"