In the current publicly released version of ESLint this approach is correct
(Although it can be optimized. Copyrights are usually located in the begging of
the file). However, a change was merged just earlier today that allows you to
subscribe to comment nodes, both block and line types are supported. You could
rewrite it to use that and then subscribe to “Program:after” to verify that
copyright was not found.
Thanks,
Ilya Volodin
From: [email protected] [mailto:[email protected]] On Behalf Of
Peter deHaan
Sent: Saturday, January 18, 2014 9:02 PM
To: [email protected]
Subject: [ESLint] Checking for copyright headers using ESLint
Kind of an odd question, but I have a simple Grunt task which does this and
figured it'd be an interesting thing to try w/ ESLint.
Note, this seems to work fine, this is just more of a sanity check or giving
people to give me a "this is lame, you should never do it this way..."
opportunity.
Can somebody take a quick look at this and tell me if I'm using ESLint
correctly or if there is an alternate/better way of trying something like this.
my .eslintrc file has the following rules:
{
"env": {
"node": true
},
"rules": {
"no-copyright": [1, "This Source Code Form is subject to the terms of
the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
with this file, You can obtain one at http://mozilla.org/MPL/2.0/."]
}
}
And my custom "no-copyright" rule looks like this:
// Found a comment.
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
Does this even work?
Or handle things?
*/
function cleanComment(node) {
"use strict";
var comment = node.value;
if (node.type === "Block") {
// Remove any leading whitespace or astrisks (*) from multi-line
comments.
comment = comment.replace(/^\s*\*\s?/gm, "");
}
// Remove any leading/trailing whitespace and collapse consecutive
whitespaces.
return comment.replace(/\s+/g, " ").trim();
}
module.exports = function (context) {
"use strict";
var copyright = context.options[0];
var hasCopyright = function (comment) {
return comment.indexOf(copyright) > -1;
};
return {
"Program": function (node) {
var comments = context.getAllComments().map(cleanComment);
if (!comments.some(hasCopyright)) {
context.report(node, "Unable to find copyright.");
}
}
};
};
--
You received this message because you are subscribed to the Google Groups
"ESLint" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups
"ESLint" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.