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_methods
Fix 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_last
Fix 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 StatelessComponent
Top level
Creates a new StatelessComponent
class.
Create StatefulComponent
Top level
Creates a new StatefulComponent
and respective State
class.
Create InheritedComponent
Top level
Creates a new InheritedComponent
class.
Convert to StatefulComponent
Class level
Converts a custom StatelessComponent
into a StatefulComponent
and respective State
class.
Convert to AsyncStatelessComponent
Class level
Converts a custom StatelessComponent
into an AsyncStatelessComponent
.
Remove this component
Component 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 Builder
Component tree level
Wraps the selected component with a Builder
.
yield div([
Builder(builder: (context) sync* {
yield span([text('Hello World')]);
}),
]);
Extract component
Component tree level
Extracts the selected component plus subtree into a new StatelessComponent
.
Add styles
Component 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 import
Directive 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.