UNPKG

5.19 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3// See LICENSE in the project root for license information.
4const tsdoc_1 = require("@microsoft/tsdoc");
5const Debug_1 = require("./Debug");
6const ConfigCache_1 = require("./ConfigCache");
7const tsdocMessageIds = {};
8const defaultTSDocConfiguration = new tsdoc_1.TSDocConfiguration();
9defaultTSDocConfiguration.allTsdocMessageIds.forEach((messageId) => {
10 tsdocMessageIds[messageId] = `${messageId}: {{unformattedText}}`;
11});
12const plugin = {
13 rules: {
14 // NOTE: The actual ESLint rule name will be "tsdoc/syntax". It is calculated by deleting "eslint-plugin-"
15 // from the NPM package name, and then appending this string.
16 syntax: {
17 meta: {
18 messages: Object.assign({ 'error-loading-config-file': 'Error loading TSDoc config file:\n{{details}}', 'error-applying-config': 'Error applying TSDoc configuration: {{details}}' }, tsdocMessageIds),
19 type: 'problem',
20 docs: {
21 description: 'Validates that TypeScript documentation comments conform to the TSDoc standard',
22 category: 'Stylistic Issues',
23 // This package is experimental
24 recommended: false,
25 url: 'https://tsdoc.org/pages/packages/eslint-plugin-tsdoc'
26 }
27 },
28 create: (context) => {
29 const sourceFilePath = context.getFilename();
30 Debug_1.Debug.log(`Linting: "${sourceFilePath}"`);
31 const tsdocConfiguration = new tsdoc_1.TSDocConfiguration();
32 try {
33 const tsdocConfigFile = ConfigCache_1.ConfigCache.getForSourceFile(sourceFilePath);
34 if (!tsdocConfigFile.fileNotFound) {
35 if (tsdocConfigFile.hasErrors) {
36 context.report({
37 loc: { line: 1, column: 1 },
38 messageId: 'error-loading-config-file',
39 data: {
40 details: tsdocConfigFile.getErrorSummary()
41 }
42 });
43 }
44 try {
45 tsdocConfigFile.configureParser(tsdocConfiguration);
46 }
47 catch (e) {
48 context.report({
49 loc: { line: 1, column: 1 },
50 messageId: 'error-applying-config',
51 data: {
52 details: e.message
53 }
54 });
55 }
56 }
57 }
58 catch (e) {
59 context.report({
60 loc: { line: 1, column: 1 },
61 messageId: 'error-loading-config-file',
62 data: {
63 details: `Unexpected exception: ${e.message}`
64 }
65 });
66 }
67 const tsdocParser = new tsdoc_1.TSDocParser(tsdocConfiguration);
68 const sourceCode = context.getSourceCode();
69 const checkCommentBlocks = function (node) {
70 for (const comment of sourceCode.getAllComments()) {
71 if (comment.type !== 'Block') {
72 continue;
73 }
74 if (!comment.range) {
75 continue;
76 }
77 const textRange = tsdoc_1.TextRange.fromStringRange(sourceCode.text, comment.range[0], comment.range[1]);
78 // Smallest comment is "/***/"
79 if (textRange.length < 5) {
80 continue;
81 }
82 // Make sure it starts with "/**"
83 if (textRange.buffer[textRange.pos + 2] !== '*') {
84 continue;
85 }
86 const parserContext = tsdocParser.parseRange(textRange);
87 for (const message of parserContext.log.messages) {
88 context.report({
89 loc: {
90 start: sourceCode.getLocFromIndex(message.textRange.pos),
91 end: sourceCode.getLocFromIndex(message.textRange.end)
92 },
93 messageId: message.messageId,
94 data: {
95 unformattedText: message.unformattedText
96 }
97 });
98 }
99 }
100 };
101 return {
102 Program: checkCommentBlocks
103 };
104 }
105 }
106 }
107};
108module.exports = plugin;
109//# sourceMappingURL=index.js.map
\No newline at end of file