Skip to content

Commit

Permalink
chore(eslint): enable typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
joris-gallot committed May 5, 2024
1 parent 3cd80f1 commit 5575cb0
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 67 deletions.
12 changes: 6 additions & 6 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { defineBuildConfig } from "unbuild";
import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
entries: ["src/index.ts", "src/cli.ts"],
entries: ['src/index.ts', 'src/cli.ts'],
declaration: true,
rollup: {
emitCJS: true,
esbuild: {
target: "esnext",
}
}
});
target: 'esnext',
},
},
})
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import antfu from '@antfu/eslint-config'

export default antfu(
{
typescript: true,
ignores: [
'**/fixtures',
],
Expand Down
54 changes: 28 additions & 26 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { existsSync } from 'fs';
import { parseTscOutput, getFinalOutput} from './index.js'
import { readFile } from 'fs/promises';
import { resolve } from 'path';
import { TypestepConfig } from './types.js';
import { tryImport } from './utils.js';
/* eslint-disable no-console */
import { existsSync } from 'node:fs'
import { readFile } from 'node:fs/promises'
import { resolve } from 'node:path'
import process from 'node:process'
import type { TypestepConfig } from './types.js'
import { tryImport } from './utils.js'
import { getFinalOutput, parseTscOutput } from './index.js'

function getTscOutputPath() {
const args = process.argv.slice(2);
const args = process.argv.slice(2)

if (args.length === 0) {
console.log('No tsc output file provided');
process.exit(1);
console.log('No tsc output file provided')
process.exit(1)
}
const tscOutputPath = args[0];

const tscOutputPath = args[0]

if (!existsSync(tscOutputPath)) {
console.log(`File ${tscOutputPath} does not exist`);
process.exit(1);
console.log(`File ${tscOutputPath} does not exist`)
process.exit(1)
}

return tscOutputPath
Expand All @@ -27,34 +29,34 @@ async function readConfigFile() {
const configFile = resolve(process.cwd(), 'typestep.config.ts')

if (!existsSync(configFile)) {
console.log('No config file found');
console.log('No config file found')
return
}

const configModule = await tryImport(configFile);
const configModule = await tryImport(configFile)

if (!configModule?.default) {
console.log('No default export found in config file');
console.log('No default export found in config file')
return
}

return configModule.default as TypestepConfig
}

async function run() {
const tscOutput = await readFile(getTscOutputPath(), 'utf8');
const configFile = await readConfigFile();
const parsedTscOutput = parseTscOutput(tscOutput);
const errors = getFinalOutput(parsedTscOutput, configFile).map(({ error }) => error);
const tscOutput = await readFile(getTscOutputPath(), 'utf8')
const configFile = await readConfigFile()
const parsedTscOutput = parseTscOutput(tscOutput)

const errors = getFinalOutput(parsedTscOutput, configFile).map(({ error }) => error)

if (errors.length === 0) {
console.log('No tsc errors found');
return;
console.log('No tsc errors found')
return
}

console.error(errors.length);
console.error(errors.length)
// process.exit(0);
}

run();
run()
25 changes: 12 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { TscDiagnostic, TypestepConfig } from './types.js';
import type { TscDiagnostic, TypestepConfig } from './types.js'

function parseTsError(tscError: string): TscDiagnostic {
const regex = /([^()\n]+)\((\d+),(\d+)\):\s+error\s+(TS\d+):\s+(.*)/;
const [_, file, line, column, tsCode, error] = regex.exec(tscError) || []
const regex = /([^()\n]+)\((\d+),(\d+)\):\s+error\s+(TS\d+):\s+(.*)/
const [_, file, line, column, tsCode, error] = regex.exec(tscError) || []

if (!file || !line || !column || !tsCode || !error) {
if (!file || !line || !column || !tsCode || !error)
throw new Error('Invalid error format')
}

return {
path: file,
cursor: {
line: parseInt(line),
column: parseInt(column)
line: Number.parseInt(line),
column: Number.parseInt(column),
},
error,
tsCode
tsCode,
}
}

Expand All @@ -27,29 +26,29 @@ export function parseTscOutput(tscOutput: string) {
const tscError = tscErrors[i]

// If the error starts with a space, we suppose it's a continuation of the previous error
if (tscError.startsWith(" ")) {
if (tscError.startsWith(' ')) {
const lastError = finalErrors[finalErrors.length - 1]
lastError.error += `\n${tscError}`
continue
}

try {
finalErrors.push(parseTsError(tscError))
} catch (error) {
}
catch (error) {
// Ignore error for now
}
}

return finalErrors
}

export function getFinalOutput(parsedTscOutput: Array<TscDiagnostic>, config?: TypestepConfig) {
const { ignoredFiles = [] } = config || {}
let finalOutput = parsedTscOutput

if (ignoredFiles.length > 0) {
if (ignoredFiles.length > 0)
finalOutput = finalOutput.filter(({ path }) => !ignoredFiles.includes(path))
}

return finalOutput
}
16 changes: 9 additions & 7 deletions src/playground.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { readFile } from 'fs/promises';
import { parseTscOutput } from './index.js';
import { resolve } from 'path';
/* eslint-disable no-console */
import { readFile } from 'node:fs/promises'
import { resolve } from 'node:path'
import { parseTscOutput } from './index.js'

async function run() {
let tscOutput
try {
tscOutput = await readFile(resolve(__dirname, '..', 'tsc-output.log'), 'utf8');
} catch (error) {
throw new Error('Could not read tsc-output.log');
tscOutput = await readFile(resolve(__dirname, '..', 'tsc-output.log'), 'utf8')
}
catch (error) {
throw new Error('Could not read tsc-output.log')
}

const parsedTscOutput = parseTscOutput(tscOutput)
console.log(parsedTscOutput)
}

run()
run()
12 changes: 6 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export type TscDiagnostic = {
path: string,
cursor: { line: number, column: number },
tsCode: string,
export interface TscDiagnostic {
path: string
cursor: { line: number, column: number }
tsCode: string
error: string
}

export type TypestepConfig = {
export interface TypestepConfig {
ignoredFiles?: string[]
}
}
19 changes: 10 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import jiti from "jiti";
import process from 'node:process'
import jiti from 'jiti'

export function tryImport(file: string, rootDir: string = process.cwd()) {
// @ts-expect-error "This expression is not callable." but works fine
const _import = jiti(rootDir, { interopDefault: true, esmResolve: true });
// @ts-expect-error "This expression is not callable." but works fine
const _import = jiti(rootDir, { interopDefault: true, esmResolve: true })

try {
return _import(file);
} catch (error: any) {
if (error.code !== "MODULE_NOT_FOUND") {
console.error(`Error trying import ${file} from ${rootDir}`, error);
};
return _import(file)
}
}
catch (error: any) {
if (error.code !== 'MODULE_NOT_FOUND')
console.error(`Error trying import ${file} from ${rootDir}`, error)
}
}

0 comments on commit 5575cb0

Please sign in to comment.