Configuring lint settings with flowlint
comments allows you to specify different
settings within a file and different settings to different regions of different
files. These comments come in three forms:
In all forms, whitespace and asterisks between words are ignored, allowing for flexible formatting.
flowlint
The basic flowlint
comment takes a comma-delimited list of rule:severity
pairs and
applies those settings for the rest of the source file until overridden. This has
three primary purposes: applying settings over a block, applying settings over a file,
and applying settings over part of a line.
settings over a block of code:
A pair of flowlint
comments can be used to apply a certain setting over a block of code.
For example, to disabling the untyped-type-import lint over a block of type imports would look like this:
1
2
3
4
5
6
7
|
import type {
Foo,
Bar,
Baz,
} from './untyped.js';
|
{"value":"import type {\n // flowlint untyped-type-import:off\n Foo,\n Bar,\n Baz,\n // flowlint untyped-type-import:error\n} from './untyped.js';\n","tokens":[{"type":"T_IMPORT","context":"normal","value":"import","line":1,"start":0,"end":6},{"type":"T_TYPE","context":"normal","value":"type","line":1,"start":7,"end":11},{"type":"T_LCURLY","context":"normal","value":"{","line":1,"start":12,"end":13},{"type":"Line","context":"comment","value":"// flowlint untyped-type-import:off","line":2,"start":16,"end":51},{"type":"T_IDENTIFIER","context":"type","value":"Foo","line":3,"start":54,"end":57},{"type":"T_COMMA","context":"normal","value":",","line":3,"start":57,"end":58},{"type":"T_IDENTIFIER","context":"type","value":"Bar","line":4,"start":61,"end":64},{"type":"T_COMMA","context":"normal","value":",","line":4,"start":64,"end":65},{"type":"T_IDENTIFIER","context":"type","value":"Baz","line":5,"start":68,"end":71},{"type":"T_COMMA","context":"normal","value":",","line":5,"start":71,"end":72},{"type":"Line","context":"comment","value":"// flowlint untyped-type-import:error","line":6,"start":75,"end":112},{"type":"T_RCURLY","context":"normal","value":"}","line":7,"start":113,"end":114},{"type":"T_IDENTIFIER","context":"normal","value":"from","line":7,"start":115,"end":119},{"type":"T_STRING","context":"normal","value":"'./untyped.js'","line":7,"start":120,"end":134},{"type":"T_SEMICOLON","context":"normal","value":";","line":7,"start":134,"end":135}],"errors":[]}
settings over a file:
A flowlint
comment doesn’t have to have a matching comment to form a block.
An unmatched comment simply applies its settings to the rest of the file. You
could use this, for example, to suppress all sketchy-null-check lints in a particular file:
{"value":"// flowlint sketchy-null:off\n...\n","tokens":[{"type":"Line","context":"comment","value":"// flowlint sketchy-null:off","line":1,"start":0,"end":28},{"type":"T_ELLIPSIS","context":"normal","value":"...","line":2,"start":29,"end":32}],"errors":[]}
settings over part of a line:
The settings applied by flowlint
start and end right at the comment itself. This
means that you can do things like
1
2
3
4
5
6
7
|
function (a: ?boolean, b: ?boolean) {
if (a && b) {
...
} else {
...
}
}
|
{"value":"function (a: ?boolean, b: ?boolean) {\n if (/* flowlint sketchy-null-bool:off */a/* flowlint sketchy-null-bool:warn */ && b) {\n ...\n } else {\n ...\n }\n}\n","tokens":[{"type":"T_FUNCTION","context":"normal","value":"function","line":1,"start":0,"end":8},{"type":"T_LPAREN","context":"normal","value":"(","line":1,"start":9,"end":10},{"type":"T_IDENTIFIER","context":"normal","value":"a","line":1,"start":10,"end":11},{"type":"T_COLON","context":"normal","value":":","line":1,"start":11,"end":12},{"type":"T_PLING","context":"normal","value":"?","line":1,"start":13,"end":14},{"type":"T_IDENTIFIER","context":"normal","value":"boolean","line":1,"start":14,"end":21},{"type":"T_COMMA","context":"normal","value":",","line":1,"start":21,"end":22},{"type":"T_IDENTIFIER","context":"normal","value":"b","line":1,"start":23,"end":24},{"type":"T_COLON","context":"normal","value":":","line":1,"start":24,"end":25},{"type":"T_PLING","context":"normal","value":"?","line":1,"start":26,"end":27},{"type":"T_IDENTIFIER","context":"normal","value":"boolean","line":1,"start":27,"end":34},{"type":"T_RPAREN","context":"normal","value":")","line":1,"start":34,"end":35},{"type":"T_LCURLY","context":"normal","value":"{","line":1,"start":36,"end":37},{"type":"T_IF","context":"normal","value":"if","line":2,"start":40,"end":42},{"type":"T_LPAREN","context":"normal","value":"(","line":2,"start":43,"end":44},{"type":"Block","context":"comment","value":"/* flowlint sketchy-null-bool:off */","line":2,"start":44,"end":80},{"type":"T_IDENTIFIER","context":"normal","value":"a","line":2,"start":80,"end":81},{"type":"Block","context":"comment","value":"/* flowlint sketchy-null-bool:warn */","line":2,"start":81,"end":118},{"type":"T_AND","context":"normal","value":"&&","line":2,"start":119,"end":121},{"type":"T_IDENTIFIER","context":"normal","value":"b","line":2,"start":122,"end":123},{"type":"T_RPAREN","context":"normal","value":")","line":2,"start":123,"end":124},{"type":"T_LCURLY","context":"normal","value":"{","line":2,"start":125,"end":126},{"type":"T_ELLIPSIS","context":"normal","value":"...","line":3,"start":131,"end":134},{"type":"T_RCURLY","context":"normal","value":"}","line":4,"start":137,"end":138},{"type":"T_ELSE","context":"normal","value":"else","line":4,"start":139,"end":143},{"type":"T_LCURLY","context":"normal","value":"{","line":4,"start":144,"end":145},{"type":"T_ELLIPSIS","context":"normal","value":"...","line":5,"start":150,"end":153},{"type":"T_RCURLY","context":"normal","value":"}","line":6,"start":156,"end":157},{"type":"T_RCURLY","context":"normal","value":"}","line":7,"start":158,"end":159}],"errors":[]}
if you want control at an even finer level than you get from the line-based comments.
flowlint-line
A flowlint-line
comment works similarly to a flowlint
comment, except it only
applies its settings to the current line instead of applying them for the rest of the file.
The primary use for flowlint-line
comments is to suppress a lint on a particular line:
1
2
3
4
5
6
7
|
function (x: ?boolean) {
if (x) {
...
} else {
...
}
}
|
{"value":"function (x: ?boolean) {\n if (x) { // flowlint-line sketchy-null-bool:off\n ...\n } else {\n ...\n }\n}\n","tokens":[{"type":"T_FUNCTION","context":"normal","value":"function","line":1,"start":0,"end":8},{"type":"T_LPAREN","context":"normal","value":"(","line":1,"start":9,"end":10},{"type":"T_IDENTIFIER","context":"normal","value":"x","line":1,"start":10,"end":11},{"type":"T_COLON","context":"normal","value":":","line":1,"start":11,"end":12},{"type":"T_PLING","context":"normal","value":"?","line":1,"start":13,"end":14},{"type":"T_IDENTIFIER","context":"normal","value":"boolean","line":1,"start":14,"end":21},{"type":"T_RPAREN","context":"normal","value":")","line":1,"start":21,"end":22},{"type":"T_LCURLY","context":"normal","value":"{","line":1,"start":23,"end":24},{"type":"T_IF","context":"normal","value":"if","line":2,"start":27,"end":29},{"type":"T_LPAREN","context":"normal","value":"(","line":2,"start":30,"end":31},{"type":"T_IDENTIFIER","context":"normal","value":"x","line":2,"start":31,"end":32},{"type":"T_RPAREN","context":"normal","value":")","line":2,"start":32,"end":33},{"type":"T_LCURLY","context":"normal","value":"{","line":2,"start":34,"end":35},{"type":"Line","context":"comment","value":"// flowlint-line sketchy-null-bool:off","line":2,"start":36,"end":74},{"type":"T_ELLIPSIS","context":"normal","value":"...","line":3,"start":79,"end":82},{"type":"T_RCURLY","context":"normal","value":"}","line":4,"start":85,"end":86},{"type":"T_ELSE","context":"normal","value":"else","line":4,"start":87,"end":91},{"type":"T_LCURLY","context":"normal","value":"{","line":4,"start":92,"end":93},{"type":"T_ELLIPSIS","context":"normal","value":"...","line":5,"start":98,"end":101},{"type":"T_RCURLY","context":"normal","value":"}","line":6,"start":104,"end":105},{"type":"T_RCURLY","context":"normal","value":"}","line":7,"start":106,"end":107}],"errors":[]}
flowlint-next-line
flowlint-next-line
works the same as flowlint-line
, except it applies its settings to the next line instead of the current line:
1
2
3
4
5
6
7
8
|
function (x: ?boolean) {
if (x) {
...
} else {
...
}
}
|
{"value":"function (x: ?boolean) {\n // flowlint-next-line sketchy-null-bool:off\n if (x) {\n ...\n } else {\n ...\n }\n}\n","tokens":[{"type":"T_FUNCTION","context":"normal","value":"function","line":1,"start":0,"end":8},{"type":"T_LPAREN","context":"normal","value":"(","line":1,"start":9,"end":10},{"type":"T_IDENTIFIER","context":"normal","value":"x","line":1,"start":10,"end":11},{"type":"T_COLON","context":"normal","value":":","line":1,"start":11,"end":12},{"type":"T_PLING","context":"normal","value":"?","line":1,"start":13,"end":14},{"type":"T_IDENTIFIER","context":"normal","value":"boolean","line":1,"start":14,"end":21},{"type":"T_RPAREN","context":"normal","value":")","line":1,"start":21,"end":22},{"type":"T_LCURLY","context":"normal","value":"{","line":1,"start":23,"end":24},{"type":"Line","context":"comment","value":"// flowlint-next-line sketchy-null-bool:off","line":2,"start":27,"end":70},{"type":"T_IF","context":"normal","value":"if","line":3,"start":73,"end":75},{"type":"T_LPAREN","context":"normal","value":"(","line":3,"start":76,"end":77},{"type":"T_IDENTIFIER","context":"normal","value":"x","line":3,"start":77,"end":78},{"type":"T_RPAREN","context":"normal","value":")","line":3,"start":78,"end":79},{"type":"T_LCURLY","context":"normal","value":"{","line":3,"start":80,"end":81},{"type":"T_ELLIPSIS","context":"normal","value":"...","line":4,"start":86,"end":89},{"type":"T_RCURLY","context":"normal","value":"}","line":5,"start":92,"end":93},{"type":"T_ELSE","context":"normal","value":"else","line":5,"start":94,"end":98},{"type":"T_LCURLY","context":"normal","value":"{","line":5,"start":99,"end":100},{"type":"T_ELLIPSIS","context":"normal","value":"...","line":6,"start":105,"end":108},{"type":"T_RCURLY","context":"normal","value":"}","line":7,"start":111,"end":112},{"type":"T_RCURLY","context":"normal","value":"}","line":8,"start":113,"end":114}],"errors":[]}