-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathnamespace.js
156 lines (126 loc) · 4.91 KB
/
namespace.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
145
146
147
148
149
150
151
152
153
154
155
156
import Exports from '../core/getExports'
import importDeclaration from '../importDeclaration'
import declaredScope from '../core/declaredScope'
module.exports = function (context) {
const namespaces = new Map()
function makeMessage(last, namepath) {
return `'${last.name}' not found in` +
(namepath.length > 1 ? ' deeply ' : ' ') +
`imported namespace '${namepath.join('.')}'.`
}
return {
// pick up all imports at body entry time, to properly respect hoisting
'Program': function ({ body }) {
function processBodyStatement(declaration) {
if (declaration.type !== 'ImportDeclaration') return
if (declaration.specifiers.length === 0) return
const imports = Exports.get(declaration.source.value, context)
if (imports == null) return null
if (imports.errors.length) {
imports.reportErrors(context, declaration)
return
}
for (let specifier of declaration.specifiers) {
switch (specifier.type) {
case 'ImportNamespaceSpecifier':
if (!imports.hasNamed) {
context.report(specifier,
`No exported names found in module '${declaration.source.value}'.`)
}
namespaces.set(specifier.local.name, imports.named)
break
case 'ImportDefaultSpecifier':
case 'ImportSpecifier': {
const meta = imports.named.get(
// default to 'default' for default http://i.imgur.com/nj6qAWy.jpg
specifier.imported ? specifier.imported.name : 'default')
if (!meta || !meta.namespace) break
namespaces.set(specifier.local.name, meta.namespace)
break
}
}
}
}
body.forEach(processBodyStatement)
},
// same as above, but does not add names to local map
'ExportNamespaceSpecifier': function (namespace) {
var declaration = importDeclaration(context)
var imports = Exports.get(declaration.source.value, context)
if (imports == null) return null
if (imports.errors.length) {
imports.reportErrors(context, declaration)
return
}
if (!imports.hasNamed) {
context.report(namespace,
`No exported names found in module '${declaration.source.value}'.`)
}
},
// todo: check for possible redefinition
'MemberExpression': function (dereference) {
if (dereference.object.type !== 'Identifier') return
if (!namespaces.has(dereference.object.name)) return
if (dereference.parent.type === 'AssignmentExpression' &&
dereference.parent.left === dereference) {
context.report(dereference.parent,
`Assignment to member of namespace '${dereference.object.name}'.`)
}
// go deep
var namespace = namespaces.get(dereference.object.name)
var namepath = [dereference.object.name]
// while property is namespace and parent is member expression, keep validating
while (namespace instanceof Map &&
dereference.type === 'MemberExpression') {
if (dereference.computed) {
context.report(dereference.property,
'Unable to validate computed reference to imported namespace \'' +
dereference.object.name + '\'.')
return
}
if (!namespace.has(dereference.property.name)) {
context.report(
dereference.property,
makeMessage(dereference.property, namepath))
break
}
// stash and pop
namepath.push(dereference.property.name)
namespace = namespace.get(dereference.property.name).namespace
dereference = dereference.parent
}
},
'VariableDeclarator': function ({ id, init }) {
if (init == null) return
if (init.type !== 'Identifier') return
if (!namespaces.has(init.name)) return
// check for redefinition in intermediate scopes
if (declaredScope(context, init.name) !== 'module') return
// DFS traverse child namespaces
function testKey(pattern, namespace, path = [init.name]) {
if (!(namespace instanceof Map)) return
if (pattern.type !== 'ObjectPattern') return
for (let property of pattern.properties) {
if (property.key.type !== 'Identifier') {
context.report({
node: property,
message: 'Only destructure top-level names.',
})
continue
}
if (!namespace.has(property.key.name)) {
context.report({
node: property,
message: makeMessage(property.key, path),
})
continue
}
path.push(property.key.name)
testKey(property.value, namespace.get(property.key.name).namespace, path)
path.pop()
}
}
testKey(id, namespaces.get(init.name))
},
}
}