Skip to content

Commit

Permalink
chore: refactor lint file results
Browse files Browse the repository at this point in the history
  • Loading branch information
zdm committed Dec 30, 2024
1 parent 1f86be4 commit 91a2b48
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions lib/lint/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,46 +257,47 @@ export class LintFile {

constructor ( file, { cwd, processUnsupportedTypes, write, fix = true, log = true, cache } = {} ) {
this.#file = File.new( file );
this.#cwd = cwd;
this.#processUnsupportedTypes = Boolean( processUnsupportedTypes );
this.#write = Boolean( write );
this.#cache = cache || {};
this.#fix = Boolean( fix );
this.#log = Boolean( log );
}

// properties
get cwd () {
return this.#cwd;
}

get path () {
return this.#path;
}

// public
async run ( action ) {
if ( !this.#file.path ) {
throw new Error( "File path is required" );
return result( [ 500, "File path is required" ] );
}

if ( path.isAbsolute( this.#file.path ) ) {
this.#path = path.normalize( this.#file.path );
}
else {
if ( !cwd ) {
throw new Error( "Cwd is required" );
if ( !this.#cwd ) {
return result( [ 500, "Cwd is required when file path is relative" ] );
}

this.#path = path.normalize( path.join( cwd, this.#file.path ) );
this.#path = path.normalize( path.join( this.#cwd, this.#file.path ) );
}

this.#cwd = path.dirname( this.#path );
}

// properties
get cwd () {
return this.#cwd;
}

get path () {
return this.#path;
}

// public
async run ( action ) {
if ( this.#write && !this.path ) {
throw new Error( "Path is required" );
if ( this.#write && !this.#path ) {
return result( [ 500, "File path is required to write content" ] );
}

this.#fullPath = this.path;
this.#fullPath = this.#path;

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

if ( extnames?.default && !extnames.has( path.extname( this.path ) ) ) {
if ( extnames?.default && !extnames.has( path.extname( this.#path ) ) ) {
this.#fullPath += extnames.default;
}
}
Expand All @@ -340,7 +341,7 @@ export class LintFile {
res.meta.bytesDelta = this.#data.length - this.#originalData.length;

if ( this.#write ) {
await fs.promises.writeFile( this.path, this.#data );
await fs.promises.writeFile( this.#path, this.#data );
}
}
else {
Expand Down Expand Up @@ -499,7 +500,7 @@ export class LintFile {
this.#cache.prettierConfigs ||= {};

if ( !this.#cache.prettierConfigs[ configs.prettier ] ) {
this.#cache.prettierConfigs[ configs.prettier ] = await prettier.resolveConfig( this.path, {
this.#cache.prettierConfigs[ configs.prettier ] = await prettier.resolveConfig( this.#path, {
"config": configs.prettier,
"editorconfig": false,
} );
Expand Down Expand Up @@ -541,7 +542,7 @@ export class LintFile {
try {

// pre-parse package.json
if ( path.basename( this.path ) === "package.json" ) {
if ( path.basename( this.#path ) === "package.json" ) {
const res = this.#runJson( "compress" );

if ( !res.ok ) return res;
Expand Down

0 comments on commit 91a2b48

Please sign in to comment.