Skip to content

Commit

Permalink
WIP: Reimplement using new comment-parser, and add basic never su…
Browse files Browse the repository at this point in the history
…pport; begin schema for options but options object not yet supported
  • Loading branch information
brettz9 committed Jan 29, 2021
1 parent 01db967 commit cd01789
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 24 deletions.
114 changes: 90 additions & 24 deletions src/rules/requireAsteriskPrefix.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,62 @@
import iterateJsdoc from '../iterateJsdoc';

const prefixMatch = /^(\s+)(?:\*( ?))?/u;
const validPrefix = /^\s+\*(?:\/?$| )/u;

export default iterateJsdoc(({
sourceCode,
jsdocNode,
report,
context,
jsdoc,
utils,
}) => {
const fix = (fixer) => {
const replacement = sourceCode.getText(jsdocNode).split('\n')
.map((line, index) => {
return index && !validPrefix.test(line) ? line.replace(prefixMatch, (_, $1, $2) => {
return `${$1}*${$2 || ' '}`;
}) : line;
})
.join('\n');

return fixer.replaceText(jsdocNode, replacement);
};

sourceCode.getText(jsdocNode).split('\n').some((line, index) => {
const lineNum = Number.parseInt(index, 10);
if (lineNum && !validPrefix.test(line)) {
report('Expected JSDoc block to have the prefix.', fix, {
line: lineNum,
});
// Todo: `tags` option
/*
const {
tags: tagMap,
} = context.options[1] || {};
*/

const {source} = jsdoc;

if (context.options[0] === 'never') {
source.some(({number, tokens}) => {
const {delimiter, tag} = tokens;
if (!tag) {
// Todo: Reinvestigate upon trying to clean-up non-tag description
return false;
}
if (delimiter) {
const fix = () => {
tokens.delimiter = '';
tokens.postDelimiter = '';
};

utils.reportJSDoc('Expected JSDoc block to have no prefix.', {
column: 0,
line: number,
}, fix);

return true;
}

return false;
});

return;
}

source.some(({number, tokens}) => {
const {delimiter, tag} = tokens;
if (!tag) {
// Todo: Reinvestigate upon trying to clean-up non-tag description
return false;
}
if (!delimiter) {
const fix = () => {
tokens.delimiter = '*';
tokens.postDelimiter = ' ';
};

utils.reportJSDoc('Expected JSDoc block to have the prefix.', {
column: 0,
line: number,
}, fix);

return true;
}
Expand All @@ -36,6 +67,41 @@ export default iterateJsdoc(({
iterateAllJsdocs: true,
meta: {
fixable: 'code',
schema: [
{
enum: ['always', 'never', 'any'],
type: 'string',
},
{
additionalProperties: false,
properties: {
tags: {
properties: {
always: {
items: {
type: 'string',
},
type: 'array',
},
any: {
items: {
type: 'string',
},
type: 'array',
},
never: {
items: {
type: 'string',
},
type: 'array',
},
},
type: 'object',
},
},
type: 'object',
},
],
type: 'layout',
},
});
50 changes: 50 additions & 0 deletions test/rules/assertions/requireAsteriskPrefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,56 @@ export default {
}
`,
},
{
code: `
/**
* @param {Number} foo
*/
function quux (foo) {
// with spaces
}
`,
errors: [
{
line: 4,
message: 'Expected JSDoc block to have no prefix.',
},
],
options: ['never'],
output: `
/**
@param {Number} foo
*/
function quux (foo) {
// with spaces
}
`,
},
{
code: `
/**
*@param {Number} foo
*/function quux (foo) {
// with spaces
}
`,
errors: [
{
line: 3,
message: 'Expected JSDoc block to have no prefix.',
},
],
options: ['never'],
output: `
/**
@param {Number} foo
*/function quux (foo) {
// with spaces
}
`,
},
],
valid: [
{
Expand Down

0 comments on commit cd01789

Please sign in to comment.