- What is mck89/peast used for in a Laravel application?
- Peast is a PHP library that parses JavaScript into an ESTree-compliant abstract syntax tree and also supports tokenization, traversal, selector queries, and rendering. In Laravel you can use it for static analysis, validating embedded JavaScript, AST-based transformations, or generating JavaScript payloads from PHP. It does not execute JavaScript, so runtime behavior still requires Node.js, V8, or another JavaScript engine.
- How do I install mck89/peast with Composer in Laravel?
- Install it with composer require mck89/peast:^1.17. Laravel's normal vendor autoloading handles the package, so no separate autoloader setup is needed. You can then call Peast::latest with your JavaScript source and options to generate an AST from a controller, job, or service class. No Laravel-specific package or service provider is required, although you can create one for convenience.
- Does mck89/peast support the latest ECMAScript and ESTree output?
- Peast parses JavaScript according to the ECMAScript specification and produces AST nodes following the ESTree standard. Version 1.17.0 adds ES2025 support, but very new proposals may still lag behind tools such as Babel or Acorn. Test Peast against the exact syntax features your Laravel application needs before relying on it for newer JavaScript.
- Can I use mck89/peast to parse JavaScript in Blade templates or user uploads?
- Yes, you can parse JavaScript strings found in Blade templates or uploaded files, but treat untrusted input carefully. Peast only parses code and does not execute it, yet malformed or deeply nested JavaScript can still consume significant memory or CPU. Validate input size and nesting, run parsing in a queued job or sandboxed process for uploads, and watch Xdebug nesting limits during development.
- How do I integrate mck89/peast with Laravel's service container?
- Register Peast as a singleton or bind a small parser wrapper in a Laravel service provider if you want shared configuration across controllers, jobs, and Blade directives. The wrapper can call Peast::latest or a version-specific parser with your preferred options. Avoid storing mutable parser state globally, and create a fresh parser instance per source string. This keeps AST generation isolated, testable, and easier to swap later.
- What Laravel and PHP versions does mck89/peast support?
- Peast itself supports PHP 5.4 and newer, and it works with modern Laravel stacks running PHP 8.1 and above. Laravel 9 and later applications can install it via Composer without Laravel framework conflicts. For current Laravel versions, use a supported PHP runtime and test parsing against your application's target JavaScript syntax.
- How do I test JavaScript parsing with mck89/peast in PHPUnit?
- In PHPUnit, feed sample JavaScript strings to Peast and assert on AST node types, names, or selector query results. Use fixtures for both ES5 and modern syntax, and compare critical ESTree shapes rather than full dumps if formatting changes. You can also test tokenization and rendering round trips. Mock uploaded files or Blade content to cover Laravel integration paths without relying on external JavaScript runtimes.
- What are the performance and production concerns when using Peast?
- Benchmark parsing on realistic file sizes because AST complexity can degrade performance with large or deeply nested JavaScript. Run parsing in background jobs for uploads or bulk analysis, cache ASTs when reuse is possible, and monitor memory limits. In development, Xdebug nesting limits can cause fatal errors, so raise xdebug.max_nesting_level or disable Xdebug for parser-heavy tasks.
- Does mck89/peast support TypeScript or executing JavaScript?
- Peast does not support TypeScript directly and does not execute JavaScript. Pre-process TypeScript to JavaScript with a TypeScript compiler before parsing it with Peast. For execution or runtime evaluation, use Node.js, v8js, or another JavaScript runtime instead. Peast is intended for static parsing, analysis, and AST-based manipulation inside PHP applications.
- What are alternatives to mck89/peast for JavaScript parsing in PHP or Laravel?
- Alternatives include Node.js parsers such as @babel/parser and Acorn, which often track newer ECMAScript proposals faster. In PHP, Peast is a focused option for ESTree-compatible parsing without requiring a Node runtime. Choose Peast for static analysis inside Laravel and keep Node tooling for execution, bundling, or transformation pipelines that need the latest JavaScript syntax.