#8237·linguist

Add `required` (boolean) field in heuristics.yml

Author: DecimalTurnCreated Sep 29, 2026Updated Sep 29, 2026
LabelsImprovement

Currently, the file how-linguist-works.md states the following (emphasis mine):

The language [...] is then determined using the following strategies, in order, with each step either identifying the precise language or reducing the number of likely languages passed down to the next strategy

However, this is currently not true in the strict sense for the heuristics strategy: it never reduces the number of likely languages, it's "all or nothing", ie. either it finds the language or it leaves the list of candidate languages unchanged.

One way to change this and make sure that we maximize the information gained from running the heyuristics strategy would be to implement a field named required. Having true for that field would mean that the heuristic pattern must match, otherwise the language will be removed from the candidates. The value would be false false by default when not specified.

This should help improve the accuracy of the classifier since it would have less candidates to deal with.

An example of how this would look:

diff
	- extensions: ['.bas']
	  rules:
	  - language: B4X
	    pattern: '\A\W{0,3}(?:.*(?:\r?\n|\r)){0,9}B4(?:J|A|R|i)=true'
+       required: true
	  - language: FreeBASIC
	    named_pattern: freebasic
	  - language: FreeBASIC
	    and:
	    - pattern: '(?i)^[ \t]*return '
	    - negative_pattern: '(?i)[ \t]*gosub '
	  - language: BASIC
	    pattern: '\A\s*\d'
+       required: true
	  - language: QuickBASIC
	    named_pattern: quickbasic
	  - language: VBA
	    named_pattern: vba
	  - language: Visual Basic 6.0
	    named_pattern: vb-module
+       required: true

In this context, B4X and VB6 have mandatory metadata at the top of the file, so this is required and the absence of such pattern means that it can't be either of those, so they are removed from the candidates. Similarly for BASIC, all lines must start with a line number, so if there isn't at least one we know it's not BASIC.

Let me know if this is a desirable feature and a PR would be welcome to implement this. Feedback would also be appreciated.

Source: github-linguist/linguist