tostoolkit Rev. 0.2.3 Source

Guide

Rules

Every diagnostic has a stable code. Codes are API: E071 means one thing forever. New behaviour gets a new code.

Prefixes group by concern:

Range Concern Gated by [header] mode?
E001–E006 file and lexical hygiene no
E031 declaration placement no, but only fires when @kind is declared
E050 asset-class declarations yes — it reads @assets, which only exists in a header
E070–E074 identifiers no
E101–E104, W101–W104 header block yes
E120, W080, W081 artifact kind vs location, naming yes
E401–E407, W401–W402 artifact-specific platform restrictions no — the kind comes from the configured directory when the header omits it
W001, W002 style no
W050 repaint honesty no, but only fires when @repaints: no is declared
W090, W091 unverified rec behaviour no

The header block is always parsed, even when mode = "off"; the mode gates validation and layout, not parsing. Rules about platform behaviour run regardless, because they describe what thinkorswim does, not how your repo is organised — E401 even infers the artifact kind from the configured directory, so a header is not required at all.


File and lexical (always on)#

Code Default Detects
E001 error tab character (ThinkScript sources use 4 spaces)
E002 error missing trailing newline
E003 error // used as a comment — it parses as two division operators
E004 error unbalanced ( ) [ ] { }
E005 error .ts extension, which collides with TypeScript
E006 error file is not valid UTF-8

E003 is the most valuable of these. // is not a comment in ThinkScript, and a file full of // notes fails to apply with an error that points at arithmetic.


Identifiers (always on)#

Code Default Detects
E070 error identifier declared twice (case-insensitive, within a namespace)
E071 error def/rec/input collides with a plot by case
E072 warn call to a function not in the registry
E073 warn assignment without def/rec/plot/input
E074 warn identifier used but never declared in the file

E071 — the one that actually breaks pastes#

rec pdHigh = if BarNumber() == 1 then high else pdHigh[1];
plot PdHigh = pdHigh;      # E071: same identifier as `pdHigh`

ThinkScript identifiers are case-insensitive, so this fails to apply with Identifier Already Used: PdHigh and Too early to access. The fix is to rename one — pdHigh becomes priorDayHigh.

E070 covers same-namespace duplicates (def a twice, input Length plus def length); E071 owns the cross-namespace def↔plot pair, so one bug produces one finding.

Known limitation: E070/E071 inspect top-level statements only. A collision entirely inside a script { … } block is not reported, because each script has its own namespace and scoped analysis is not implemented. Collisions between a script's locals and the outer file are meaningful and will be missed — keep script blocks small, or check them by paste-test.

E074 — the check that was missing#

def firstRthBar = inRth and (isNewDay or !inRth[1]);   # E074: neither is declared

An undeclared name is rejected on paste, and until this rule existed nothing caught it: E072 only validates function calls, so four files referencing an undeclared isNewDay passed tos lint with zero findings and would have failed in the platform.

It is a warning because the analysis is deliberately conservative: names inside a script { … } block are treated as block-local, Namespace.MEMBER and named arguments (close(period = …)) are skipped, and fold variables are recognised. That leaves it quiet on legal code — zero findings across this repository's own scripts and templates — but a stricter treatment is possible if a false negative ever bites.

E073 and E031 are warnings, deliberately#

Two rules describe platform behaviour we have not confirmed in the editor:

  • E073 — the form def x; followed by a top-level x = close; may be legal.
  • E031 — a study with no declare at all may default to the upper panel.

Both ship as warnings. They become errors only after a paste-test proves them, and docs/knowledge-base/unverified-tokens.md records the experiment to run.


Asset classes (profile-gated)#

Code Default Detects
E050 error an option/future function used without the matching @assets entry

Driven by the registry's category, not a hard-coded list, so it stays in sync as the registry grows. A file that calls GetATMOption() must declare @assets: ..., option.


Header block (profile-gated)#

Code Default Detects
E031 warn a study or strategy that does not open with a declare (unverified; needs @kind)
E101 error missing header block
E102 error missing required key
E103 error invalid @status / @kind / @panel / @assets / @timeframes
E104 error @inputs omits a declared input
W050 warn AggregationPeriod used while @repaints: no
W101 warn unknown header key
W102 warn @version is not semver
W103 warn @inputs documents an input that does not exist
W104 warn @inputs order differs from declaration order

E031 only runs when the artifact kind is known — without a header there is nothing to say the file is a study.

W050 exists because a higher-timeframe read updates through the session. A file that claims @repaints: no while reading daily data is lying to anyone backtesting it.


Artifact kind vs location (profile-gated)#

Code Default Detects
E120 error @kind disagrees with the directory (scan outside scans/)
W080 warn file name is not lower_snake_case
W081 warn missing _scan / _strat / _col suffix

These fire only when the file sits inside a configured kind directory, and only when header validation is enabled. There is no rule enforcing a studies/<category>/ subdirectory structure — the category is your business.


Artifact restrictions (always on)#

These are the platform's rules, not ours.

Code Default Detects
E401 error chart-only function inside a scan
E402 error function unavailable in a watchlist column
E403 error a scan with no plot at all
E404 error a column with no output (neither a plot nor an AddLabel)
E405 error declare upper/lower in a scan or column
W401 warn strategy with no AddOrder
W402 warn scan plot is a constant (any nonzero number counts), so every symbol matches
E406 error scan exposes more than one plot
E407 error scan passes period = … (a secondary aggregation)

AddLabel and AssignBackgroundColor are legal in columns; the other drawing functions are not. Function names are matched case-insensitively, because ThinkScript identifiers are.

E403 checks that a scan exposes a plot somewhere, not that the plot is the last statement: plot Scan; followed by Scan = …; is documented and allowed. It does not attempt to prove the expression is boolean-comparison shaped.

E406 enforces Schwab's single-plot rule for Stock Hacker filters. W402 treats any nonzero numeric literal as always-true, because ThinkScript does.

E406 enforces Schwab's documented rule that a Stock Hacker study filter carries exactly one plot. Two plots leaves the scanner using whichever it picks, which is worse than a failed build.

These do not require a header. If a file has no @kind, the kind is inferred from the configured directory, so a headerless scans/foo.thinkscript that calls AddLabel still gets E401. Platform restrictions are not house style, and the loose profile does not weaken them.


Style and unverified behaviour (always on)#

Code Default Detects
W001 warn trailing whitespace
W002 warn line over 100 columns (header # @key: lines exempt)
W090 warn a rec referencing itself 3+ times
W091 warn a rec self-referencing at an offset greater than [1]

W091 flags offsets greater than 1. [0] (the current bar) is not flagged.

W090/W091 encode unverified platform behaviour. One self-reference per branch — rec x = if c then x[1] + 1 else x[1]; — is the ordinary form and is not flagged. Three or more has no known precedent, so it warns and asks for a paste-test rather than pretending to be certain.


Silencing a rule#

[checks]
W090 = "off"                # one code
unknown_functions = "off"   # one setting

Prefer silencing a specific code over switching to the loose profile: the former is a decision you can see, the latter hides everything in Layer 2 at once.

View this page's source