Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3533

"import" should be used to include external code

    XMLWordPrintable

    Details

    • Message:
      Use a standard "import" statement instead of "xxx".
    • Highlighting:
      Hide

      define(...), require(...)

      Show
      define(...) , require(...)
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Covered Languages:
      JavaScript, TypeScript
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • TSLint:
      no-reference, no-require-imports, no-var-requires

      Description

      Before ECMAScript 2015, module management had to be ad-hoc or provided by 3rd-party libraries such as Node.js, Webpack, or RequireJS. Fortunately, ES2015, provides language-standard mechanisms for module management, import and export, and older usages should be converted.

      Noncompliant Code Example

      // circle.js
      exports.area = function (r) {
        return PI * r * r;
      };
      
      // foo.js
      define(["./cart", "./horse"], function(cart, horse) {  // Noncompliant
        // ...
      });
      
      // bar.js
      const circle = require('./circle.js');  // Noncompliant
      

      Compliant Solution

      // circle.js
      let area = function (r) {
        return PI * r * r;
      }
      export default area;
      
      // foo.js
      import cart from "./cart.js";
      import horse from "./horse.js";
      
      // bar.js
      import circle from "./circle.js"
      

        Attachments

        1.
        JavaScript RSPEC-5456 Language-Specification Active Unassigned

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            ann.campbell.2 Ann Campbell
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated: