Skip to content

Commit

Permalink
Enable a custom comment block by adding options and
Browse files Browse the repository at this point in the history
  • Loading branch information
lyingdragon committed Jan 9, 2020
1 parent e0fc7c3 commit a6dea78
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
15 changes: 13 additions & 2 deletions index.js
Expand Up @@ -22,11 +22,20 @@ const { utils } = lib;
class Comments extends Emitter {
constructor(options = {}) {
super();
this.options = options;
this.options = Object.assign({commentStart: '/*'}, options);
this.comments = [];
this.parsers = {};
this.tokens = [];
this.ast = {};

console.log(this.options)

if (this.options.commentStart) {
this.options.commentStart = this.options.commentStart.replace(/([\*\/\(\)])/g, "\\$1")
}
if (this.options.commentEnd) {
this.options.commentEnd = this.options.commentEnd.replace(/([\*\/\(\)])/g, "\\$1")
}
}

/**
Expand All @@ -45,7 +54,9 @@ class Comments extends Emitter {
tokenize(input, options) {
let opts = Object.assign({}, this.options, options);
// this only needs to be roughly correct. the tokenizer is smarter
let isComment = str => /^(\s*\/\*|\*\s*@| {4,})/gm.test(str);
// let isComment = str => /^(\s*\/\*|\*\s*@| {4,})/gm.test(str);
var re = new RegExp("^(\\s*" + opts.commentStart + "|\\*\\s*@| {4,})", "gm");
let isComment = str => re.test(str);
if (opts.stripStars === void 0 && input && !isComment(input)) {
opts.stripStars = false;
}
Expand Down
13 changes: 7 additions & 6 deletions lib/utils.js
Expand Up @@ -78,8 +78,8 @@ exports.last = (arr = [], n = 1) => arr[arr.length - n];
* @return {Boolean}
*/

exports.isProtectedComment = str => /^\/\*{1,2}\s*!/.test(str);;

// exports.isProtectedComment = str => /^\/\*{1,2}\s*!/.test(str);;
exports.isProtectedComment = str => /^\(\*{1,2}\s*!/.test(str);;
/**
* Returns true if the given comment `str` is a config comment that contains
* `eslint`, `global`, `jshint`, `jslint` etc.
Expand All @@ -103,17 +103,18 @@ exports.isConfigComment = (str, names = '[ej]s[hl]int') => {
*/

exports.isValidBlockComment = (comment, options = {}) => {
let regex = options.allowSingleStar ? /^\s*\/\*/ : /^\s*\/\*\*/;
// let regex = options.allowSingleStar ? /^\s*\/\*/ : /^\s*\/\*\*/;
let regex = options.allowSingleStar ? new RegExp("/^\\s*" + options.commentStart + "/") : new RegExp("/^\\s*" + options.commentStart + "\\*/");
if (typeof comment === 'string') {
return regex.test(comment);
}
switch (comment.type && comment.type.toLowerCase()) {
case 'block':
case 'commentblock':
case 'blockcomment':
if (!options.allowSingleStar) {
return comment.raw[0] === '*';
}
// if (!options.allowSingleStar) {
// return comment.raw[0] === '*';
// }
return true;
default: {
return false;
Expand Down

0 comments on commit a6dea78

Please sign in to comment.