Coder Social home page Coder Social logo

rightcapitalhq / phpdoc-parser Goto Github PK

View Code? Open in Web Editor NEW
18.0 8.0 1.0 1.21 MB

Next-gen PHPDoc parser with support for intersection types and generics(TypeScript version)

License: MIT License

JavaScript 1.37% Shell 0.04% TypeScript 98.58%
ast javascript parser php phpdoc phpdoc-parser phpstan rightcapital typescript php7

phpdoc-parser's Introduction

PHPDoc parser TypeScript version

made by RightCapital GitHub Workflow Status (with event) Conventional Commits RightCapital frontend style guide

Next-gen PHPDoc parser with support for intersection types and generics(TypeScript version)

What's that

This parser is inspired by the PHPStan's phpdoc-parser library: https://github.com/phpstan/phpdoc-parser

This library @rightcapital/phpdoc-parser represents PHPDocs with an AST (Abstract Syntax Tree). It supports parsing and modifying PHPDocs by using TypeScript/JavaScript.

For the complete list of supported PHPDoc features check out PHPStan documentation.

Installation

# pnpm
pnpm add @rightcapital/phpdoc-parser
# yarn
yarn add @rightcapital/phpdoc-parser
# npm
npm install --save @rightcapital/phpdoc-parser

Basic usage

import {
  ConstExprParser,
  Lexer,
  PhpDocParser,
  TokenIterator,
  TypeParser,
} from '@rightcapital/phpdoc-parser';

// basic setup

const lexer = new Lexer();
const constExprParser = new ConstExprParser();
const typeParser = new TypeParser(constExprParser);
const phpDocParser = new PhpDocParser(typeParser, constExprParser);

// parsing and reading a PHPDoc string

const tokens = new TokenIterator(lexer.tokenize('/** @param Lorem $a */'));
const phpDocNode = phpDocParser.parse(tokens); // PhpDocNode
const paramTags = phpDocNode.getParamTagValues(); // ParamTagValueNode[]
console.log(paramTags[0].parameterName); // '$a'
console.log(paramTags[0].type); // IdentifierTypeNode { attributes: {}, name: 'Lorem' }

Format-preserving printer

This component can be used to modify the AST and print it again as close as possible to the original.

It's heavily inspired by format-preserving printer component in nikic/PHP-Parser.

import {
  CloningVisitor,
  ConstExprParser,
  IdentifierTypeNode,
  Lexer,
  NodeTraverser,
  PhpDocNode,
  PhpDocParser,
  TokenIterator,
  TypeParser,
  Printer,
} from '@rightcapital/phpdoc-parser';

const usedAttributes = { lines: true, indexes: true };

const lexer = new Lexer();
const constExprParser = new ConstExprParser(true, true, usedAttributes);
const typeParser = new TypeParser(constExprParser, true, usedAttributes);
const phpDocParser = new PhpDocParser(
  typeParser,
  constExprParser,
  true,
  true,
  usedAttributes,
);

const tokens = new TokenIterator(lexer.tokenize('/** @param Lorem $a */'));
const phpDocNode = phpDocParser.parse(tokens); // PhpDocNode

const cloningTraverser = new NodeTraverser([new CloningVisitor()]);

const [newPhpDocNode] = cloningTraverser.traverse([phpDocNode]) as [PhpDocNode];

// change something in newPhpDocNode
newPhpDocNode.getParamTagValues()[0].type = new IdentifierTypeNode('Ipsum');

// print changed PHPDoc
const printer = new Printer();
const newPhpDoc = printer.print(newPhpDocNode);
console.log(newPhpDoc);
// --- result ---
// /**
//  * @param Ipsum $a
//  */

const newPhpDocWithFormatPreserving = printer.printFormatPreserving(
  newPhpDocNode,
  phpDocNode,
  tokens,
);
console.log(newPhpDocWithFormatPreserving); // '/** @param Ipsum $a */'

Welcome to contribute

We are stilling waiting for someones to contribute, especially for the following features.

  • Doctrine Annotations support
  • More tests
  • More docs
  • A script to monitor upstream updates and notify the author to catch up.

Please check out our Contribution guide

How did we create the initial version of this project.

We created most of our code by using ChatGPT as a tool to transform most code from https://github.com/phpstan/phpdoc-parser to TypeScript version.

Our PHP API haven't used Doctrine, so Doctrine support are removed.

License

MIT License © 2023-Present

phpdoc-parser's People

Contributors

frantic1048 avatar liby avatar rainx avatar renovate[bot] avatar yilun-sun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phpdoc-parser's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • chore(deps): lock file maintenance

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • actions/checkout v4
  • actions/setup-node v4.0.3
  • actions/checkout v4
  • actions/setup-node v4.0.3
  • actions/checkout v4
  • actions/setup-node v4.0.3
  • actions/checkout v4
  • actions/setup-node v4.0.3
.github/workflows/codeql.yml
  • actions/checkout v4
  • github/codeql-action v3
  • github/codeql-action v3
.github/workflows/labels.yml
  • actions/checkout v4
  • micnncim/action-label-syncer v1.3.0
.github/workflows/lock.yml
  • dessant/lock-threads v5.0.1
.github/workflows/pr-labels.yml
  • jesusvasquez333/verify-pr-label-action v1.4.0
.github/workflows/stale.yml
  • actions/stale v9
.github/workflows/typo-check.yml
  • actions/checkout v4
nodenv
.node-version
  • node 20.16.0
npm
package.json
  • @types/node 20.14.15
  • lodash 4.17.21
  • typescript 5.5.4
  • @babel/core 7.24.7
  • @babel/preset-env 7.24.7
  • @babel/preset-typescript 7.24.7
  • @commitlint/cli 19.3.0
  • @commitlint/config-conventional 19.2.2
  • @commitlint/cz-commitlint 19.2.0
  • @rightcapital/eslint-config-javascript 20.1.2
  • @rightcapital/eslint-config-typescript 20.1.2
  • @rightcapital/prettier-config 6.0.16
  • @types/lodash 4.17.5
  • beachball 2.43.1
  • commitizen 4.3.0
  • eslint 8.57.0
  • husky 9.0.11
  • inquirer 9.2.23
  • prettier 3.3.2
  • vitest 1.6.0
  • node >=16.x
  • pnpm >=8.x
  • pnpm 9.7.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.