musictheory / NilScript

Objective-C-style language superset of JavaScript with a tiny, simple runtime
Other
50 stars 5 forks source link

Add API to modify/lint generated code #79

Closed iccir closed 8 years ago

iccir commented 8 years ago

78 removes JSHint integration, but some clients may still wish to use it (or another linter, like ESLint, #40). Now that we generate results on a per-file basis and do incremental compilers, having official API to lint/transform source would be nice:

let ojOptions = …;
let hinterOptions = …;

ojOptions.hook = function(file, callback) {
    let contents = file.getContents();

    let hinter = new TheExternalHinter();
    hinter.hint(contents, hinterOptions, (err, hints) => {
        _.each(hints, hint => {
            file.addWarning(hint.line, hint.reason);
        });

        callback(err);
    });
};

compiler.compile(…, ojOptions, …);

The file argument would have setContents, getContents, addWarning as official API. setContents would be used by a transformer such as Babel. Note: line numbers MUST be preserved.

iccir commented 8 years ago

I'm not yet sure about the hook name, the others seem fine though

iccir commented 8 years ago

The hook function is only called when the generated JavaScript of a file changes.

iccir commented 8 years ago

hook is now on-compile. Here is a working example for ESLint:

let ojcOptions = …;
let linterOptions = …;

ojcOptions["on-compile"] = function(file, callback) {
    if (!linter) linter = require("eslint").linter;

    _.each(linter.verify(file.getContents(), linterOptions), warning => {
        file.addWarning(warning.line, warning.message);
    });

    callback();
};

compiler.compile(ojcOptions, callback);
iccir commented 8 years ago

Documentation added, on branch