Skip to content

Commit 91a2b48

Browse files
committed
chore: refactor lint file results
1 parent 1f86be4 commit 91a2b48

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

lib/lint/file.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -257,46 +257,47 @@ export class LintFile {
257257

258258
constructor ( file, { cwd, processUnsupportedTypes, write, fix = true, log = true, cache } = {} ) {
259259
this.#file = File.new( file );
260+
this.#cwd = cwd;
260261
this.#processUnsupportedTypes = Boolean( processUnsupportedTypes );
261262
this.#write = Boolean( write );
262263
this.#cache = cache || {};
263264
this.#fix = Boolean( fix );
264265
this.#log = Boolean( log );
266+
}
267+
268+
// properties
269+
get cwd () {
270+
return this.#cwd;
271+
}
265272

273+
get path () {
274+
return this.#path;
275+
}
276+
277+
// public
278+
async run ( action ) {
266279
if ( !this.#file.path ) {
267-
throw new Error( "File path is required" );
280+
return result( [ 500, "File path is required" ] );
268281
}
269282

270283
if ( path.isAbsolute( this.#file.path ) ) {
271284
this.#path = path.normalize( this.#file.path );
272285
}
273286
else {
274-
if ( !cwd ) {
275-
throw new Error( "Cwd is required" );
287+
if ( !this.#cwd ) {
288+
return result( [ 500, "Cwd is required when file path is relative" ] );
276289
}
277290

278-
this.#path = path.normalize( path.join( cwd, this.#file.path ) );
291+
this.#path = path.normalize( path.join( this.#cwd, this.#file.path ) );
279292
}
280293

281294
this.#cwd = path.dirname( this.#path );
282-
}
283-
284-
// properties
285-
get cwd () {
286-
return this.#cwd;
287-
}
288295

289-
get path () {
290-
return this.#path;
291-
}
292-
293-
// public
294-
async run ( action ) {
295-
if ( this.#write && !this.path ) {
296-
throw new Error( "Path is required" );
296+
if ( this.#write && !this.#path ) {
297+
return result( [ 500, "File path is required to write content" ] );
297298
}
298299

299-
this.#fullPath = this.path;
300+
this.#fullPath = this.#path;
300301

301302
// read file content
302303
this.#originalData = this.#data = await this.#file.text( "utf8" );
@@ -321,7 +322,7 @@ export class LintFile {
321322
if ( this.#type ) {
322323
const extnames = customMime.get( this.#type.type )?.extnames;
323324

324-
if ( extnames?.default && !extnames.has( path.extname( this.path ) ) ) {
325+
if ( extnames?.default && !extnames.has( path.extname( this.#path ) ) ) {
325326
this.#fullPath += extnames.default;
326327
}
327328
}
@@ -340,7 +341,7 @@ export class LintFile {
340341
res.meta.bytesDelta = this.#data.length - this.#originalData.length;
341342

342343
if ( this.#write ) {
343-
await fs.promises.writeFile( this.path, this.#data );
344+
await fs.promises.writeFile( this.#path, this.#data );
344345
}
345346
}
346347
else {
@@ -499,7 +500,7 @@ export class LintFile {
499500
this.#cache.prettierConfigs ||= {};
500501

501502
if ( !this.#cache.prettierConfigs[ configs.prettier ] ) {
502-
this.#cache.prettierConfigs[ configs.prettier ] = await prettier.resolveConfig( this.path, {
503+
this.#cache.prettierConfigs[ configs.prettier ] = await prettier.resolveConfig( this.#path, {
503504
"config": configs.prettier,
504505
"editorconfig": false,
505506
} );
@@ -541,7 +542,7 @@ export class LintFile {
541542
try {
542543

543544
// pre-parse package.json
544-
if ( path.basename( this.path ) === "package.json" ) {
545+
if ( path.basename( this.#path ) === "package.json" ) {
545546
const res = this.#runJson( "compress" );
546547

547548
if ( !res.ok ) return res;

0 commit comments

Comments
 (0)