-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck-names.js
More file actions
185 lines (151 loc) · 5.73 KB
/
Copy pathcheck-names.js
File metadata and controls
185 lines (151 loc) · 5.73 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// # Copyright (c) 2014-2021 Feudal Code Limitada - MIT license #
"use strict"
import { err } from "./helper.js"
var isSingleJs = true
var rats
var missingFiles = { } // path: token
var internalUnuseds = [ ]
var internalUnknowns = { } // fullname: token
var unusedExports = [ ]
var unknownImports = [ ]
export function checkNames(rs, singleJs) {
isSingleJs = singleJs
rats = rs
const paths = Object.keys(rats)
//
for (const path of paths) {
const rat = rats[path]
//
markUseOnDeclareds(rat) // fills internalUnknowns and checks forbidden assignments
checkImports(rat) // fills unknownImports; also marks exporteds elsewhere
}
//
for (const path of paths) {
const rat = rats[path]
fillUnusedExports(rat) // must come before fillUnuseds
fillUnuseds(rat)
}
//
warnMissingFiles()
warnUnusedExports()
warnUnknownImports()
warnInternalUnuseds()
warnInternalUnknowns()
}
// mark useds /////////////////////////////////////////////////////////////////
function markUseOnDeclareds(rat) { // fills internalUnknowns and checks forbidden assignments
for (const [fullname, token] of Object.entries(rat.useds)) {
markUseOnDeclared(rat.declareds, fullname, token)
}
}
function markUseOnDeclared(declareds, fullname, usedToken) {
let name = fullname
//
while (true) {
const declarToken = declareds[name]
if (declarToken != undefined) { markUseOnDeclaredNow(declarToken, usedToken); return }
// searching declared in outer scope:
if (name.indexOf(".") == -1) { break }
name = shiftName(name)
}
// declared not found
if (internalUnknowns[name] != undefined) { return }
internalUnknowns[name] = usedToken
}
function shiftName(name) {
const parts = name.split(".")
const tail = parts.pop()
parts.pop()
parts.push(tail)
return parts.join(".")
}
function markUseOnDeclaredNow(declarToken, usedToken) {
declarToken.wasUsed = true
if (! usedToken.wasAssigned) { return }
//
let msg = ""
if (declarToken.isImport) { msg = "can not assign to imported element" }
if (declarToken.isConstant) { msg = "can not assign to constant" }
if (msg == "") { return }
err(usedToken, msg)
}
// check imports //////////////////////////////////////////////////////////////
function checkImports(rat) { // fills unknownImports; also marks exporteds elsewhere
for (const [path, importObj] of Object.entries(rat.imports)) {
checkImports2(path, importObj)
}
}
function checkImports2(importPath, importObj) {
if (importPath.startsWith("https://")) { return }
//
const exporter = rats[importPath]
if (exporter == undefined) {
const token = importObj["path-token"]
missingFiles[importPath] = token
return
}
//
const imports = importObj["import-tokens"]
const exports = Object.values(exporter.exports)
for (const token of imports) { checkImports3(token, exports) }
}
function checkImports3(importToken, exports) {
for (const token of exports) {
if (token.value == importToken.value) { token.wasExported = true; return }
}
unknownImports.push(importToken)
}
///////////////////////////////////////////////////////////////////////////////
function fillUnusedExports(rat) {
for (const token of Object.values(rat.exports)) {
if (token.wasExported) { continue }
unusedExports.push(token)
}
}
///////////////////////////////////////////////////////////////////////////////
function fillUnuseds(rat) {
for (const token of Object.values(rat.declareds)) {
if (token.wasUsed) { continue }
if (token.wasExported) { continue }
if (unusedExports.indexOf(token) != -1) { continue } // avoiding double warning
internalUnuseds.push(token) // includes exported names
}
}
///////////////////////////////////////////////////////////////////////////////
function warnMissingFiles() {
for (const [path, token] of Object.entries(missingFiles)) {
console.log("\n" + token.rat.path + " (" + token.row + ":" + token.col + ")")
const msg = isSingleJs ? "not checking" : "could not find"
console.log("Warning: " + msg + " module: " + path)
}
}
///////////////////////////////////////////////////////////////////////////////
function warnUnusedExports() {
for (const token of unusedExports) {
console.log("\n" + token.rat.path + " (" + token.row + ":" + token.col + ")")
console.log("Warning: unused export: " + token.value)
}
}
///////////////////////////////////////////////////////////////////////////////
function warnUnknownImports() {
for (const token of unknownImports) {
console.log("\n" + token.rat.path + " (" + token.row + ":" + token.col + ")")
console.log("Warning: UNKNOWN IMPORT: " + token.value)
}
}
///////////////////////////////////////////////////////////////////////////////
function warnInternalUnuseds() {
for (const token of internalUnuseds) {
if (token.value.startsWith("__")) { continue }
console.log("\n" + token.rat.path + " (" + token.row + ":" + token.col + ")")
console.log("Warning: unused element: " + token.value)
}
}
///////////////////////////////////////////////////////////////////////////////
function warnInternalUnknowns() {
for (const token of Object.values(internalUnknowns)) {
console.log("\n" + token.rat.path + " (" + token.row + ":" + token.col + ")")
console.log("*** UNKNOWN NAME WARNING ***: " + token.value)
}
}
//