forked from gajus/eslint-plugin-flowtype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typeImportStyle.js
56 lines (53 loc) · 1.49 KB
/
typeImportStyle.js
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
const schema = [
{
enum: ['declaration', 'identifier'],
type: 'string'
}
];
const create = (context) => {
if (context.options[0] === 'declaration') {
return {
ImportDeclaration (node) {
if (node.importKind !== 'type') {
node.specifiers.forEach((specifier) => {
if (specifier.importKind === 'type') {
context.report({
message: 'Unexpected type import',
node
});
}
});
}
}
};
} else {
// Default to 'identifier'
return {
ImportDeclaration (node) {
if (node.importKind === 'type') {
context.report({
fix (fixer) {
const imports = node.specifiers.map((specifier) => {
if (specifier.type === 'ImportDefaultSpecifier') {
return 'type default as ' + specifier.local.name;
} else if (specifier.imported.name === specifier.local.name) {
return 'type ' + specifier.local.name;
} else {
return 'type ' + specifier.imported.name + ' as ' + specifier.local.name;
}
});
const source = node.source.value;
return fixer.replaceText(node, 'import {' + imports.join(', ') + '} from \'' + source + '\';');
},
message: 'Unexpected "import type"',
node
});
}
}
};
}
};
export default {
create,
schema
};