ESLint Plugin Flowtype provides comprehensive linting rules for Flow type annotations in JavaScript projects. It offers 49 specialized rules for validating Flow syntax, enforcing code style, and preventing common type-related errors. The plugin integrates seamlessly with ESLint configurations and provides shareable configurations for consistent Flow type checking across projects.
npm install eslint-plugin-flowtypeESLint plugins are loaded through ESLint configuration rather than direct imports:
// .eslintrc.js
module.exports = {
plugins: ['flowtype'],
extends: ['plugin:flowtype/recommended'],
rules: {
// Individual rule configuration
'flowtype/boolean-style': ['error', 'boolean'],
'flowtype/define-flow-type': 'warn'
}
};For accessing plugin utilities programmatically:
const flowtypePlugin = require('eslint-plugin-flowtype');// .eslintrc.js
module.exports = {
parser: '@babel/eslint',
parserOptions: {
babelOptions: {
plugins: [
'@babel/plugin-transform-react-jsx',
'@babel/plugin-syntax-flow'
]
}
},
plugins: ['flowtype'],
extends: ['plugin:flowtype/recommended'],
settings: {
flowtype: {
onlyFilesWithFlowAnnotation: false
}
}
};// @flow
type User = {
name: string,
age: number,
active: boolean
};
function processUser(user: User): string {
return `${user.name} (${user.age})`;
}ESLint Plugin Flowtype is structured around several key components:
Rules that validate Flow type syntax and prevent common type-related errors, including duplicate type detection, weak type prevention, and Flow-specific syntax validation.
// Plugin structure
const plugin = {
configs: {
recommended: ESLintConfig
},
rules: {
[ruleName: string]: ESLintRule
},
rulesConfig: {
[ruleName: string]: number
}
};
interface ESLintConfig {
parser: string;
parserOptions: ParserOptions;
plugins: string[];
rules: { [ruleName: string]: RuleConfiguration };
settings: { flowtype: FlowtypeSettings };
}
interface ESLintRule {
create: (context: ESLintContext) => RuleImplementation;
meta?: RuleMeta;
schema?: JSONSchema;
}Rules that enforce consistent formatting and style for Flow type annotations, including spacing, delimiters, and code organization.
interface RuleConfiguration {
0 | 1 | 2 | 'off' | 'warn' | 'error': RuleLevel;
[options: any[]]: RuleOptions;
}
interface FlowtypeSettings {
onlyFilesWithFlowAnnotation?: boolean;
}Rules that enforce the presence of type annotations where required, including parameter types, return types, and variable types.
interface ESLintContext {
getFilename(): string;
getSourceCode(): SourceCode;
report(descriptor: ReportDescriptor): void;
options: any[];
}
interface ReportDescriptor {
node: ASTNode;
message: string;
fix?: (fixer: RuleFixer) => Fix;
}Predefined ESLint configurations and settings for Flow type checking integration.
interface ParserOptions {
babelOptions: {
plugins: string[];
};
}
const configs = {
recommended: ESLintConfig
};Helper functions for Flow file detection, AST processing, and rule implementation support.
function checkFlowFileAnnotation(ruleCreate: Function): Function;
function isFlowFile(context: ESLintContext): boolean;
function fuzzyStringMatch(needle: string, haystack: string): boolean;interface ASTNode {
type: string;
range: [number, number];
loc: SourceLocation;
}
interface SourceLocation {
start: Position;
end: Position;
}
interface Position {
line: number;
column: number;
}
interface RuleFixer {
insertTextBefore(node: ASTNode, text: string): Fix;
insertTextAfter(node: ASTNode, text: string): Fix;
replaceText(node: ASTNode, text: string): Fix;
removeRange(range: [number, number]): Fix;
}
interface Fix {
range: [number, number];
text: string;
}
interface RuleMeta {
docs?: {
description: string;
category: string;
recommended: boolean;
};
fixable?: 'code' | 'whitespace';
schema?: JSONSchema;
}