Skip to content

⬅️ Back to Table of Contents

πŸ“„ ban-tslint-comment

πŸ“Š Analysis Summary

Metric Count
πŸ”§ Functions 2
πŸ“¦ Imports 2
πŸ“Š Variables & Constants 1

πŸ“š Table of Contents

πŸ› οΈ File Location:

πŸ“‚ packages/eslint-plugin/src/rules/ban-tslint-comment.ts

πŸ“€ Default Export

export default createRule({ ... })
Property Value
name 'ban-tslint-comment'
meta.type 'suggestion'
meta.docs.description 'Disallow // tslint:<rule-flag> comments'
meta.docs.recommended 'stylistic'
meta.fixable 'code'
meta.messages.commentDetected 'tslint comment detected: "{{ text }}"'
meta.schema []
defaultOptions []

Entry point: create β€” documented under Functions.


πŸ“¦ Imports

Name Source
AST_TOKEN_TYPES @typescript-eslint/utils
createRule ../util

Variables & Constants

Name Type Kind Value Exported
ENABLE_DISABLE_REGEX RegExp const /^\s*tslint:(enable\|disable)(?:-(line\|next-line))?(:\|\s\|$)/ βœ—

Functions

create(context: any): { Program(): void; }

Parameters:

  • context any

Returns: { Program(): void; }

Calls:

  • context.sourceCode.getAllComments
  • comments.forEach
  • ENABLE_DISABLE_REGEX.test
  • context.report
  • toText
  • context.sourceCode.getIndexFromLoc
  • fixer.removeRange
Code
context => {
    return {
      Program(): void {
        const comments = context.sourceCode.getAllComments();
        comments.forEach(c => {
          if (ENABLE_DISABLE_REGEX.test(c.value)) {
            context.report({
              node: c,
              messageId: 'commentDetected',
              data: { text: toText(c.value, c.type) },
              fix(fixer) {
                const rangeStart = context.sourceCode.getIndexFromLoc({
                  column: c.loc.start.column > 0 ? c.loc.start.column - 1 : 0,
                  line: c.loc.start.line,
                });
                const rangeEnd = context.sourceCode.getIndexFromLoc({
                  column: c.loc.end.column,
                  line: c.loc.end.line,
                });
                return fixer.removeRange([rangeStart, rangeEnd + 1]);
              },
            });
          }
        });
      },
    };
  }

toText(text: string, type: AST_TOKEN_TYPES.Block | AST_TOKEN_TYPES…): string

Parameters:

  • text string
  • type AST_TOKEN_TYPES.Block | AST_TOKEN_TYPES.Line

Returns: string

Code
(
  text: string,
  type: AST_TOKEN_TYPES.Block | AST_TOKEN_TYPES.Line,
): string =>
  type === AST_TOKEN_TYPES.Line
    ? ['//', text.trim()].join(' ')
    : ['/*', text.trim(), '*/'].join(' ')

Generated by Syntax Scribe