Lint

Lint a file blob against a specified license header.

Usage

var lint = require( '@stdlib/_tools/lint/license-header' );

lint( blob, header )

Lints a file blob against a specified license header.

var str = [
    '// This file is licensed under Apache-2.0.',
    '',
    '"use strict";',
    '',
    'var x = 3.14;',
    ''
];
str = str.join( '\n' );

var header = '// This file is licensed under MIT.';

var err = lint( str, header );
// returns {...}

The blob argument may be either a string or Buffer.

var string2buffer = require( '@stdlib/buffer/from-string' );

var str = [
    '// This file is licensed under Apache-2.0.',
    '',
    '"use strict";',
    '',
    'var x = 3.14;',
    ''
];
str = str.join( '\n' );

var header = '// This file is licensed under MIT.';

var err = lint( string2buffer( str ), header );
// returns {...}

The header argument may be either a string or regular expression.

var str = [
    '// This file is licensed under Apache-2.0.',
    '',
    '"use strict";',
    '',
    'var x = 3.14;',
    ''
];
str = str.join( '\n' );

var header = /^(?:\/\/ |# |\/\* |)This file is licensed under Apache-2\.0\.(?: \*\/|)/;

var err = lint( str, header );
// returns null

Notes

  • If the provided file blob contains a valid license header, the linter returns null.
  • The linter considers the presence of more than one license header in a file blob a lint error.
  • License headers are expected to appear within the first 5 lines of a file. If a license header begins elsewhere in a provided file blob, the linter considers this a lint error.

Examples

var headerRegExp = require( '@stdlib/_tools/licenses/header-regexp' );
var readFile = require( '@stdlib/fs/read-file' ).sync;
var lint = require( '@stdlib/_tools/lint/license-header' );

// Generate a header RegExp:
var header = headerRegExp( 'Apache-2.0', 'js' );

// Read the current file:
var fopts = {
    'encoding': 'utf8'
};
var file = readFile( __filename, fopts );

// Lint the file license header:
var err = lint( file, header );
if ( err ) {
    console.error( err.error );
} else {
    console.log( 'No lint errors.' );
}

CLI

Usage

Usage: lint-license-header [options] [--header <header>] [<string>]

Options:

  -h, --help             Print this message.
  -V, --version          Print the package version.
      --header header    License header.

Notes

  • Lint errors are printed to stderr.

  • If the header is a regular expression, ensure that the option value is properly escaped.

    # Not escaped...
    $ <stdout> | lint-license-header --header /\/\/ @license Apache-2\.0/
    
    # Escaped...
    $ <stdout> | lint-license-header --header /\\\\/\\\\/ @license Apache-2\\.0/
    

Examples

$ lint-license-header --header '// @license Apache-2.0' '"use strict";\n\nvar x = 3.14;\n'

To use as part of a standard stream pipeline,

$ cat ./README.md | lint-license-header --header '<!-- This file is licensed under Apache-2.0. -->'