odooscan 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. {odooscan-0.2.0 → odooscan-0.3.0}/Cargo.lock +2 -2
  2. {odooscan-0.2.0 → odooscan-0.3.0}/Cargo.toml +1 -1
  3. {odooscan-0.2.0 → odooscan-0.3.0}/PKG-INFO +15 -13
  4. {odooscan-0.2.0 → odooscan-0.3.0}/README.md +14 -12
  5. {odooscan-0.2.0 → odooscan-0.3.0}/crates/odooscan_core/src/checks.rs +81 -7
  6. {odooscan-0.2.0 → odooscan-0.3.0}/crates/odooscan_core/src/facts.rs +20 -1
  7. {odooscan-0.2.0 → odooscan-0.3.0}/crates/odooscan_core/src/index.rs +100 -12
  8. {odooscan-0.2.0 → odooscan-0.3.0}/crates/odooscan_core/src/python.rs +44 -1
  9. {odooscan-0.2.0 → odooscan-0.3.0}/crates/odooscan_core/src/rules.rs +1 -0
  10. {odooscan-0.2.0 → odooscan-0.3.0}/crates/odooscan_core/src/xml.rs +92 -5
  11. {odooscan-0.2.0 → odooscan-0.3.0}/LICENSE +0 -0
  12. {odooscan-0.2.0 → odooscan-0.3.0}/crates/odooscan/Cargo.toml +0 -0
  13. {odooscan-0.2.0 → odooscan-0.3.0}/crates/odooscan/src/baseline.rs +0 -0
  14. {odooscan-0.2.0 → odooscan-0.3.0}/crates/odooscan/src/config.rs +0 -0
  15. {odooscan-0.2.0 → odooscan-0.3.0}/crates/odooscan/src/main.rs +0 -0
  16. {odooscan-0.2.0 → odooscan-0.3.0}/crates/odooscan/src/output.rs +0 -0
  17. {odooscan-0.2.0 → odooscan-0.3.0}/crates/odooscan_core/Cargo.toml +0 -0
  18. {odooscan-0.2.0 → odooscan-0.3.0}/crates/odooscan_core/src/csvdata.rs +0 -0
  19. {odooscan-0.2.0 → odooscan-0.3.0}/crates/odooscan_core/src/lib.rs +0 -0
  20. {odooscan-0.2.0 → odooscan-0.3.0}/crates/odooscan_core/src/manifest.rs +0 -0
  21. {odooscan-0.2.0 → odooscan-0.3.0}/crates/odooscan_core/src/project.rs +0 -0
  22. {odooscan-0.2.0 → odooscan-0.3.0}/crates/odooscan_core/src/series.rs +0 -0
  23. {odooscan-0.2.0 → odooscan-0.3.0}/crates/odooscan_core/src/suppress.rs +0 -0
  24. {odooscan-0.2.0 → odooscan-0.3.0}/pyproject.toml +0 -0
@@ -508,7 +508,7 @@ checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
508
508
 
509
509
  [[package]]
510
510
  name = "odooscan"
