npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

code-sniper

v0.1.3

Published

A clone of code sniffer (PHPCS) for javascript

Downloads

7

Readme

code-sniper

npm version Build Status Coverage Status Gitter

A clone of code sniffer (PHPCS) written in nodejs.

CLI Usage

Installation :

npm install php-sniper -g

Command line :

php-sniper -o txt ./**/*.php

Note : If the working directory contains a ̀.php-sniper.json file, the cli will try to load it and use it as a ruleset configuration file

Integrate the library into your project

The API is open, so you can use this library in your own project, without needing to run it from a CLI process for example.

npm install php-sniper --save

And from your code :

var fs = require('fs');
var Sniper = require('php-sniper');
var scan = new Sniper({
  ruleset: 'phpcs' // defines a default ruleset (see src/rulesets/*.json)
});
var file = __dirname + '/foo.php';
var ast = scan.parseFile(file, fs.readFileSync(file, 'utf8'));
console.log(scan.report);

Writing your own Rule

Writing a rule is quite simple, this library is specially designed for that :

  • Register a rule from the instance
  • CSS syntax based node/token filtering
  • Session for storing data/criterias between files
  • Namespace resolution helper
  • Automatic multiple passes optimisation (with parsing cache)
  • PHP-Parser decorator (a drop-in replacement)

Here a sample code :

var Sniper = require('php-sniper');
var scan = new Sniper();

scan.setRule('domain.category.WarnDeprecated', function() {
  // first pass : extract deprecated
  this.on('ast: doc[isDoc,lines~@deprecated]', function(node) {
    var next = this.nextNode();
    if (next && (next.kind === 'function' || next.kind === 'method')) {
      this.sessionSet(
        'deprecated:' + this.getFQN(next), true
      );
    }
  });
  // second pass : warn them
  .after('ast: call > identifier', function(node) {
    var name = this.getFQN(node);
    if (this.sessionGet('deprecated:' + name)) {
      this.warningMessage('Deprecated "'+name+'"');
    }
  });
});

scan.setRule('domain.category.MyOwnRule', function() {
  this.customProperty = 10;
  // can make the parsing dependent on other modules passes
  this.after(
    ['domain.category.WarnDeprecated'],
    // or ['domain.category.WarnDeprecated:1'] saying that will run after it first pass
    'ast: call > identifier[name=DoNotCall]', function(node) {
    this.warningMessage('Should not call this function !');
  });
  // note : if WarnDeprecated is disabled, this pass will not be
  // triggered, and a warning into the '*' filename will be added
});


// loads a customized ruleset
scan.useRuleSet('./my-custom-phpcs.json');

// ... write here the parsing of your files ...
var filename = __dirname + '/foo.php';

// php-parser drop-in replacement
var ast = scan.getParser().getCode(
  fs.readFileSync(filename, 'utf8'), filename
);

// important call in order to say to the system the first pass is finished
// so other passes will automatically run after the first one
scan.finished();

Configure a customized ruleset

Rulesets are similar to PHPCS, but json based instead of xml based. A ruleset is just a way to define a list of rules, with options if needed.

A ruleset can inherit configuration from a list of parent rulesets, and the rules property will overwrite their configuration.

An example of ruleset configuration my-custom-phpcs.json

{
  "description": "A sample configuration file.",
  "includes": ["phpcs"],
  "rules": {
    "zend": {
      "files": {
        "ClosingTag": false
      }
    },
    "domain": {
      "category": {
        "WarnDeprecated": true,
        "MyOwnRule": {
          "customProperty": 5
        }
      }
    }
  }
}

Read further from the API documentation.

Roadmap

  • 0.1.x : First alpha prototype (soon)
    • run some rules
    • implement the selector
    • pass some tests
    • benchmarks & early optimisations
  • 1.0.x : Release a stable version of API
    • framework of automated tests
    • clean documentation
    • all API classes implemented
  • 1.1.0 : handle all PHPCS options
  • 1.2.0 : Integrate all generic rules
  • 1.3.0 : Integrate all PHPCS rules
  • 1.4.0 : Integrate all PEAR rules
  • 1.5.0 : Integrate all PSR1/2 rules
  • 1.6.0 : Integrate all Squiz rules
  • 1.7.0 : Integrate all Zend rules
  • 1.8.0 : Integrate all Squiz rules

Contributions

Any contribution is welcomed; after the alpha release, I'll be able to merge any PR about a rule definition.

Misc

This library is released under BSD-3 license clause.


This Library is Under Development - no BETA release available