noDefaultExportRule.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2016 Palantir Technologies, Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. var tslib_1 = require("tslib");
  20. var ts = require("typescript");
  21. var Lint = require("../index");
  22. var Rule = /** @class */ (function (_super) {
  23. tslib_1.__extends(Rule, _super);
  24. function Rule() {
  25. return _super !== null && _super.apply(this, arguments) || this;
  26. }
  27. Rule.prototype.apply = function (sourceFile) {
  28. return this.applyWithFunction(sourceFile, walk);
  29. };
  30. /* tslint:disable:object-literal-sort-keys */
  31. Rule.metadata = {
  32. ruleName: "no-default-export",
  33. description: "Disallows default exports in ES6-style modules.",
  34. descriptionDetails: "Use named exports instead.",
  35. rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Named imports/exports [promote clarity](https://github.com/palantir/tslint/issues/1182#issue-151780453).\n In addition, current tooling differs on the correct way to handle default imports/exports.\n Avoiding them all together can help avoid tooling bugs and conflicts."], ["\n Named imports/exports [promote clarity](https://github.com/palantir/tslint/issues/1182#issue-151780453).\n In addition, current tooling differs on the correct way to handle default imports/exports.\n Avoiding them all together can help avoid tooling bugs and conflicts."]))),
  36. optionsDescription: "Not configurable.",
  37. options: null,
  38. optionExamples: [true],
  39. type: "maintainability",
  40. typescriptOnly: false,
  41. };
  42. /* tslint:enable:object-literal-sort-keys */
  43. Rule.FAILURE_STRING = "Use of default exports is forbidden";
  44. return Rule;
  45. }(Lint.Rules.AbstractRule));
  46. exports.Rule = Rule;
  47. function walk(ctx) {
  48. if (ctx.sourceFile.isDeclarationFile || !ts.isExternalModule(ctx.sourceFile)) {
  49. return;
  50. }
  51. for (var _i = 0, _a = ctx.sourceFile.statements; _i < _a.length; _i++) {
  52. var statement = _a[_i];
  53. if (statement.kind === ts.SyntaxKind.ExportAssignment) {
  54. if (!statement.isExportEquals) {
  55. ctx.addFailureAtNode(statement.getChildAt(1, ctx.sourceFile), Rule.FAILURE_STRING);
  56. }
  57. }
  58. else if (statement.modifiers !== undefined && statement.modifiers.length >= 2 &&
  59. statement.modifiers[0].kind === ts.SyntaxKind.ExportKeyword &&
  60. statement.modifiers[1].kind === ts.SyntaxKind.DefaultKeyword) {
  61. ctx.addFailureAtNode(statement.modifiers[1], Rule.FAILURE_STRING);
  62. }
  63. }
  64. }
  65. var templateObject_1;