Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of eslint plugin #190

Merged
merged 4 commits into from
Nov 3, 2019
Merged

Conversation

bafolts
Copy link
Contributor

@bafolts bafolts commented Oct 25, 2019

This pull request adds an eslint plugin which validates tsdoc comment blocks. This should resolve #154

@msftclas
Copy link

msftclas commented Oct 25, 2019

CLA assistant check
All CLA requirements met.

@@ -0,0 +1,2 @@
registry=https://registry.npmjs.org/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need this file.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so we should also delete the copy under /tsdoc/.npmrc.

I think you're right though -- this probably predates the ADO publishing pipeline.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably actually predates this repo's migration to Rush. Let's clean these up in a follow-up PR.

"private": true,
"license": "MIT",
"scripts": {
"build": "",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You definitely want a CI build script here.

"name": "eslint-plugin-tsdoc",
"version": "0.1.0",
"description": "eslint plugin for tsdoc",
"private": true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We expect to publish this plugin, no?

@@ -0,0 +1,44 @@

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be written in TypeScript.

@@ -0,0 +1,14 @@

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our convention is to put tests under src/<relevant path>/tests/<featurename>.test.ts.

This should also be written in TypeScript.

var commentString = "/*" + commentBlock.value + "*/";
var results = tsDocParser.parseString(commentString).log;
if (results._messages.length > 0) {
results._messages.forEach(function (message) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be more efficient to use a for(... of ...) loop here.

var commentBlock = commentBlocks[0];
var commentString = "/*" + commentBlock.value + "*/";
var results = tsDocParser.parseString(commentString).log;
if (results._messages.length > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You actually don't need this check. If there are no entries in the _messages property, the loop won't iterate.

var commentString = "/*" + commentBlock.value + "*/";
var results = tsDocParser.parseString(commentString).log;
if (results._messages.length > 0) {
results._messages.forEach(function (message) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Properties that begin with underscores (like _messages) are private. Use a public property here.

var results = tsDocParser.parseString(commentString).log;
if (results._messages.length > 0) {
results._messages.forEach(function (message) {
context.report({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will report every TSDoc parse error as a separate linting violation, won't it? Is that what we want? Or do we want to report a single violation that says that the doc comment is invalid, and list the parse errors as additional information on that single violation?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we do want separate lint errors for each TSDoc error. This way the red squiggly underlines can be as accurate as possible.

module.exports = {
rules: {
"tsdoc-comments": {
meta: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bafolts
Copy link
Contributor Author

bafolts commented Oct 29, 2019

I will make the suggested changes here and comment back when I am done. Thanks for the feedback.

@bafolts
Copy link
Contributor Author

bafolts commented Oct 31, 2019

I believe I have made all the suggested improvements. Let me know if there are any more.

"private": false,
"license": "MIT",
"scripts": {
"build": "tsc",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want to run eslint on this package as well. Maybe copy parts of the build script from tsdoc.

});

module.exports = {
rules: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use two-space indentation?

messageIds[messageId] = messageId + ": {{ unformattedText }}";
});

module.exports = {
Copy link
Member

@iclanton iclanton Oct 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should just be able to say exports = ....

Suggested change
module.exports = {
exports = {

const messageIds: {[x: string]: string} = {};

allTsdocMessageIds.forEach((messageId: string) => {
messageIds[messageId] = messageId + ": {{ unformattedText }}";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use a template string here:

Suggested change
messageIds[messageId] = messageId + ": {{ unformattedText }}";
messageIds[messageId] = `${messageId}: {{ unformattedText }}`;

url: "https://github.com/microsoft/tsdoc"
}
},
create: function(context: eslint.Rule.RuleContext) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a lambda function here.

Suggested change
create: function(context: eslint.Rule.RuleContext) {
create: (context: eslint.Rule.RuleContext) => {

var commentBlock = commentBlocks[0];
var commentString = "/*" + commentBlock.value + "*/";
var results = tsDocParser.parseString(commentString).log;
for (let message of results.messages) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (let message of results.messages) {
for (const message of results.messages) {

@@ -0,0 +1,26 @@

import { RuleTester } from "eslint";
const rule = require("../index").rules["tsdoc-comments"];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const rule = require("../index").rules["tsdoc-comments"];
import * as rules from '../index';
const rule: eslint.Rule /* or whatever the type is here */ = rules['tsdoc-comments'];

"license": "MIT",
"scripts": {
"build": "tsc",
"test": "node tests/index.js"
Copy link
Member

@iclanton iclanton Oct 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This path should now be lib/tests/index.js.

Although it'd probably be best to include this in the build script so we run the test as part of the build.

Suggested change
"test": "node tests/index.js"
"test": "node lib/tests/index.js"

"@microsoft/tsdoc": "0.12.14"
},
"devDependencies": {
"@types/eslint": "^6.1.3",
Copy link
Member

@iclanton iclanton Oct 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@types/ packages don't follow semver. Pin this to the specific version.

Suggested change
"@types/eslint": "^6.1.3",
"@types/eslint": "6.1.3",

messageIds[messageId] = messageId + ": {{ unformattedText }}";
});

module.exports = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be useful to ensure that the object we export is well-typed.

@iclanton
Copy link
Member

iclanton commented Nov 1, 2019

Thanks so much for putting this together! This rule will be really useful.

Most of the remaining comments are fairly superficial, and they can probably be addressed in a follow-up PR if you like.

@bafolts bafolts force-pushed the eslint-plugin branch 3 times, most recently from 1aaf298 to 3ac9f2f Compare November 2, 2019 23:34
@bafolts
Copy link
Contributor Author

bafolts commented Nov 3, 2019

I have made all the requested changes. It appears something with the build is failing in the playground directory. If I update the @types/react-dom version to 16.9.3 locally I can fix the problem, but for the life of me I can't get rushstack to build with the version I specify in playground/package.json.

@octogonz
Copy link
Collaborator

octogonz commented Nov 3, 2019

It's this old issue. The TSDoc repo was serving as a guinea pig for the Yarn package manager, but Yarn really doesn't play well with DefinitelyTyped. There's an active megathread where they're finally trying to sort this out. @iclanton I wonder if we should just switch to PNPM for the time being?

I've pushed a fix to your branch.

@@ -0,0 +1,2 @@
registry=https://registry.npmjs.org/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably actually predates this repo's migration to Rush. Let's clean these up in a follow-up PR.

@iclanton iclanton merged commit 3f3dc73 into microsoft:master Nov 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TSLint rules to enforce TypeDoc?
4 participants