-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path_helpers.js
144 lines (131 loc) · 3.52 KB
/
_helpers.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict';
var resolve = require('resolve');
function StorageObject() {}
StorageObject.prototype = Object.create(null);
// A cache that resets itself after one tick.
function oneTickCache() {
var store = null;
function reset() { store = null; }
return function() {
if (store === null) {
store = new StorageObject();
process.nextTick(reset);
}
return store;
};
}
// The resolve cache is shared by all the rules, since the operation is very
// common and expensive.
var _resolveCache = oneTickCache();
function resolveSync(x, opts) {
var resolveCache = _resolveCache();
var cacheKey = JSON.stringify([x, opts]);
if (!(cacheKey in resolveCache)) {
try {
resolveCache[cacheKey] = resolve.sync(x, opts);
} catch(err) {
resolveCache[cacheKey] = null;
}
}
return resolveCache[cacheKey];
}
function isRequireCall(node) {
// require("…");
return (
node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
node.callee.name === 'require' &&
node.arguments[0] &&
node.arguments[0].type === 'Literal'
);
}
function isRequireResolveCall(node) {
// require.resolve("…");
return (
node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.computed === false &&
node.callee.object.type === 'Identifier' &&
node.callee.object.name === 'require' &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'resolve' &&
node.arguments[0] &&
node.arguments[0].type === 'Literal'
);
}
function isImport(node) {
// import … from "…";
// import "…";
return (
node.type === 'ImportDeclaration' &&
(node.importKind == null || node.importKind === 'value') &&
node.source &&
node.source.type === 'Literal'
);
}
function isImportType(node) {
// import type … from "…";
return (
node.type === 'ImportDeclaration' &&
node.importKind === 'type' &&
node.source &&
node.source.type === 'Literal'
);
}
function isExportFrom(node) {
// export * from "…";
// export … from "…";
return (
node.type === 'ExportAllDeclaration' ||
node.type === 'ExportNamedDeclaration'
) && (
(node.exportKind == null || node.exportKind === 'value') &&
node.source &&
node.source.type === 'Literal'
);
}
function isExportTypeFrom(node) {
// export type * from "…";
// export type … from "…";
return (
node.type === 'ExportAllDeclaration' ||
node.type === 'ExportNamedDeclaration'
) && (
node.exportKind === 'type' &&
node.source &&
node.source.type === 'Literal'
);
}
function getModuleId(node) {
if (isRequireCall(node) || isRequireResolveCall(node)) {
return node.arguments[0].value;
} else if (isImport(node) || isExportFrom(node) ||
isImportType(node) || isExportTypeFrom(node)) {
return node.source.value;
} else {
return null;
}
}
function getIdNode(node) {
if (isRequireCall(node) || isRequireResolveCall(node)) {
return node.arguments[0];
} else if (isImport(node) || isExportFrom(node) ||
isImportType(node) || isExportTypeFrom(node)) {
return node.source;
} else {
return null;
}
}
module.exports = {
StorageObject: StorageObject,
oneTickCache: oneTickCache,
resolveSync: resolveSync,
isRequireCall: isRequireCall,
isRequireResolveCall: isRequireResolveCall,
isImport: isImport,
isImportType: isImportType,
isExportFrom: isExportFrom,
isExportTypeFrom: isExportTypeFrom,
getIdNode: getIdNode,
getModuleId: getModuleId,
};