Skip to content

Commit 300a50a

Browse files
authored
Merge pull request #20 from ziqq/master
feat: add optional Error object and StackTrace to generated state's
2 parents c0d3701 + 08a586a commit 300a50a

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# Change Log
22

3-
All notable changes to the "flutter-plus" extension will be documented in this file.
3+
All notable changes to the "flutter-plus" extension will be documented in this file.
4+
5+
## 0.7.1
6+
- **ADDED**: Error object and StackTrace to generated state's

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "Flutter Plus",
44
"description": "Extension with various improvements for Flutter",
55
"icon": "assets/logo.png",
6-
"version": "0.7.0",
6+
"version": "0.7.1",
77
"pricing": "Free",
88
"engines": {
99
"vscode": "^1.92.0"

src/commands/sealed-states.command.ts

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ export const sealedStates = async (uri: Uri) => {
141141
{ label: "Generate property getters", picked: true, id: 'propertyGetters' },
142142
{ label: "Generate type alias", picked: true, id: 'typeAlias' },
143143
{ label: "Generate equality operator (==)", picked: true, id: 'equalityOperator' },
144+
{ label: "Add error object", picked: false, id: 'error' },
145+
{ label: "Add stackTrace", picked: false, id: 'stackTrace' },
144146
];
145147

146148
const selectedOptions = await vscode.window.showQuickPick(options, {
@@ -155,6 +157,8 @@ export const sealedStates = async (uri: Uri) => {
155157
let propertyGettersOption = selectedOptions.find(option => option.id === 'propertyGetters') !== undefined;
156158
let typeAliasOption = selectedOptions.find(option => option.id === 'typeAlias') !== undefined;
157159
let initialStateOption = selectedOptions.find(option => option.id === 'initialState') !== undefined;
160+
let errorOption = selectedOptions.find(option => option.id === 'error') !== undefined;
161+
let stackTraceOption = selectedOptions.find(option => option.id === 'stackTrace') !== undefined;
158162

159163
const dataType = nullableDataOption ? '\${1}Entity?' : '\${1}Entity';
160164

@@ -174,7 +178,7 @@ export const sealedStates = async (uri: Uri) => {
174178

175179
// Constructor
176180
codeBuilder.push(` /// {@macro \${2}}`);
177-
codeBuilder.push(` const \${1}({required super.data, required super.message});`);
181+
codeBuilder.push(` const \${1}({required super.data, required super.message${errorOption ? ', super.error' : ''}${stackTraceOption ? ', super.stackTrace' : ''}});`);
178182

179183
// Generate the factory constructors for each state
180184
Object.values(stateFormats).forEach(({ pascalCase, camelCase }) => {
@@ -188,6 +192,12 @@ export const sealedStates = async (uri: Uri) => {
188192
codeBuilder.push(` required ${dataType} data,`);
189193
}
190194
codeBuilder.push(` String message,`);
195+
if (errorOption) {
196+
codeBuilder.push(` Object? error,`);
197+
}
198+
if (stackTraceOption) {
199+
codeBuilder.push(` StackTrace? stackTrace,`);
200+
}
191201
codeBuilder.push(` }) = \${1}\\$${pascalCase};`);
192202
});
193203

@@ -203,6 +213,12 @@ export const sealedStates = async (uri: Uri) => {
203213
codeBuilder.push(` required ${dataType} data,`);
204214
}
205215
codeBuilder.push(` String? message,`);
216+
if (errorOption) {
217+
codeBuilder.push(` Object? error,`);
218+
}
219+
if (stackTraceOption) {
220+
codeBuilder.push(` StackTrace? stackTrace,`);
221+
}
206222
codeBuilder.push(` }) =>`);
207223
if (Object.values(stateFormats).find(({ camelCase }) => camelCase === 'idle')) {
208224
codeBuilder.push(` \${1}\\$Idle(`);
@@ -211,6 +227,12 @@ export const sealedStates = async (uri: Uri) => {
211227
}
212228
codeBuilder.push(` data: data,`);
213229
codeBuilder.push(` message: message ?? 'Initial',`);
230+
if (errorOption) {
231+
codeBuilder.push(` error: error,`);
232+
}
233+
if (stackTraceOption) {
234+
codeBuilder.push(` stackTrace: stackTrace,`);
235+
}
214236
codeBuilder.push(` );`);
215237
}
216238

@@ -223,10 +245,11 @@ export const sealedStates = async (uri: Uri) => {
223245
codeBuilder.push(`final class \${1}\\$${pascalCase} extends \${1} {`);
224246

225247
if (nullableDataOption) {
226-
codeBuilder.push(` const \${1}\\$${pascalCase}({super.data, super.message = '${pascalCase}'});`);
248+
codeBuilder.push(` const \${1}\\$${pascalCase}({super.data, super.message = '${pascalCase}'${errorOption ? ', super.error' : ''}${stackTraceOption ? ', super.stackTrace' : ''}`);
227249
} else {
228-
codeBuilder.push(` const \${1}\\$${pascalCase}({required super.data, super.message = '${pascalCase}'});`);
250+
codeBuilder.push(` const \${1}\\$${pascalCase}({required super.data, super.message = '${pascalCase}'${errorOption ? ', super.error' : ''}${stackTraceOption ? ', super.stackTrace' : ''}`);
229251
}
252+
codeBuilder.push(`});`);
230253

231254
if (typeAliasOption) {
232255
codeBuilder.push('');
@@ -248,7 +271,8 @@ export const sealedStates = async (uri: Uri) => {
248271
codeBuilder.push('');
249272
codeBuilder.push('@immutable');
250273
codeBuilder.push(`abstract base class _\\$\${1}Base {`);
251-
codeBuilder.push(` const _\\$\${1}Base({required this.data, required this.message});`);
274+
codeBuilder.push(` const _\\$\${1}Base({required this.data, required this.message${errorOption ? ', this.error' : ''}${stackTraceOption ? ', this.stackTrace' : ''}`);
275+
codeBuilder.push(`});`);
252276

253277
// Type alias
254278
if (typeAliasOption) {
@@ -269,6 +293,22 @@ export const sealedStates = async (uri: Uri) => {
269293
codeBuilder.push(` @nonVirtual`);
270294
codeBuilder.push(` final String message;`);
271295

296+
// Error object
297+
if (errorOption) {
298+
codeBuilder.push('');
299+
codeBuilder.push(` /// Error object.`);
300+
codeBuilder.push(` @nonVirtual`);
301+
codeBuilder.push(` final Object? error;`);
302+
}
303+
304+
// Stack trace object
305+
if (stackTraceOption) {
306+
codeBuilder.push('');
307+
codeBuilder.push(` /// Stack trace object.`);
308+
codeBuilder.push(` @nonVirtual`);
309+
codeBuilder.push(` final StackTrace? stackTrace;`);
310+
}
311+
272312
// Check existence of data
273313
if (nullableDataOption) {
274314
codeBuilder.push('');

0 commit comments

Comments
 (0)