511
- version = "0.2.0"
511
+ version = "0.3.0"
512
512
  dependencies = [
513
513
  "clap",
514
514
  "ignore",
@@ -520,7 +520,7 @@ dependencies = [
520
520
 
521
521
  [[package]]
522
522
  name = "odooscan_core"
523
- version = "0.2.0"
523
+ version = "0.3.0"
524
524
  dependencies = [
525
525
  "csv",
526
526
  "ignore",
@@ -3,7 +3,7 @@ resolver = "3"
3
3
  members = ["crates/*"]
4
4
 
5
5
  [workspace.package]
6
- version = "0.2.0"
6
+ version = "0.3.0"
7
7
  edition = "2024"
8
8
  license = "MIT"
9
9
  # Released as binaries on GitHub, not on crates.io.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: odooscan
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Classifier: Development Status :: 3 - Alpha
5
5
  Classifier: Environment :: Console
6
6
  Classifier: Framework :: Odoo
@@ -77,7 +77,8 @@ Excluded files are still indexed: they only go unreported.
77
77
  ## In CI
78
78
 
79
79
  The scan needs the Odoo sources your modules build on. A shallow sparse clone into a hidden
80
- directory is enough, and module discovery skips hidden directories. On GitHub, `--format github`
80
+ directory is enough, and module discovery skips hidden directories. Leaving out translations,
81
+ static assets and tests makes it about 20 MB instead of 190 MB, with the same index. On GitHub, `--format github`
81
82
  annotates the pull request, with no need for the Advanced Security that SARIF upload requires on a
82
83
  private repository:
83
84
 
@@ -89,9 +90,14 @@ private repository:
89
90
  ref: "18.0"
90
91
  path: .odoo
91
92
  fetch-depth: 1
93
+ filter: blob:none
94
+ sparse-checkout-cone-mode: false
92
95
  sparse-checkout: |
93
- addons
94
- odoo/addons
96
+ /addons/
97
+ /odoo/addons/
98
+ !**/i18n/
99
+ !**/static/
100
+ !**/tests/
95
101
  persist-credentials: false
96
102
  - uses: astral-sh/setup-uv@v10
97
103
  - run: uvx --from odooscan oscn check --addons-path .odoo --format github
@@ -103,8 +109,9 @@ On GitLab, `--format gitlab` writes a Code Quality report for the merge request:
103
109
  odooscan:
104
110
  image: python:3.13
105
111
  script:
106
- - git clone --depth 1 --branch 18.0 --filter=blob:none --sparse https://github.com/odoo/odoo .odoo
107
- - git -C .odoo sparse-checkout set addons odoo/addons
112
+ - git clone --depth 1 --branch 18.0 --filter=blob:none --no-checkout https://github.com/odoo/odoo .odoo
113
+ - git -C .odoo sparse-checkout set --no-cone '/addons/' '/odoo/addons/' '!**/i18n/' '!**/static/' '!**/tests/'
114
+ - git -C .odoo checkout 18.0
108
115
  - pip install odooscan
109
116
  - oscn check --addons-path .odoo --format gitlab > gl-code-quality-report.json
110
117
  artifacts:
@@ -153,11 +160,6 @@ every rule. XML takes the same text inside `<!-- -->`.
153
160
 
154
161
  `cargo test` runs a snapshot over `crates/odooscan_core/tests/fixtures`. That fixture is a small
155
162
  core (`base`, `mail`) plus custom modules written to trigger every rule; review changes with
156
- `cargo insta review`. The false-positive oracle is Odoo itself. A scan of Odoo core must stay
157
- near-silent for the index-aware rules, so run one after any change to them:
158
-
159
- ```sh
160
- git -C ~/repos/odoo/odoo archive origin/18.0 -- addons odoo/addons ':(exclude,glob)**/static/**' | tar -x -C /tmp/odoo-18.0
161
- oscn check /tmp/odoo-18.0 --odoo-version 18.0 --statistics
162
- ```
163
+ `cargo insta review`. The false-positive oracle is Odoo itself: a scan of core must report exactly
164
+ the findings recorded in `tests/oracle/`, as CONTRIBUTING.md describes.
163
165
 
@@ -58,7 +58,8 @@ Excluded files are still indexed: they only go unreported.
58
58
  ## In CI
59
59
 
60
60
  The scan needs the Odoo sources your modules build on. A shallow sparse clone into a hidden
61
- directory is enough, and module discovery skips hidden directories. On GitHub, `--format github`
61
+ directory is enough, and module discovery skips hidden directories. Leaving out translations,
62
+ static assets and tests makes it about 20 MB instead of 190 MB, with the same index. On GitHub, `--format github`
62
63
  annotates the pull request, with no need for the Advanced Security that SARIF upload requires on a
63
64
  private repository:
64
65
 
@@ -70,9 +71,14 @@ private repository:
70
71
  ref: "18.0"
71
72
  path: .odoo
72
73
  fetch-depth: 1
74
+ filter: blob:none
75
+ sparse-checkout-cone-mode: false
73
76
  sparse-checkout: |
74
- addons
75
- odoo/addons
77
+ /addons/
78
+ /odoo/addons/
79
+ !**/i18n/
80
+ !**/static/
81
+ !**/tests/
76
82
  persist-credentials: false
77
83
  - uses: astral-sh/setup-uv@v10
78
84
  - run: uvx --from odooscan oscn check --addons-path .odoo --format github
@@ -84,8 +90,9 @@ On GitLab, `--format gitlab` writes a Code Quality report for the merge request:
84
90
  odooscan:
85
91
  image: python:3.13
86
92
  script:
87
- - git clone --depth 1 --branch 18.0 --filter=blob:none --sparse https://github.com/odoo/odoo .odoo
88
- - git -C .odoo sparse-checkout set addons odoo/addons
93
+ - git clone --depth 1 --branch 18.0 --filter=blob:none --no-checkout https://github.com/odoo/odoo .odoo
94
+ - git -C .odoo sparse-checkout set --no-cone '/addons/' '/odoo/addons/' '!**/i18n/' '!**/static/' '!**/tests/'
95
+ - git -C .odoo checkout 18.0
89
96
  - pip install odooscan
90
97
  - oscn check --addons-path .odoo --format gitlab > gl-code-quality-report.json
91
98
  artifacts:
@@ -134,10 +141,5 @@ every rule. XML takes the same text inside `<!-- -->`.
134
141
 
135
142
  `cargo test` runs a snapshot over `crates/odooscan_core/tests/fixtures`. That fixture is a small
136
143
  core (`base`, `mail`) plus custom modules written to trigger every rule; review changes with
137
- `cargo insta review`. The false-positive oracle is Odoo itself. A scan of Odoo core must stay
138
- near-silent for the index-aware rules, so run one after any change to them:
139
-
140
- ```sh
141
- git -C ~/repos/odoo/odoo archive origin/18.0 -- addons odoo/addons ':(exclude,glob)**/static/**' | tar -x -C /tmp/odoo-18.0
142
- oscn check /tmp/odoo-18.0 --odoo-version 18.0 --statistics
143
- ```
144
+ `cargo insta review`. The false-positive oracle is Odoo itself: a scan of core must report exactly
145
+ the findings recorded in `tests/oracle/`, as CONTRIBUTING.md describes.
@@ -5,10 +5,13 @@ use std::path::Path;
5
5
  use rustc_hash::{FxHashMap, FxHashSet};
6
6
 
7
7
  use crate::facts::{ClassFact, FieldFact, ModelKind, Pos, RawDiag, RecordFact, Spanned, ViewFact, XmlRef};
8
- use crate::index::{Index, Scope, Status, full_id};
8
+ use crate::index::{Index, Scope, Status, ViewTree, full_id, located};
9
9
  use crate::project::Module;
10
10
  use crate::xml::{LOCATOR, UNKNOWN_CONTEXT};
11
11
 
12
+ /// Directories of upgrade scripts, which name XML ids of past versions.
13
+ const UPGRADE_DIRS: &[&str] = &["migrations", "upgrades"];
14
+
12
15
  /// Directories whose XML and CSV files are not meant to be listed in the manifest.
13
16
  const UNLISTED_DIRS: &[&str] = &["tests", "i18n", "migrations", "upgrades", "doc", "docs", "examples"];
14
17
 
@@ -33,6 +36,7 @@ pub fn check_module<'m>(index: &Index<'m>, mi: usize) -> Vec<(&'m Path, RawDiag)
33
36
  index,
34
37
  module,
35
38
  scope: &scope,
39
+ dep_names: module.manifest.depends.iter().map(|d| d.value.as_str()).collect(),
36
40
  };
37
41
  ctx.dependencies(&mut out);
38
42
  ctx.python(&mut out);
@@ -144,6 +148,7 @@ struct Ctx<'a, 'm> {
144
148
  index: &'a Index<'m>,
145
149
  module: &'m Module,
146
150
  scope: &'a Scope,
151
+ dep_names: FxHashSet<&'m str>,
147
152
  }
148
153
 
149
154
  impl<'m> Ctx<'_, 'm> {
@@ -190,6 +195,14 @@ impl<'m> Ctx<'_, 'm> {
190
195
  for class in &file.facts.classes {
191
196
  self.class(out, path, class);
192
197
  }
198
+ if !UPGRADE_DIRS.contains(&file.rel.split('/').next().unwrap_or_default()) {
199
+ for r in &file.facts.xml_refs {
200
+ self.py_ref(out, path, r, true);
201
+ }
202
+ for r in &file.facts.group_refs {
203
+ self.py_ref(out, path, r, false);
204
+ }
205
+ }
193
206
  // env['x'] for a model outside depends is usually guarded by `if 'x' in self.env`, and
194
207
  // a dotless name is as likely a key of some other mapping called env.
195
208
  for model in file.facts.env_models.iter().filter(|m| m.value.contains('.')) {
@@ -280,15 +293,16 @@ impl<'m> Ctx<'_, 'm> {
280
293
  }
281
294
 
282
295
  fn data(&self, out: &mut Out<'m>) {
283
- let dep_names: FxHashSet<&str> = self.module.manifest.depends.iter().map(|d| d.value.as_str()).collect();
296
+ let mut trees: FxHashMap<String, ViewTree<'m>> = FxHashMap::default();
284
297
  // Files the manifest does not load, such as chart template CSVs read by Python code, are skipped.
285
298
  for file in self.module.data.iter().filter(|f| f.rank.is_some()) {
286
299
  let path = file.path.as_path();
287
300
  for r in &file.facts.refs {
288
- self.xml_ref(out, path, r, &dep_names);
301
+ self.xml_ref(out, path, r);
289
302
  }
290
303
  for view in &file.facts.views {
291
304
  self.view(out, path, view);
305
+ self.locators(out, path, view, &mut trees);
292
306
  }
293
307
  for record in &file.facts.records {
294
308
  self.record(out, path, record);
@@ -296,7 +310,7 @@ impl<'m> Ctx<'_, 'm> {
296
310
  }
297
311
  }
298
312
 
299
- fn xml_ref(&self, out: &mut Out<'m>, path: &'m Path, r: &XmlRef, dep_names: &FxHashSet<&str>) {
313
+ fn xml_ref(&self, out: &mut Out<'m>, path: &'m Path, r: &XmlRef) {
300
314
  let index = self.index;
301
315
  let id = full_id(&self.module.name, &r.id);
302
316
  match index.xmlids.get(&id) {
@@ -310,11 +324,11 @@ impl<'m> Ctx<'_, 'm> {
310
324
  format!("'{id}' is defined by module {m}; add it to depends"),
311
325
  );
312
326
  }
313
- None => self.undefined_ref(out, path, r.pos, &id, dep_names),
327
+ None => self.undefined_ref(out, path, r.pos, &id),
314
328
  }
315
329
  }
316
330
 
317
- fn undefined_ref(&self, out: &mut Out<'m>, path: &'m Path, pos: Pos, id: &str, dep_names: &FxHashSet<&str>) {
331
+ fn undefined_ref(&self, out: &mut Out<'m>, path: &'m Path, pos: Pos, id: &str) {
318
332
  let owner = id.split_once('.').map_or("", |(m, _)| m);
319
333
  match self.index.by_name.get(owner) {
320
334
  Some(&mi) if self.scope.sees(mi) => {
@@ -326,7 +340,7 @@ impl<'m> Ctx<'_, 'm> {
326
340
  pos,
327
341
  format!("'{id}' belongs to module {owner}; add it to depends"),
328
342
  ),
329
- None if !dep_names.contains(owner) => {
343
+ None if !self.dep_names.contains(owner) => {
330
344
  out.push(
331
345
  path,
332
346
  "OD302",
@@ -338,6 +352,29 @@ impl<'m> Ctx<'_, 'm> {
338
352
  }
339
353
  }
340
354
 
355
+ /// Python code may name ids of modules outside the index, such as Enterprise ones, behind a
356
+ /// runtime check, so only ids of indexed modules are checked; `hidden` also reports those of
357
+ /// modules missing from depends.
358
+ fn py_ref(&self, out: &mut Out<'m>, path: &'m Path, r: &XmlRef, hidden: bool) {
359
+ let owner = r.id.split_once('.').map_or("", |(m, _)| m);
360
+ let Some(&mi) = self.index.by_name.get(owner) else {
361
+ return;
362
+ };
363
+ if !self.scope.sees(mi) {
364
+ if hidden {
365
+ let message = format!("'{}' belongs to module {owner}; add it to depends", r.id);
366
+ out.push(path, "OD302", r.pos, message);
367
+ }
368
+ } else if !self.index.xmlids.contains_key(&r.id) {
369
+ out.push(
370
+ path,
371
+ "OD301",
372
+ r.pos,
373
+ format!("module {owner} defines no XML id '{}'", r.id),
374
+ );
375
+ }
376
+ }
377
+
341
378
  fn view(&self, out: &mut Out<'m>, path: &'m Path, view: &ViewFact) {
342
379
  let model = match (&view.model, &view.inherit) {
343
380
  (Some(m), _) => {
@@ -362,6 +399,43 @@ impl<'m> Ctx<'_, 'm> {
362
399
  }
363
400
  }
364
401
 
402
+ /// Specs of a view must each find their element in the tree of views it extends, which is
403
+ /// built once per root and module.
404
+ fn locators(
405
+ &self,
406
+ out: &mut Out<'m>,
407
+ path: &'m Path,
408
+ view: &'m ViewFact,
409
+ trees: &mut FxHashMap<String, ViewTree<'m>>,
410
+ ) {
411
+ let Some(parent) = view.inherit.as_deref().filter(|_| !view.locators.is_empty()) else {
412
+ return;
413
+ };
414
+ let parent = full_id(&self.module.name, parent);
415
+ // A parent outside depends is already reported by OD302.
416
+ if !self.index.view_visible(&parent, self.scope) {
417
+ return;
418
+ }
419
+ let Some(root) = self.index.view_root(parent) else {
420
+ return;
421
+ };
422
+ let tree = trees
423
+ .entry(root.clone())
424
+ .or_insert_with(|| self.index.view_tree(root, self.scope));
425
+ for locator in &view.locators {
426
+ let e = located(&locator.value, &self.module.name);
427
+ if tree.visible.contains(&e) {
428
+ continue;
429
+ }
430
+ let element = format!("<{} {}=\"{}\">", e.tag, e.attr, e.value);
431
+ let message = match tree.hidden.get(&e) {
432
+ Some(m) => format!("{element} is added by module {m}, which is not in depends"),
433
+ None => format!("no {element} in view {} or the views that extend it", tree.root),
434
+ };
435
+ out.push(path, "OD310", locator.pos, message);
436
+ }
437
+ }
438
+
365
439
  /// The dotted path of an arch field without its locators, or None when one of them is not on
366
440
  /// `model`: such a locator matched inside a subview we cannot see.
367
441
  fn arch_path(&self, model: &str, segments: &[String]) -> Option<String> {
@@ -90,6 +90,11 @@ pub struct PyFacts {
90
90
  pub classes: Vec<ClassFact>,
91
91
  /// Literal model names in `env['x']`.
92
92
  pub env_models: Vec<Spanned<String>>,
93
+ /// XML ids named by `env.ref()`.
94
+ pub xml_refs: Vec<XmlRef>,
95
+ /// Groups named by `has_group()` and the `groups` of fields, which may come from modules
96
+ /// that are not installed.
97
+ pub group_refs: Vec<XmlRef>,
93
98
  /// Base names of `.xml` and `.csv` files named in string literals, which Python code may load.
94
99
  pub file_mentions: Vec<String>,
95
100
  pub diags: Vec<RawDiag>,
@@ -115,12 +120,26 @@ pub struct ArchField {
115
120
  pub pos: Pos,
116
121
  }
117
122
 
118
- #[derive(Debug)]
123
+ /// An arch element as an inheriting view locates it: its tag and its `name` or `id`.
124
+ #[derive(Debug, Clone, PartialEq, Eq, Hash)]
125
+ pub struct ArchElement {
126
+ pub tag: String,
127
+ pub attr: &'static str,
128
+ pub value: String,
129
+ }
130
+
131
+ #[derive(Debug, Default)]
119
132
  pub struct ViewFact {
120
133
  pub id: Option<String>,
121
134
  pub model: Option<Spanned<String>>,
122
135
  pub inherit: Option<String>,
123
136
  pub fields: Vec<ArchField>,
137
+ /// The view's arch was read, so `elements` is complete.
138
+ pub has_arch: bool,
139
+ /// Elements the arch adds, which views extending it can locate.
140
+ pub elements: Vec<ArchElement>,
141
+ /// Elements the arch's specs (`position` elements and xpaths) must find in the parent views.
142
+ pub locators: Vec<Spanned<ArchElement>>,
124
143
  }
125
144
 
126
145
  #[derive(Debug)]
@@ -1,6 +1,6 @@
1
1
  use rustc_hash::{FxHashMap, FxHashSet};
2
2
 
3
- use crate::facts::{ClassFact, FieldFact};
3
+ use crate::facts::{ArchElement, ClassFact, FieldFact, ViewFact};
4
4
  use crate::project::Module;
5
5
 
6
6
  /// Fields every model has without declaring them.
@@ -49,6 +49,37 @@ type Models<'m> = FxHashMap<&'m str, Vec<(usize, &'m ClassFact)>>;
49
49
 
50
50
  type XmlIds = FxHashMap<String, Vec<XmlIdDef>>;
51
51
 
52
+ /// Every definition of a view id: a module may redefine another module's view.
53
+ type Views<'m> = FxHashMap<String, Vec<ViewNode<'m>>>;
54
+
55
+ struct ViewNode<'m> {
56
+ module: usize,
57
+ inherit: Option<String>,
58
+ fact: &'m ViewFact,
59
+ }
60
+
61
+ /// The elements of the views in one inheritance tree: those of views in a module's scope, and for
62
+ /// the others the first module that adds each.
63
+ #[derive(Default)]
64
+ pub struct ViewTree<'m> {
65
+ pub root: String,
66
+ pub visible: FxHashSet<ArchElement>,
67
+ pub hidden: FxHashMap<ArchElement, &'m str>,
68
+ }
69
+
70
+ /// `%(id)d` names an action by XML id, which Odoo resolves against the module of the view.
71
+ pub fn located(e: &ArchElement, module: &str) -> ArchElement {
72
+ let value = match e.value.strip_prefix("%(").and_then(|v| v.strip_suffix(")d")) {
73
+ Some(id) => format!("%({})d", full_id(module, id)),
74
+ None => e.value.clone(),
75
+ };
76
+ ArchElement {
77
+ tag: e.tag.clone(),
78
+ attr: e.attr,
79
+ value,
80
+ }
81
+ }
82
+
52
83
  /// The ids base generates for a module and for each level of its category.
53
84
  fn module_ids(module: &Module, base: usize, xmlids: &mut XmlIds) {
54
85
  xmlids
@@ -96,7 +127,7 @@ fn data_ids<'m>(
96
127
  module: &'m Module,
97
128
  by_name: &FxHashMap<&str, usize>,
98
129
  xmlids: &mut XmlIds,
99
- views: &mut FxHashMap<String, (Option<&'m str>, Option<String>)>,
130
+ views: &mut Views<'m>,
100
131
  ) {
101
132
  let name = module.name.as_str();
102
133
  for file in &module.data {
@@ -112,11 +143,11 @@ fn data_ids<'m>(
112
143
  }
113
144
  for view in &file.facts.views {
114
145
  if let Some(id) = &view.id {
115
- let inherit = view.inherit.as_deref().map(|i| full_id(name, i));
116
- views.insert(
117
- full_id(name, id),
118
- (view.model.as_ref().map(|m| m.value.as_str()), inherit),
119
- );
146
+ views.entry(full_id(name, id)).or_default().push(ViewNode {
147
+ module: mi,
148
+ inherit: view.inherit.as_deref().map(|i| full_id(name, i)),
149
+ fact: view,
150
+ });
120
151
  }
121
152
  }
122
153
  }
@@ -211,7 +242,9 @@ pub struct Index<'m> {
211
242
  pub by_name: FxHashMap<&'m str, usize>,
212
243
  models: Models<'m>,
213
244
  pub xmlids: FxHashMap<String, Vec<XmlIdDef>>,
214
- views: FxHashMap<String, (Option<&'m str>, Option<String>)>,
245
+ views: Views<'m>,
246
+ /// View id to the ids of the views that inherit from it.
247
+ children: FxHashMap<String, Vec<String>>,
215
248
  pub models_with_access: FxHashSet<String>,
216
249
  pub has_base: bool,
217
250
  }
@@ -236,6 +269,12 @@ impl<'m> Index<'m> {
236
269
  xmlids.entry(id).or_default().push(XmlIdDef { module: mi });
237
270
  }
238
271
  let models_with_access = access_models(modules, &model_by_xmlid);
272
+ let mut children: FxHashMap<String, Vec<String>> = FxHashMap::default();
273
+ for (id, nodes) in &views {
274
+ for parent in nodes.iter().filter_map(|n| n.inherit.clone()) {
275
+ children.entry(parent).or_default().push(id.clone());
276
+ }
277
+ }
239
278
  Self {
240
279
  modules,
241
280
  has_base: base.is_some(),
@@ -243,6 +282,7 @@ impl<'m> Index<'m> {
243
282
  models,
244
283
  xmlids,
245
284
  views,
285
+ children,
246
286
  models_with_access,
247
287
  }
248
288
  }
@@ -410,14 +450,62 @@ impl<'m> Index<'m> {
410
450
  /// The model a view applies to, following `inherit_id` when the view does not name one.
411
451
  pub fn view_model(&self, mut id: String) -> Option<&'m str> {
412
452
  for _ in 0..32 {
413
- let (model, inherit) = self.views.get(&id)?;
414
- if let Some(model) = model {
415
- return Some(model);
453
+ let nodes = self.views.get(&id)?;
454
+ if let Some(model) = nodes.iter().find_map(|n| n.fact.model.as_ref()) {
455
+ return Some(&model.value);
456
+ }
457
+ id = nodes.iter().find_map(|n| n.inherit.clone())?;
458
+ }
459
+ None
460
+ }
461
+
462
+ /// The root of the inheritance tree above view `id`, or None when a view on the way is not
463
+ /// indexed or its arch was not read, so the tree's elements would be incomplete.
464
+ pub fn view_root(&self, mut id: String) -> Option<String> {
465
+ for _ in 0..32 {
466
+ let nodes = self.views.get(&id)?;
467
+ if !nodes.iter().any(|n| n.fact.has_arch) {
468
+ return None;
469
+ }
470
+ match nodes.iter().find_map(|n| n.inherit.clone()) {
471
+ Some(parent) => id = parent,
472
+ None => return Some(id),
416
473
  }
417
- id = inherit.clone()?;
418
474
  }
419
475
  None
420
476
  }
477
+
478
+ pub fn view_visible(&self, id: &str, scope: &Scope) -> bool {
479
+ self.views
480
+ .get(id)
481
+ .is_some_and(|nodes| nodes.iter().any(|n| scope.sees(n.module)))
482
+ }
483
+
484
+ /// The elements of every view in the tree under `root`.
485
+ pub fn view_tree(&self, root: String, scope: &Scope) -> ViewTree<'m> {
486
+ let mut tree = ViewTree::default();
487
+ let mut seen = FxHashSet::default();
488
+ let mut stack = vec![root.as_str()];
489
+ while let Some(id) = stack.pop() {
490
+ if !seen.insert(id) {
491
+ continue;
492
+ }
493
+ for node in self.views.get(id).map(Vec::as_slice).unwrap_or_default() {
494
+ let module = self.modules[node.module].name.as_str();
495
+ let elements = node.fact.elements.iter().map(|e| located(e, module));
496
+ if scope.sees(node.module) {
497
+ tree.visible.extend(elements);
498
+ } else {
499
+ for e in elements {
500
+ tree.hidden.entry(e).or_insert(module);
501
+ }
502
+ }
503
+ }
504
+ stack.extend(self.children.get(id).into_iter().flatten().map(String::as_str));
505
+ }
506
+ tree.root = root;
507
+ tree
508
+ }
421
509
  }
422
510
 
423
511
  impl PathError<'_> {
@@ -4,7 +4,7 @@ use ruff_python_parser::parse_module;
4
4
  use ruff_source_file::LineIndex;
5
5
  use ruff_text_size::{Ranged, TextSize};
6
6
 
7
- use crate::facts::{ClassFact, FieldFact, ModelKind, Pos, PyFacts, RawDiag, Spanned};
7
+ use crate::facts::{ClassFact, FieldFact, ModelKind, Pos, PyFacts, RawDiag, Spanned, XmlRef};
8
8
 
9
9
  const FIELD_TYPES: &[&str] = &[
10
10
  "Boolean",
@@ -462,6 +462,49 @@ impl PyVisitor<'_> {
462
462
  }
463
463
  _ => {}
464
464
  }
465
+ self.id_refs(call, expr);
466
+ }
467
+
468
+ fn id_refs(&mut self, call: &ast::ExprCall, expr: &Expr) {
469
+ let first = call.arguments.args.first().and_then(Expr::as_string_literal_expr);
470
+ match &*call.func {
471
+ Expr::Attribute(a) if a.attr.as_str() == "ref" && tail_name(&a.value) == Some("env") => self.env_ref(call),
472
+ Expr::Attribute(a) if a.attr.as_str() == "has_group" => first.into_iter().for_each(|s| self.group_refs(s)),
473
+ _ if field_call(expr).is_some() => {
474
+ if let Some(groups) = keyword(call, "groups").and_then(Expr::as_string_literal_expr) {
475
+ self.group_refs(groups);
476
+ }
477
+ }
478
+ _ => {}
479
+ }
480
+ }
481
+
482
+ /// `env.ref()` raises on an unknown id unless told not to, and then the caller handles it.
483
+ fn env_ref(&mut self, call: &ast::ExprCall) {
484
+ let raise = keyword(call, "raise_if_not_found").or_else(|| call.arguments.args.get(1));
485
+ if raise.is_none_or(|e| matches!(e, Expr::BooleanLiteral(b) if b.value))
486
+ && let Some(id) = call.arguments.args.first().and_then(Expr::as_string_literal_expr)
487
+ && id.value.to_str().contains('.')
488
+ {
489
+ let pos = self.loc.at(id);
490
+ self.facts.xml_refs.push(XmlRef {
491
+ id: id.value.to_str().to_owned(),
492
+ pos,
493
+ });
494
+ }
495
+ }
496
+
497
+ /// The ids in a comma-separated list of groups.
498
+ fn group_refs(&mut self, s: &ast::ExprStringLiteral) {
499
+ let pos = self.loc.at(s);
500
+ let ids = s
501
+ .value
502
+ .to_str()
503
+ .split(',')
504
+ .map(|g| g.trim().trim_start_matches(['!', '-']));
505
+ for id in ids.filter(|id| id.contains('.')) {
506
+ self.facts.group_refs.push(XmlRef { id: id.to_owned(), pos });
507
+ }
465
508
  }
466
509
 
467
510
  fn cursor_call(&mut self, method: &str, expr: &Expr, formatted: bool) {
@@ -87,6 +87,7 @@ rules! {
87
87
  "OD307" "t-raw-unsafe" Security Warning Some(">=15.0"), false, "t-raw is deprecated and renders unescaped HTML; use t-out with Markup.";
88
88
  "OD308" "xmlid-used-before-definition" Correctness Error None, false, "An XML id is referenced before the file that defines it is loaded.";
89
89
  "OD309" "unknown-record-field" Correctness Error None, true, "A data record sets a field its model does not have.";
90
+ "OD310" "unmatched-view-locator" Correctness Error None, true, "An inherited view locates an element its parent views do not have.";
90
91
  "OD401" "model-without-access" Security Warning None, true, "A new model has no ir.model.access rule.";
91
92
  "OD402" "public-write-access" Security Warning None, false, "Public or portal users get write, create or unlink access.";
92
93
  "OD501" "manifest-data-missing" Correctness Error None, false, "A file listed in the manifest does not exist.";
@@ -4,7 +4,7 @@ use regex::Regex;
4
4
  use roxmltree::{Document, Node, ParsingOptions};
5
5
 
6
6
  use crate::facts::{
7
- AccessRow, ArchField, DataFacts, Lines, Pos, RawDiag, RecordFact, Spanned, ViewFact, XmlDef, XmlRef,
7
+ AccessRow, ArchElement, ArchField, DataFacts, Lines, Pos, RawDiag, RecordFact, Spanned, ViewFact, XmlDef, XmlRef,
8
8
  };
9
9
 
10
10
  static EVAL_REF: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#"\bref\(\s*['"]([\w.]+)['"]"#).unwrap());
@@ -21,6 +21,11 @@ static XPATH_AMBIGUOUS: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(^|/)(li
21
21
  pub const UNKNOWN_CONTEXT: &str = "?";
22
22
  /// Prefix of a path segment naming a `<field position="...">` locator rather than a subview.
23
23
  pub const LOCATOR: char = '~';
24
+ /// An xpath step that names its element, as in `field[@name='x']` or `div[@id="x"]`.
25
+ static XPATH_NAMED_STEP: LazyLock<Regex> =
26
+ LazyLock::new(|| Regex::new(r#"([A-Za-z][\w-]*)\[@(name|id)=['"]([^'"]+)['"]\]"#).unwrap());
27
+ /// Attributes by which a spec names the element it locates.
28
+ const LOCATING_ATTRS: [&str; 2] = ["name", "id"];
24
29
  static ACTION_REF: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"%\(([\w.]+)\)d").unwrap());
25
30
 
26
31
  /// Tags under which a `<field>` opens a subview on the field's comodel.
@@ -136,11 +141,12 @@ impl Walker<'_> {
136
141
  if let Some(parent) = child.attribute("inherit_id") {
137
142
  self.reference(parent, child);
138
143
  }
144
+ self.template(child);
139
145
  self.groups(child);
140
146
  self.unsafe_qweb(child);
141
147
  }
142
148
  "menuitem" => self.menuitem(child),
143
- "act_window" | "report" => {
149
+ "act_window" | "report" | "asset" => {
144
150
  self.def(child);
145
151
  self.groups(child);
146
152
  }
@@ -150,6 +156,21 @@ impl Walker<'_> {
150
156
  }
151
157
  }
152
158
 
159
+ /// A `<template>` is a QWeb view whose arch is its content.
160
+ fn template(&mut self, node: Node) {
161
+ let mut view = ViewFact {
162
+ id: node.attribute("id").map(str::to_owned),
163
+ inherit: node.attribute("inherit_id").map(str::to_owned),
164
+ has_arch: true,
165
+ ..ViewFact::default()
166
+ };
167
+ for n in node.descendants().skip(1).filter(Node::is_element) {
168
+ let pos = self.pos(n);
169
+ arch_element(n, pos, &mut view);
170
+ }
171
+ self.facts.views.push(view);
172
+ }
173
+
153
174
  fn menuitem(&mut self, node: Node) {
154
175
  self.def(node);
155
176
  for attr in ["parent", "action"] {
@@ -175,11 +196,13 @@ impl Walker<'_> {
175
196
  };
176
197
  let mut view = ViewFact {
177
198
  id: node.attribute("id").map(str::to_owned),
178
- model: None,
179
- inherit: None,
180
- fields: vec![],
199
+ ..ViewFact::default()
181
200
  };
182
201
  for field in node.children().filter(|c| c.has_tag_name("field")) {
202
+ // Records nested in a one2many field are created with their own XML ids.
203
+ for nested in field.children().filter(|c| c.has_tag_name("record")) {
204
+ self.record(nested);
205
+ }
183
206
  let Some(name) = field.attribute("name") else { continue };
184
207
  let pos = self.pos(field);
185
208
  record.fields.push(Spanned {
@@ -227,6 +250,7 @@ impl Walker<'_> {
227
250
  }
228
251
  "inherit_id" => view.inherit = field.attribute("ref").map(str::to_owned),
229
252
  "arch" | "arch_db" => {
253
+ view.has_arch = true;
230
254
  for root in field.children().filter(Node::is_element) {
231
255
  self.arch(root, &mut Vec::new(), view);
232
256
  }
@@ -238,6 +262,7 @@ impl Walker<'_> {
238
262
  fn arch(&mut self, node: Node, path: &mut Vec<String>, view: &mut ViewFact) {
239
263
  let pos = self.pos(node);
240
264
  let tag = node.tag_name().name();
265
+ arch_element(node, pos, view);
241
266
  self.arch_attrs(node, pos);
242
267
  self.groups(node);
243
268
  self.arch_tag(node, tag, pos);
@@ -323,6 +348,68 @@ impl Walker<'_> {
323
348
  }
324
349
  }
325
350
 
351
+ /// Records `node` as a locator when it is a spec, or else as an element views extending this one
352
+ /// can locate. A spec on another tag than field matches every attribute it carries, so checking one
353
+ /// that names the element is a necessary condition.
354
+ fn arch_element(node: Node, pos: Pos, view: &mut ViewFact) {
355
+ let tag = node.tag_name().name();
356
+ let named = LOCATING_ATTRS
357
+ .iter()
358
+ .filter_map(|&attr| node.attribute(attr).map(|v| (attr, v)))
359
+ .map(|(attr, v)| ArchElement {
360
+ tag: tag.to_owned(),
361
+ attr,
362
+ value: v.to_owned(),
363
+ });
364
+ match tag {
365
+ "xpath" => view
366
+ .locators
367
+ .extend(xpath_locators(node.attribute("expr").unwrap_or_default(), pos)),
368
+ // An attribute spec can rename the element it locates; any other attribute it sets is no element.
369
+ "attribute" => view.elements.extend(renamed(node)),
370
+ _ if node.has_attribute("position") => view.locators.extend(named.take(1).map(|value| Spanned { value, pos })),
371
+ _ => view.elements.extend(named),
372
+ }
373
+ }
374
+
375
+ /// The element an `<attribute name="name">` spec gives a new name to.
376
+ fn renamed(node: Node) -> Option<ArchElement> {
377
+ let attr = LOCATING_ATTRS.into_iter().find(|a| node.attribute("name") == Some(a))?;
378
+ let spec = node.parent_element()?;
379
+ let tag = match spec.tag_name().name() {
380
+ "xpath" => {
381
+ let step = spec.attribute("expr")?.rsplit('/').next()?;
382
+ let step = step.rsplit("::").next()?;
383
+ step.split('[').next()?
384
+ }
385
+ tag => tag,
386
+ };
387
+ Some(ArchElement {
388
+ tag: tag.to_owned(),
389
+ attr,
390
+ value: node.text()?.trim().to_owned(),
391
+ })
392
+ }
393
+
394
+ /// The named steps of an xpath, each of which must match some element. A union or an `or` would
395
+ /// make a step optional, so such expressions are not checked.
396
+ fn xpath_locators(expr: &str, pos: Pos) -> Vec<Spanned<ArchElement>> {
397
+ if expr.contains('|') || expr.contains(" or ") {
398
+ return vec![];
399
+ }
400
+ XPATH_NAMED_STEP
401
+ .captures_iter(expr)
402
+ .map(|c| Spanned {
403
+ value: ArchElement {
404
+ tag: c[1].to_owned(),
405
+ attr: if &c[2] == "name" { "name" } else { "id" },
406
+ value: c[3].to_owned(),
407
+ },
408
+ pos,
409
+ })
410
+ .collect()
411
+ }
412
+
326
413
  /// Extends `path` with where an xpath's target sits: the subviews it reaches into and the field it
327
414
  /// locates.
328
415
  fn xpath_steps(expr: &str, path: &mut Vec<String>) {
File without changes
File without changes