Linting

Jaspr comes with its own set of lint rules and code assists.

Jaspr has its own linting and analyzer package called jaspr_lints. This comes pre-configured with every new project and enables the following set of lint rules and code assists:

Lint Rules

prefer_html_methodsFix available

Prefer html methods like div(...) over DomComponent(tag: 'div', ...).

BAD:

yield DomComponent(
  tag: 'div',
  children: [
    DomComponent(
      tag: 'p',
      child: Text('Hello World'),
    ),
  ],
);

GOOD:

yield div([
  p([text('Hello World')]),
]);
sort_children_properties_lastFix available

Sort children properties last in html component methods.

This improves readability and best represents actual html.

BAD:

yield div([
  p([text('Hello World')], classes: 'text-red'),
], id: 'main');

GOOD:

yield div(id: 'main', [
  p(classes: 'text-red', [text('Hello World')]),
]);

Code Assists

Create StatelessComponentTop level

Creates a new StatelessComponent class.

Create StatefulComponentTop level

Creates a new StatefulComponent and respective State class.

Create InheritedComponentTop level

Creates a new InheritedComponent class.

Convert to StatefulComponentClass level

Converts a custom StatelessComponent into a StatefulComponent and respective State class.

Convert to AsyncStatelessComponentClass level

Converts a custom StatelessComponent into an AsyncStatelessComponent.

Remove this componentComponent tree level

Surgically removes the selected component from the component tree, making its children the new direct children of its parent.

Wrap with html...Component tree level

Wraps the selected component with a new html component.

yield div([
  p([ 
    span([text('Hello World')]),
  ]), 
]);
Wrap with component...Component tree level

Wraps the selected component with a new component.

yield div([
  MyComponent( 
    child: span([text('Hello World')]),
  ), 
]);
Wrap with BuilderComponent tree level

Wraps the selected component with a Builder.

yield div([
  Builder(builder: (context) sync* { 
    yield span([text('Hello World')]);
  }), 
]);
Extract componentComponent tree level

Extracts the selected component plus subtree into a new StatelessComponent.

Add stylesComponent tree level

Adds new css styles to the selected component.

This will either add a new class name:

yield div(
  classes: '[...]'
  [],
);

/* ... */

@css
static List<StyleRule> styles = [ 
  css('.[...]').box(...), 
]; 

Or use an existing id or class name:

yield div(id: 'content', []);

/* ... */

@css
static List<StyleRule> styles = [ 
  css('#content').box(...), 
]; 
Convert to web-only importDirective level

Converts any import to a web-only import using the @Import annotation.

import 'dart:html'; 

@Import.onWeb('dart:html', show: [#window]) 
import 'filename.imports.dart'; 

This will auto-detect all used members from the import and add them to the show parameter.

Convert to server-only import

Converts any import to a server-only import using the @Import annotation.

import 'dart:io'; 

@Import.onServer('dart:io', show: [#HttpRequest]) 
import 'filename.imports.dart'; 

This will auto-detect all used members from the import and add them to the show parameter.