Skip to content

Commit

Permalink
Modulify
Browse files Browse the repository at this point in the history
  • Loading branch information
skeate committed Sep 16, 2022
1 parent 4ebc91e commit c0e9fef
Show file tree
Hide file tree
Showing 5 changed files with 2,563 additions and 2,328 deletions.
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": false ,
"trailingComma": "all",
"singleQuote": true
}
72 changes: 35 additions & 37 deletions lib/rules/haiku-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
* @fileoverview Enforces haiku comments
* @author skeate
*/
'use strict';
import { syllable } from 'syllable'

const syllable = require('syllable');
const zip = (xs, ys) => xs.map((x, i) => [x, ys[i]])

const zip = (xs, ys) => xs.map((x, i) => [x, ys[i]]);

module.exports = {
export default {
meta: {
docs: {
description: 'Enforces haiku comments',
Expand All @@ -19,53 +17,53 @@ module.exports = {
{
type: 'object',
properties: {
prefix: {type: 'string'},
includeLineComments: {type: 'boolean'},
includeBlockComments: {type: 'boolean'},
prefix: { type: 'string' },
includeLineComments: { type: 'boolean' },
includeBlockComments: { type: 'boolean' },
},
additionalProperties: false,
},
],
},

create(context) {
const sourceCode = context.getSourceCode();
const sourceCode = context.getSourceCode()
const options = {
prefix: '',
includeLineComments: true,
includeBlockComments: true,
...(context.options.length > 0 ? context.options[0] : {}),
};
}

return {
Program() {
const allComments = sourceCode.getAllComments();
const normalizedCommentLines = [];
const allComments = sourceCode.getAllComments()
const normalizedCommentLines = []

if (options.includeLineComments) {
allComments.forEach(c => {
if (c.type !== 'Line') return;
const trimmed = c.value.trim();
const postprefix = trimmed.substr(options.prefix.length).trim();
allComments.forEach((c) => {
if (c.type !== 'Line') return
const trimmed = c.value.trim()
const postprefix = trimmed.substr(options.prefix.length).trim()
if (trimmed.startsWith(options.prefix) && postprefix.length > 0) {
normalizedCommentLines.push({
loc: c.loc,
value: postprefix,
});
})
}
});
})
}

if (options.includeBlockComments) {
allComments.forEach(c => {
if (c.type !== 'Block') return;
allComments.forEach((c) => {
if (c.type !== 'Block') return
c.value.split('\n').forEach((line, lineno) => {
const trimmed = line.trim().replace(/^\* */, '');
const postprefix = trimmed.substr(options.prefix.length).trim();
const trimmed = line.trim().replace(/^\* */, '')
const postprefix = trimmed.substr(options.prefix.length).trim()
if (trimmed.startsWith(options.prefix) && postprefix.length > 0) {
const relativeLineNo = c.loc.start.line + lineno;
const startCol = line.indexOf(trimmed);
const endCol = startCol + trimmed.length;
const relativeLineNo = c.loc.start.line + lineno
const startCol = line.indexOf(trimmed)
const endCol = startCol + trimmed.length
normalizedCommentLines.push({
loc: {
start: {
Expand All @@ -78,34 +76,34 @@ module.exports = {
},
},
value: postprefix,
});
})
}
});
});
})
})
}

for (let i = 0; i < normalizedCommentLines.length; i++) {
if (i + 2 >= normalizedCommentLines.length) {
context.report({
message: 'Not enough lines for haiku',
loc: normalizedCommentLines[i].loc,
});
i += 2;
})
i += 2
} else {
const lines = [
normalizedCommentLines[0],
normalizedCommentLines[1],
normalizedCommentLines[2],
];
]

if (lines[2].loc.start.line - lines[0].loc.start.line > 2) {
context.report({
message: 'Not enough lines for haiku',
loc: lines[0].loc,
});
})
} else {
i += 2; // plus the one from the loop
const syls = lines.map(l => syllable(l.value));
i += 2 // plus the one from the loop
const syls = lines.map((l) => syllable(l.value))
if (syls[0] !== 5 || syls[1] !== 7 || syls[2] !== 5) {
context.report({
message: `Comment not in haiku:\n${zip(lines, syls)
Expand All @@ -115,12 +113,12 @@ module.exports = {
start: lines[0].loc.start,
end: lines[2].loc.end,
},
});
})
}
}
}
}
},
};
}
},
};
}
Loading

0 comments on commit c0e9fef

Please sign in to comment.