JavaScript Rules
This guide shows how to write a custom JavaScript rule that reads type information from typescript-go and runs on both .ts and .vue files.
You will build a js/unsafe-calls rule that reports any call where the called value has type any, then run it on the example files.
Install Dependencies
Section titled “Install Dependencies”This example uses Vue so the same rule runs on a plain TypeScript file and an embedded-language file.
npm i -D golar @golar/vue vuepnpm add -D golar @golar/vue vueyarn add -D golar @golar/vue vueCreate Example Files
Section titled “Create Example Files”Create index.ts:
export declare const fn: any
fn()Create index.vue:
<script lang="ts" setup> import { fn } from './index.ts'</script>
<template> {{ fn() }}</template>Write the Rule
Section titled “Write the Rule”Create js-rule.ts:
import { defineRule } from 'golar/unstable'import { isCallExpression, TypeFlags, type CallExpression,} from 'golar/unstable-tsgo'
export const jsRule = defineRule({ name: 'js/unsafe-calls', setup(context) {
context.sourceFile.forEachChild(function visit(node) { if (isCallExpression(node)) { checkCall(node) }
node.forEachChild(visit) })
function checkCall(node: CallExpression) {
const type = context.program.getTypeAtLocation(node.expression) if (type == null) { return }
if ((type.flags & TypeFlags.Any) !== 0 && type.intrinsicName === 'any') { context.report({ message: 'Unsafe any call.', range: { begin: node.expression.pos, end: node.expression.end, }, }) } } },})This rule works in three steps: traverse the file, ask Golar for the type of each called expression, and report an error if that type is any.
The important part is context.program.getTypeAtLocation(...). That is what makes the rule type-aware instead of syntax-only.
Register the Rule
Section titled “Register the Rule”Create golar.config.ts:
import { defineConfig } from 'golar/unstable'import '@golar/vue'
import { jsRule } from './js-rule.ts'
export default defineConfig({ lint: { use: [ { files: ['index.ts', 'index.vue'], rules: [jsRule], }, ], },})The lint.use entry tells Golar which files should use the rule.
Run the Rule
Section titled “Run the Rule”Run Golar in the default mode:
npx golarpnpm golaryarn golarYou should see js/unsafe-calls errors for both index.ts and index.vue.