Skip to content

False positive "Undefined namespace or variable" on Pine v6 user-defined types (UDT) #15

Description

@pash7ka

Summary

The validator flags .new() calls and field access on user-defined types (UDTs) as Undefined namespace or variable, even though type X declarations and X.new(...) are valid Pine v6 (and have been supported since v5.7).

Closely related to #10 (enums) — both UDTs and enums declared via type/enum aren't tracked by accurateValidator.

Environment

  • Extension: jpantsjoha.pinescript-v6-extension v0.4.4
  • VS Code: any recent
  • OS: platform-independent

Minimal repro

//@version=6
indicator("UDT repro", overlay=true)

type Foo
    float a
    float b

newFoo() =>
    Foo.new(na, na)

var Foo state = newFoo()
plot(close)

Expected

No diagnostics. The script compiles and runs correctly in the TradingView Pine Editor.

Actual

Red squiggle on Foo.new(na, na):

Undefined namespace or variable 'Foo'

Same error appears on any later Foo.field or Foo.new(...) reference.

Root cause

In accurateValidator.ts, checkUndefinedNamespaces treats every X.Y pattern as namespace access and checks only three sources:

if (!this.knownNamespaces.has(namespace) &&
    !this.declaredVariables.has(namespace) &&
    !this.isBuiltInVariable(namespace)) {
    this.addError(... `Undefined namespace or variable '${namespace}'`);
}

There's no tracking of type X declarations, so UDT names never enter any registry the check consults.

Suggested minimal fix

  1. Add a declaredTypes: Set<string> field to AccurateValidator.
  2. Clear it at the top of validate(), alongside declaredVariables.clear().
  3. In the existing first-pass loop, also call collectDeclaredTypes(line) which matches /^\s*(?:export\s+)?type\s+([A-Za-z_]\w*)\s*$/ and adds the captured name to declaredTypes.
  4. Extend the check above with !this.declaredTypes.has(namespace).

This suppresses the false positive without trying to validate UDT field access (which would be a larger change requiring multi-line block parsing and a field registry).

I've been running a local patch along these lines against v0.4.4 — it cleanly removes the false positives without re-introducing other ones (verified with NoSuchThing.foo() counter-tests that still correctly flag).

If you'd like a PR with the minimal fix + a test fixture, happy to send one.

Why this matters

UDTs are the idiomatic way to bundle related state in modern v6 indicators and strategies. Right now the extension produces persistent red diagnostics on any non-trivial v6 codebase using type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions