forked from OfficeDev/teams-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eslint-local-rules.js
74 lines (67 loc) · 2.26 KB
/
eslint-local-rules.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* @fileoverview auto add jsdoc author
*/
"use strict";
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const childProcess = require("child_process");
module.exports = {
"jsdoc-author": {
meta: {
type: "layout",
fixable: "code",
docs: {
description: "save @author JSDoc according to git history",
recommended: false,
},
schema: [],
},
create: (context) =>
/\.ts$/.test(context.getFilename())
? {
Program: (node) => {
let command = `git log --reverse --format="%aN" -1 ${context.getFilename()}`;
let author = (0, childProcess.execSync)(command, {
encoding: "utf8",
});
command = `git log --reverse --format="%aE" -1 ${context.getFilename()}`;
let email = (0, childProcess.execSync)(command, {
encoding: "utf8",
});
if (author === "") {
command = "git config user.name";
author = (0, childProcess.execSync)(command, {
encoding: "utf8",
});
}
if (email === "") {
command = "git config user.email";
email = (0, childProcess.execSync)(command, {
encoding: "utf8",
});
}
const authorJSDoc = `/**\n * @author ${author.trim()} <${email.trim()}>\n */\n`;
console.log(authorJSDoc);
const headerComments = context
.getSourceCode()
.getCommentsBefore(node);
if (
headerComments.length === 0 ||
headerComments.every(
(comment) => !comment.value.includes("@author")
)
) {
context.report({
node: node,
message: "no @author header found",
fix: (fixer) => fixer.insertTextBefore(node, authorJSDoc),
});
}
},
}
: {},
},
};