Skip to content

Commit

Permalink
Add support for parsing TypeScript files (#49)
Browse files Browse the repository at this point in the history
* Add methods to handle and find controllers with different fileendings
* Use `@typescript-eslint/parser` for parsing
* Detect .ts files in project
* Rename `ts-rails` to `typescript` controller
* Resolve types warnings
* Rename `filename` argument
* Switch to `@typescript-eslint/typescript-estree` for parsing
* Add @typescript-eslint/types
* Change TypeScript `module` from `commonjs` to `es2020` and let Vite bundle up the commonjs file
* Change TypeScript `module` and `moduleResolution` to `node16`
* Drop Vite
* Drop direct `@typescript-eslint/types` dependency

---------

Co-authored-by: Fran Zekan <[email protected]>
Co-authored-by: Carlos Garcia <[email protected]>
  • Loading branch information
3 people authored Feb 9, 2024
1 parent b0e1037 commit 2283575
Show file tree
Hide file tree
Showing 6 changed files with 652 additions and 168 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {
"@hotwired/stimulus-webpack-helpers": "^1.0.1",
"acorn": "^8.11.2",
"@typescript-eslint/typescript-estree": "^6.21.0",
"acorn-walk": "^8.3.1",
"fs": "^0.0.1-security",
"glob": "^10.3.10"
Expand Down
22 changes: 13 additions & 9 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
import { simple } from "acorn-walk"
import { Parser as AcornParser } from "acorn"
import * as ESLintParser from "@typescript-eslint/typescript-estree"

import { Project } from "./project"
import { ControllerDefinition, defaultValuesForType } from "./controller_definition"
import { NodeElement, PropertyValue } from "./types"

import type { Program } from "acorn"

type NestedArray<T> = T | NestedArray<T>[]
type NestedObject<T> = {
[k: string]: T | NestedObject<T>
}

export class Parser {
private readonly project: Project
private parser: typeof AcornParser
private parser: typeof ESLintParser

constructor(project: Project) {
this.project = project
this.parser = AcornParser
this.parser = ESLintParser
}

parse(code: string): Program {
parse(code: string, filename?: string) {
return this.parser.parse(code, {
sourceType: "module",
ecmaVersion: "latest",
filePath: filename
})
}

parseController(code: string, filename: string) {
try {
const ast = this.parse(code)
const ast = this.parse(code, filename)
const controller = new ControllerDefinition(this.project, filename)

simple(ast, {
simple(ast as any, {
MethodDefinition(node: any): void {
if (node.kind === "method") {
controller.methods.push(node.key.name)
const methodName = node.key.name
const isPrivate = node.accessibility === "private" || node.key.type === "PrivateIdentifier"
const name = isPrivate ? `#${methodName}` : methodName

controller.methods.push(name)
}
},

Expand Down
4 changes: 2 additions & 2 deletions src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class Project {
}

get controllerRoot() {
return this.controllerRoots[0] || this.controllerRootFallback
return this.controllerRoots[0] || this.controllerRootFallback
}

get controllerRoots() {
Expand All @@ -102,7 +102,7 @@ export class Project {
const relativePath = this.relativePath(path)
const relativeRoots = this.controllerRoots.map(root => this.relativePath(root))

return relativeRoots.find(root => relativePath.startsWith(root)) || this.controllerRootFallback
return relativeRoots.find(root => relativePath.startsWith(root)) || this.controllerRootFallback
}

private async readControllerFiles() {
Expand Down
Loading

0 comments on commit 2283575

Please sign in to comment.