dsheiko / jscodesniffer

⛔️ [DEPRECATED] Tool to ensure that your JavaScript code does not violate the specified coding standard (Idiomatic Style Manifesto or JQuery Core Style Guidelines)
http://dsheiko.github.io/jscodesniffer/
42 stars 8 forks source link

Gulp plugin #15

Closed jedrichards closed 10 years ago

jedrichards commented 10 years ago

Hey

I'm considering making a Gulp friendly plugin that processes file streams against this module. I've had a quick look around the code and the docs but its not immediately clear how to use this module strictly programmatically from another module. In pseudo code, something like the following:

var jscs = require('jscodesniffer');

var results = jscs(args);

Where results in the above example would be a reporter/formatter implementation agnostic object literal that could be used flexibly to transform the output in any way:

{
    path: "path/to/file/under/test.js",
    pass: false,
    errors: [
        {
            line: 47,
            column: 13,
            message: "Fail message text"
        },
        { ... },
        { ... }
    ]
}

I don't mean the above code examples as a prescriptive suggestion, I'm just trying to illustrate the way I'd need to use the module programmatically. From what I can see the module is quite structured around the notion of using it as a cli tool rather than a node module.

Is this above use case supported do you think?

dsheiko commented 10 years ago

You can go with high-level interface with the report in stdout:

var argv = [ "node", "jscs", "./source-dir/", "--standard=Jquery", "--report-full" ],
        jscodesniffer = require( "jscodesniffer" );

jscodesniffer( argv, process.cwd() );

Or with low-level one:

var Sniffer = require( "./lib/Sniffer" ),
        sniffer = new Sniffer(),
        src = "var a= 1;",
        options = {
            standard: "Jquery"
        },
        logger = sniffer.getTestResults( src, options, {} );

console.log(logger.getMessages());

/*
  [ { sniff: 'OperatorSpacing',
    errorCode: 'OperatorPrecedingWhitespaces',
    range: [ 5, 5 ],
    loc: {  start: { line: 1, column: 5 }, end: { line: 1, column: 5 }  },
    payload:
     { actual: 0,
       expected: 1,
       excerpt: '',
       trace: '..a=..',
       where: '<' } } ]

*/
jedrichards commented 10 years ago

Great that's exactly what I needed, thank you. FYI the Gulp plugin is coming along well. It isn't published to npm yet but here's a heads up:

https://github.com/jedrichards/gulp-jscodesniffer