All files / src/nodes ImportDirective.js

100% Statements 21/21
100% Branches 14/14
100% Functions 2/2
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55        55x 55x   55x 55x   55x   148x     148x   30x 118x   53x 53x 182x         53x     10x 10x       43x 43x     53x               65x   148x       55x  
const {
  doc: {
    builders: { group, line, softline }
  }
} = require('prettier');
const semver = require('semver');
 
const printSeparatedList = require('./print-separated-list');
const { printString } = require('../prettier-comments/common/util');
 
const ImportDirective = {
  print: ({ node, options }) => {
    const importPath = printString(node.path, options);
    let doc;
 
    if (node.unitAlias) {
      // import "./Foo.sol" as Foo;
      doc = [importPath, ' as ', node.unitAlias];
    } else if (node.symbolAliases) {
      // import { Foo, Bar as Qux } from "./Foo.sol";
      const compiler = semver.coerce(options.compiler);
      const symbolAliases = node.symbolAliases.map(([a, b]) =>
        b ? `${a} as ${b}` : a
      );
      let firstSeparator;
      let separator;
 
      if (compiler && semver.satisfies(compiler, '>=0.7.4')) {
        // if the compiler exists and is greater than or equal to 0.7.4 we will
        // split the ImportDirective.
        firstSeparator = options.bracketSpacing ? line : softline;
        separator = [',', line];
      } else {
        // if the compiler is not given or is lower than 0.7.4 we will not
        // split the ImportDirective.
        firstSeparator = options.bracketSpacing ? ' ' : '';
        separator = ', ';
      }
 
      doc = [
        '{',
        printSeparatedList(symbolAliases, { firstSeparator, separator }),
        '} from ',
        importPath
      ];
    } else {
      // import "./Foo.sol";
      doc = importPath;
    }
    return group(['import ', doc, ';']);
  }
};
 
module.exports = ImportDirective;