Skip to content

Commit

Permalink
Init stimulus-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoroth committed Sep 8, 2023
0 parents commit ae2746f
Show file tree
Hide file tree
Showing 21 changed files with 2,634 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/**/*.*
22 changes: 22 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint",
"prettier"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"rules": {
"prettier/prettier": ["error"],
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }]
},
"env": {
"node": true
}
}
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
types/**/* linguist-vendored
45 changes: 45 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: "CodeQL"

on:
push:
branches:
- main

pull_request:
branches:
- main

schedule:
- cron: "49 0 * * 6"

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [javascript]

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
queries: +security-and-quality

- name: Autobuild
uses: github/codeql-action/autobuild@v2

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{ matrix.language }}"
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
*.tgz
*.tsbuildinfo
*.log
*~

/dist
node_modules/
coverage/

yarn-error.log

.DS_Store
.rollup.cache
.tool-versions
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20.5.1
25 changes: 25 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.babelrc
.babelrc.js
.DS_Store
.gitignore
.yarn.lock

*.log
*.tsbuildinfo
*.tgz

README.md
rollup.config.js
tsconfig.json
yarn-error.log
*~

/.git
/.github
/.gitattributes

/node_modules
/src
/test
/coverage
/assets
5 changes: 5 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": false,
"printWidth": 120,
"semi": false
}
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Marco Roth

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<h1 align="center">stimulus-parser</h1>

<p align="center">
<a href="https://github.com/marcoroth/stimulus-parser">
<img src="https://github.com/marcoroth/stimulus-parser/actions/workflows/tests.yml/badge.svg">
</a>
<a href="https://www.npmjs.com/package/stimulus-parser">
<img alt="NPM Version" src="https://img.shields.io/npm/v/stimulus-parser?logo=npm&color=38C160">
</a>
<a href="https://www.npmjs.com/package/stimulus-parser">
<img alt="NPM Downloads" src="https://img.shields.io/npm/dm/stimulus-parser?logo=npm&color=38C160">
</a>
<a href="https://bundlephobia.com/package/stimulus-parser">
<img alt="NPM Bundle Size" src="https://img.shields.io/bundlephobia/minzip/stimulus-parser?label=bundle%20size&logo=npm">
</a>
</p>


## Installation

To add `stimulus-parser` to your project, run the following command in your terminal:

```bash
yarn add stimulus-parser
```

## Usage

TODO

## Development

To run the tests:

```bash
yarn install
yarn build
yarn test
```
45 changes: 45 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "stimulus-parser",
"version": "0.1.0",
"type": "module",
"description": "Statically analyze Stimulus controllers",
"main": "dist/stimulus-parser.js",
"module": "dist/stimulus-parser.js",
"types": "dist/types/stimulus-parser.d.ts",
"author": "Marco Roth",
"license": "MIT",
"repository": "https://github.com/marcoroth/stimulus-parser",
"sideEffects": false,
"scripts": {
"prebuild": "yarn clean",
"build": "tsc --noEmit false --declaration true --emitDeclarationOnly true --outDir dist/types && rollup -c",
"watch": "rollup -wc",
"dev": "watch",
"clean": "rimraf dist",
"prerelease": "yarn build",
"lint": "eslint .",
"format": "yarn lint --fix"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.1.0",
"@rollup/plugin-terser": "^0.4.3",
"@rollup/plugin-typescript": "^11.1.2",
"@types/glob": "^8.1.0",
"@typescript-eslint/eslint-plugin": "^5.60.1",
"@typescript-eslint/parser": "^5.60.1",
"acorn": "^8.8.2",
"acorn-static-class-features": "^1.0.0",
"acorn-walk": "^8.2.0",
"eslint": "^8.44.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^4.2.1",
"fs": "^0.0.1-security",
"path": "^0.12.7",
"prettier": "^2.8.8",
"rimraf": "^5.0.1",
"rollup": "^3.29.0",
"rollup-plugin-filesize": "^10.0.0",
"tslib": "^2.6.2",
"typescript": "^5.2.2"
}
}
21 changes: 21 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import resolve from "@rollup/plugin-node-resolve"
import typescript from "@rollup/plugin-typescript"

export default [
{
input: "src/index.ts",
output: [
{
file: "dist/stimulus-parser.js",
format: "esm",
},
],
plugins: [
resolve(),
typescript(),
],
watch: {
include: "src/**",
},
},
]
23 changes: 23 additions & 0 deletions src/controller_definition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export interface ControllerDefinition {
identifier: string
dasherized: string
methods: Array<string>
targets: Array<string>
classes: Array<string>
values: { [key: string]: string }
}

// export class ControllerDefinition {
// readonly identifier: string
// readonly path: string
//
// readonly targets = []
// readonly values = []
// readonly classes = []
// readonly outlets = []
//
// constructor(identifier: string, path: string) {
// this.identifier = identifier
// this.path = path
// }
// }
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./controller_definition"
export * from "./parser"
export * from "./project"
export * from "./types"
69 changes: 69 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { simple } from "acorn-walk"
import { Parser as AcornParser } from "acorn"
import staticClassFeatures from "acorn-static-class-features"

import { dasherize, camelize } from "./util"

import { ControllerDefinition } from "./controller_definition"
import { NodeElement, PropertyElement } from "./types"

export class Parser {
private parser: typeof AcornParser

constructor() {
this.parser = AcornParser.extend(staticClassFeatures)
}

parse(code: string) {
return this.parser.parse(code, {
sourceType: "module",
ecmaVersion: 2020,
})
}

parseController(code: string, filename: string) {
const ast = this.parse(code)

// TODO also check for namespaced controllers
const splits = filename.split("/")
const fileName = splits[splits.length - 1]
const identifier = fileName.split("_controller.js")[0]

const controller: ControllerDefinition = {
identifier: identifier,
dasherized: dasherize(camelize(identifier)),
methods: [],
targets: [],
classes: [],
values: {},
}

simple(ast, {
MethodDefinition(node: any): void {
if (node.kind === "method") {
controller.methods.push(node.key.name)
}
},

PropertyDefinition(node: any): void {
const { name } = node.key

if (name === "targets") {
controller.targets = node.value.elements.map((element: NodeElement) => element.value)
}

if (name === "classes") {
controller.classes = node.value.elements.map((element: NodeElement) => element.value)
}

if (name === "values") {
node.value.properties.forEach((property: PropertyElement) => {
controller.values[property.key.name] = property.value.name
})
}
},
})

return controller
}
}
42 changes: 42 additions & 0 deletions src/project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ControllerDefinition } from "./controller_definition"
import { Parser } from "./parser"

import * as fs from "fs"
import { glob } from "glob"

interface ControllerFile {
filename: string
content: string
}

export class Project {
readonly projectPath: string
readonly controllerDefinitions: ControllerDefinition[] = []
private controllersFiles: Array<ControllerFile> = []

private parser: Parser = new Parser()

constructor(projectPath: string) {
this.projectPath = projectPath
}

async analyze() {
await this.detectControllerFiles()

this.controllersFiles.forEach((file: ControllerFile) => {
this.controllerDefinitions.push(this.parser.parseController(file.content, file.filename))
})
}

async detectControllerFiles() {
const controllerFiles = await glob(`${this.projectPath}/**/*_controller.js`, {
ignore: `${this.projectPath}/node_modules/**/*`,
})

controllerFiles.forEach((filename: string) => {
fs.readFile(filename, "utf8", (_err: any, content) => {
this.controllersFiles.push({ filename, content })
})
})
}
}
Loading

0 comments on commit ae2746f

Please sign in to comment.