noInternalModuleRule.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  4. function __() { this.constructor = d; }
  5. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  6. };
  7. var ts = require("typescript");
  8. var Lint = require("../lint");
  9. var Rule = (function (_super) {
  10. __extends(Rule, _super);
  11. function Rule() {
  12. _super.apply(this, arguments);
  13. }
  14. Rule.prototype.apply = function (sourceFile) {
  15. return this.applyWithWalker(new NoInternalModuleWalker(sourceFile, this.getOptions()));
  16. };
  17. Rule.FAILURE_STRING = "forbidden internal module";
  18. return Rule;
  19. }(Lint.Rules.AbstractRule));
  20. exports.Rule = Rule;
  21. var NoInternalModuleWalker = (function (_super) {
  22. __extends(NoInternalModuleWalker, _super);
  23. function NoInternalModuleWalker() {
  24. _super.apply(this, arguments);
  25. }
  26. NoInternalModuleWalker.prototype.visitModuleDeclaration = function (node) {
  27. if (this.isInternalModuleDeclaration(node)) {
  28. this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
  29. }
  30. _super.prototype.visitModuleDeclaration.call(this, node);
  31. };
  32. NoInternalModuleWalker.prototype.isInternalModuleDeclaration = function (node) {
  33. return !Lint.isNodeFlagSet(node, ts.NodeFlags.Namespace)
  34. && !isNestedDeclaration(node)
  35. && node.name.kind === ts.SyntaxKind.Identifier;
  36. };
  37. return NoInternalModuleWalker;
  38. }(Lint.RuleWalker));
  39. function isNestedDeclaration(node) {
  40. return node.name.pos === node.pos;
  41. }