Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser Rework: NPM Package Support, import/export tracing, Internal Structure Refactor, Playground and more #66

Merged
merged 97 commits into from
Feb 25, 2024

Conversation

marcoroth
Copy link
Owner

@marcoroth marcoroth commented Feb 23, 2024

This pull request is a big revamp of the existing parser and also adds a lot of new features. Here are the highlights:

NPM Package Support

We now fully support Stimulus controllers imported from NPM packages installed on disk. We can trace the import declaration to the resolved source file within the NPM package. If the import is importing a class declaration, we can fully resolve the import to the class declaration in the NPM package.

Sadly the parser doesn't yet resolve controllers imported from import-mapped NPM packages.

Stimulus Parser Playground

The pull requests features a fully interactive web-based playground to interact and inspect the parse results. The playground allows users to encode the input into a sharable URL that can come in heady for bug and issue reports.

CleanShot 2024-02-23 at 14 44 09

New Internal Structure and classes to represent objects

We already had:

  • Project
  • Parser
  • ControllerDefinition
  • MethodDefinition
  • ValueDefinition
  • ClassDefinition
  • TargetDefinition

Newly added:

  • SourceFile - A representation of a (parsed) JavaScript/TypeScript source file.
  • ImportDeclaration - A representation of an import declaration within a SourceFile. If a single import statement imports two constants it will be represented with two ImportDeclaration objects. We decide between default, named and namespace imports.
  • ExportDeclaration - A representation of an export declaration within a SourceFile. If a single export statement exports two constants it will be represented with two ExportDeclaration objects. We decide between default, named and namespace exports.
  • ClassDeclaration - A representation of any class declaration within a SourceFile. If class declaration defines a Stimulus controller it will have the controllerDefinition populated.
  • NodeModule - The node module represents a detected NPM package. It holds a SourceFile for each file it contains.
  • ApplicationFile - The file where the Stimulus application is defined/started in and exported from.
  • ControllersIndexFile - Responsible for detecting the registered controllers on the application.
  • RegisteredController - A ControllerDefinition /identifier pair register which controllers where registered on the application with which identifier.

Analysis of registered controllers on the Stimulus application

The Project now exposes controllerDefinitions and registeredControllers, where controllerDefinitions contain any of the controller definitions the parser found in the project and registeredControllers are the controllers the were actually detected to be registered on the Stimulus Application with an identifier.

The controllerDefinitions have a guessIdentifier property that uses a best effort strategy to come up with the most likely identifier somewhere would register this controllers as, this also includes controllers found in the installed NPM packages.

The registeredControllers have an identifier property, for which we are certain the this controller was registered with this identifier, either by manually calling application.register(...) or through one of the autoload plugins we support. Currently we have support for the following mechanisms:

  • register
  • stimulus-loading-lazy
  • stimulus-loading-eager
  • esbuild-rails
  • stimulus-vite-helpers
  • stimulus-webpack-helpers

But we would love to implement support for other autoload mechanisms.

Test coverage

This pull requests also introduces tons of new tests to make sure we ensure we don't introduce any regressions.

Resolved Issues

Closes #29
Closes #51
Closes #52
Closes #54
Closes #61
Closes #63
Closes #64
Closes #65

marcoroth and others added 19 commits February 20, 2024 19:11
* Adds Laravel importmap eager and lazy loading examples

* Adds the vite-laravel setup

* Add tests for Laravel app fixtures

---------

Co-authored-by: Marco Roth <marco.roth@intergga.ch>
Also added symetrical getters for `valueNames` and `localValueNames` and two additional getters for keeping the return value of previous getter around: `valueDefinitionsMap` and `localValueDefinitionsMap`

Also added `node` to all `ControllerPropertyDefinition`

And added another validation to detect duplicate properties definitions if the parent already defined it
playground/server.dev.mjs Fixed Show fixed Hide fixed
src/util/ast.ts Fixed Show fixed Hide fixed
src/util/ast.ts Fixed Show fixed Hide fixed
src/util/ast.ts Fixed Show fixed Hide fixed
test/packages/app.test.ts Fixed Show fixed Hide fixed
src/controllers_index_file.ts Dismissed Show dismissed Hide dismissed
playground/server.prod.mjs Fixed Show fixed Hide fixed
playground/server.prod.mjs Fixed Show fixed Hide fixed
playground/server.dev.mjs Fixed Show fixed Hide fixed
playground/server.dev.mjs Dismissed Show dismissed Hide dismissed
@marcoroth marcoroth merged commit e0c2880 into main Feb 25, 2024
8 checks passed
@marcoroth marcoroth deleted the parser-rework branch February 25, 2024 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment