useStrictRule.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. var useStrictWalker = new UseStrictWalker(sourceFile, this.getOptions());
  16. return this.applyWithWalker(useStrictWalker);
  17. };
  18. Rule.FAILURE_STRING = "missing 'use strict'";
  19. return Rule;
  20. }(Lint.Rules.AbstractRule));
  21. exports.Rule = Rule;
  22. var UseStrictWalker = (function (_super) {
  23. __extends(UseStrictWalker, _super);
  24. function UseStrictWalker() {
  25. _super.apply(this, arguments);
  26. }
  27. UseStrictWalker.prototype.createScope = function () {
  28. return {};
  29. };
  30. UseStrictWalker.prototype.visitModuleDeclaration = function (node) {
  31. if (!Lint.hasModifier(node.modifiers, ts.SyntaxKind.DeclareKeyword)
  32. && this.hasOption(UseStrictWalker.OPTION_CHECK_MODULE)
  33. && node.body != null
  34. && node.body.kind === ts.SyntaxKind.ModuleBlock) {
  35. var firstModuleDeclaration = getFirstInModuleDeclarationsChain(node);
  36. var hasOnlyModuleDeclarationParents = firstModuleDeclaration.parent.kind === ts.SyntaxKind.SourceFile;
  37. if (hasOnlyModuleDeclarationParents) {
  38. this.handleBlock(firstModuleDeclaration, node.body);
  39. }
  40. }
  41. _super.prototype.visitModuleDeclaration.call(this, node);
  42. };
  43. UseStrictWalker.prototype.visitFunctionDeclaration = function (node) {
  44. if (this.getCurrentDepth() === 2 &&
  45. this.hasOption(UseStrictWalker.OPTION_CHECK_FUNCTION) &&
  46. node.body != null) {
  47. this.handleBlock(node, node.body);
  48. }
  49. _super.prototype.visitFunctionDeclaration.call(this, node);
  50. };
  51. UseStrictWalker.prototype.handleBlock = function (node, block) {
  52. var isFailure = true;
  53. if (block.statements != null && block.statements.length > 0) {
  54. var firstStatement = block.statements[0];
  55. if (firstStatement.kind === ts.SyntaxKind.ExpressionStatement) {
  56. var firstChild = firstStatement.getChildAt(0);
  57. if (firstChild.kind === ts.SyntaxKind.StringLiteral
  58. && firstChild.text === UseStrictWalker.USE_STRICT_STRING) {
  59. isFailure = false;
  60. }
  61. }
  62. }
  63. if (isFailure) {
  64. this.addFailure(this.createFailure(node.getStart(), node.getFirstToken().getWidth(), Rule.FAILURE_STRING));
  65. }
  66. };
  67. UseStrictWalker.OPTION_CHECK_FUNCTION = "check-function";
  68. UseStrictWalker.OPTION_CHECK_MODULE = "check-module";
  69. UseStrictWalker.USE_STRICT_STRING = "use strict";
  70. return UseStrictWalker;
  71. }(Lint.ScopeAwareRuleWalker));
  72. function getFirstInModuleDeclarationsChain(node) {
  73. var current = node;
  74. while (current.parent.kind === ts.SyntaxKind.ModuleDeclaration) {
  75. current = current.parent;
  76. }
  77. return current;
  78. }