Repository: nzakas/eslint-plugin-typescript
Pull request #6 (2016-10-04 04:45:21)

New: Add rule no-triple-slash-reference
Rule based off of TSLint's [no-reference rule](http://palantir.github.io/tslint/rules/no-reference/)


Patch:
diff --git a/README.md b/README.md
index 2132ea2..bd81fa5 100644
--- a/README.md
+++ b/README.md
@@ -51,3 +51,4 @@ Then configure the rules you want to use under the rules section.
 
 * `typescript/type-annotation-spacing` - enforces one space after the colon and zero spaces before the colon of a type annotation.
 * `typescript/explicit-member-accessibility` - enforces accessibility modifiers on class properties and methods.
+* `typescript/no-triple-slash-reference` - enforces `/// <reference />` is not used.
diff --git a/docs/rules/no-triple-slash-reference.md b/docs/rules/no-triple-slash-reference.md
new file mode 100644
index 0000000..d5a106b
--- /dev/null
+++ b/docs/rules/no-triple-slash-reference.md
@@ -0,0 +1,41 @@
+# Enforces reference comments are not used (no-triple-slash-reference)
+
+Triple-slash reference directive comments should not be used anymore. Use `import` instead.
+
+Before TypeScript adopted ES6 Module syntax,
+triple-slash reference directives were used to specify dependencies.
+Now that we have `import`, triple-slash reference directives are discouraged for specifying dependencies
+in favor of `import`.
+
+A triple-slash reference directive is a comment beginning with three slashes followed by a path to the module being imported:
+`/// <reference path="./Animal" />`.
+ES6 Modules handle this now:
+`import animal from "./Animal"`
+
+## Rule Details
+
+Does not allow the use of `/// <reference />` comments.
+
+The following patterns are considered warnings:
+
+```ts
+/// <reference path="Animal">
+```
+
+The following patterns are not warnings:
+
+```ts
+import Animal from "Animal"
+```
+
+## When Not To Use It
+
+If you use `/// <reference />` style imports.
+
+## Further Reading
+
+* TypeScript [Triple-Slash Directives](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html)
+
+## Compatibility
+
+* TSLint: [no-reference](http://palantir.github.io/tslint/rules/no-reference/)
diff --git a/lib/rules/no-triple-slash-reference.js b/lib/rules/no-triple-slash-reference.js
new file mode 100644
index 0000000..70b5b5c
--- /dev/null
+++ b/lib/rules/no-triple-slash-reference.js
@@ -0,0 +1,56 @@
+/**
+ * @fileoverview Enforces triple slash references are not used.
+ * @author Danny Fritz
+ */
+"use strict";
+
+//------------------------------------------------------------------------------
+// Rule Definition
+//------------------------------------------------------------------------------
+
+module.exports = {
+    meta: {
+        docs: {
+            description: "Enforces triple slash references are not used.",
+            category: "TypeScript"
+        },
+        schema: []
+    },
+
+    create: function(context) {
+        var referenceRegExp = /^\/\s*<reference/;
+        var sourceCode = context.getSourceCode();
+
+        //----------------------------------------------------------------------
+        // Helpers
+        //----------------------------------------------------------------------
+
+        /**
+         * Checks if property has an accessibility modifier.
+         * @param {ASTNode} program The node representing a Program.
+         * @returns {void}
+         * @private
+         */
+        function checkTripleSlashReference(program) {
+            var leading = sourceCode.getComments(program).leading;
+            leading.forEach(function(comment) {
+                if (comment.type !== "Line") {
+                    return;
+                }
+                if (referenceRegExp.test(comment.value)) {
+                    context.report({
+                        node: comment,
+                        message: "Do not use a triple slash reference"
+                    });
+                }
+            });
+        }
+
+        //----------------------------------------------------------------------
+        // Public
+        //----------------------------------------------------------------------
+        return {
+            Program: checkTripleSlashReference
+        };
+    }
+};


Test patch:
diff --git a/tests/lib/rules/no-triple-slash-reference.js b/tests/lib/rules/no-triple-slash-reference.js
new file mode 100644
index 0000000..0479a8a
--- /dev/null
+++ b/tests/lib/rules/no-triple-slash-reference.js
@@ -0,0 +1,43 @@
+/**
+ * @fileoverview Enforces triple slash references are not used
+ * @author Danny Fritz
+ */
+"use strict";
+
+//------------------------------------------------------------------------------
+// Requirements
+//------------------------------------------------------------------------------
+
+var rule = require("../../../lib/rules/no-triple-slash-reference"),
+    RuleTester = require("eslint").RuleTester;
+
+
+//------------------------------------------------------------------------------
+// Tests
+//------------------------------------------------------------------------------
+
+var ruleTester = new RuleTester();
+ruleTester.run("no-triple-slash-reference", rule, {
+
+    valid: [
+        {
+            code: "/// Non-reference triple-slash comment",
+            parser: "typescript-eslint-parser"
+        },
+        {
+            code: "// <reference path=\"Animal\" />",
+            parser: "typescript-eslint-parser"
+        }
+    ],
+    invalid: [
+        {
+            code: "/// <reference path=\"Animal\" />",
+            parser: "typescript-eslint-parser",
+            errors: [{
+                message: "Do not use a triple slash reference",
+                line: 1,
+                column: 1
+            }]
+        }
+    ]
+});