-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathImportDirective.js
More file actions
61 lines (56 loc) · 1.99 KB
/
ImportDirective.js
File metadata and controls
61 lines (56 loc) · 1.99 KB
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
56
57
58
59
60
61
import { doc } from 'prettier';
import coerce from 'semver/functions/coerce.js';
import satisfies from 'semver/functions/satisfies.js';
import { printSeparatedList } from '../common/printer-helpers.js';
import { printString } from '../common/util.js';
const { group, line, softline } = doc.builders;
export const ImportDirective = {
print: ({ node, options }) => {
const importPath = printString(node.path, options);
let document;
if (node.unitAlias) {
// First we look for '*' between the beginning of the import and the
// beginning of the importPath
document = options.originalText
.slice(options.locStart(node), options.locStart(node.pathLiteral))
.includes('*')
? // import * as Bar from "./Bar.sol";
['* as ', node.unitAlias, ' from ', importPath]
: // import "./Foo.sol" as Foo;
[importPath, ' as ', node.unitAlias];
} else if (node.symbolAliases) {
// import { Foo, Bar as Qux } from "./Foo.sol";
const compiler = coerce(options.compiler);
const symbolAliases = node.symbolAliases.map(([a, b]) =>
b ? `${a} as ${b}` : a
);
let firstSeparator;
let separator;
if (
compiler &&
satisfies(compiler, '>=0.7.4') &&
symbolAliases.length > 1
) {
// 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 = ', ';
}
document = [
'{',
printSeparatedList(symbolAliases, { firstSeparator, separator }),
'} from ',
importPath
];
} else {
// import "./Foo.sol";
document = importPath;
}
return group(['import ', document, ';']);
}
};