Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
update with custom plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
imrishabh18 committed Jul 10, 2024
1 parent a92f21c commit 0d33e4b
Showing 1 changed file with 90 additions and 32 deletions.
122 changes: 90 additions & 32 deletions lib/cmd-fns/lint.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
import { AppContext } from "lib/util/app-context"
import { z } from "zod"
import kleur from "kleur"
import { ESLint } from "eslint"
import path from "path"
import * as glob from "glob"
import kleur from "kleur"
import { AppContext } from "lib/util/app-context"
import path from "path"
import { z } from "zod"
import { Rule } from "eslint"

const tscircuitPlugin = {
rules: {
"capacitor-unit": {
meta: {
type: "problem",
docs: {
description: "Ensure capacitor values include units",
category: "Possible Errors",
recommended: true,
},
fixable: "code",
},
create(context: Rule.RuleContext) {
return {
CallExpression(node: any) {
if (node.callee.name === 'Capacitor') {
const valueArg = node.arguments.find((arg: any) => arg.type === 'ObjectExpression')
if (valueArg) {
const valueProp = valueArg.properties.find((prop: any) => prop.key.name === 'value')
if (valueProp && valueProp.value.type === 'Literal') {
const value = valueProp.value.value
if (typeof value === 'string' && !value.match(/[µuμ]F$/)) {
context.report({
node: valueProp,
message: 'Capacitor value should include units (e.g., "100µF")',
fix(fixer) {
return fixer.replaceText(valueProp.value, `"${value}µF"`)
}
})
}
}
}
}
}
}
}
}
}
}

export const lintCmd = async (ctx: AppContext, args: any) => {
const params = z
Expand All @@ -15,50 +56,67 @@ export const lintCmd = async (ctx: AppContext, args: any) => {
const { cwd } = ctx
const { fix } = params

console.log(kleur.blue("Running linter..."))
console.log(kleur.blue("Running tscircuit linter..."))

// Initialize ESLint
const eslint = new ESLint({
useEslintrc: false,
baseConfig: {
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 2020,
sourceType: "module",
},
plugins: ["tscircuit"],
rules: {
"tscircuit/capacitor-unit": "error" // or "warn" if you prefer
}
},
fix: fix,
fix,
extensions: [".ts", ".tsx"],
ignorePath: path.join(cwd, '.eslintignore'),
plugins: {
tscircuit: tscircuitPlugin
}
})

// Find all TypeScript files
const files = glob.sync("**/*.{ts,tsx}", {
cwd: cwd,
cwd,
ignore: ["**/node_modules/**", "dist/**"],
})

// Run ESLint
const results = await eslint.lintFiles(files.map(file => path.join(cwd, file)))
try {
const results = await eslint.lintFiles(
files.map((file) => path.join(cwd, file))
)

// Fix files if requested
if (fix) {
await ESLint.outputFixes(results)
}
if (fix) {
await ESLint.outputFixes(results)
}

// Format results
const formatter = await eslint.loadFormatter("stylish")
const resultText = formatter.format(results)
const formatter = await eslint.loadFormatter("stylish")
const resultText = formatter.format(results)

// Output results
console.log(resultText)
console.log(resultText)

// Determine if there were any errors
const errorCount = results.reduce((count, result) => count + result.errorCount, 0)
const errorCount = results.reduce(
(count, result) => count + result.errorCount,
0
)

if (errorCount > 0) {
console.log(kleur.red(`\nLinting failed with ${errorCount} error${errorCount === 1 ? '' : 's'}.`))
if (errorCount > 0) {
console.log(
kleur.yellow(
`\nFound ${errorCount} issue${
errorCount === 1 ? "" : "s"
} in your tscircuit code.`
)
)
process.exit(1)
} else {
console.log(kleur.green("\nNo tscircuit-specific issues found!"))
}
} catch (error) {
console.error(kleur.red("An error occurred during linting:"))
console.error(error)
process.exit(1)
} else {
console.log(kleur.green("\nLinting passed successfully!"))
}
}
}

0 comments on commit 0d33e4b

Please sign in to comment.