forked from bazelbuild/rules_closure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckStrictDeps.java
More file actions
158 lines (135 loc) · 5.26 KB
/
Copy pathCheckStrictDeps.java
File metadata and controls
158 lines (135 loc) · 5.26 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
/*
* Copyright 2016 The Closure Rules Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
import static com.google.javascript.jscomp.JsCheckerHelper.convertPathToModuleName;
import com.google.javascript.jscomp.NodeTraversal.AbstractShallowCallback;
import com.google.javascript.rhino.Node;
import io.bazel.rules.closure.Webpath;
abstract class CheckStrictDeps
extends AbstractShallowCallback implements HotSwapCompilerPass {
public static final DiagnosticType DUPLICATE_PROVIDES =
DiagnosticType.error(
"CR_DUPLICATE_PROVIDES", "Namespace provided multiple times by srcs of {0}.");
public static final DiagnosticType NOT_PROVIDED =
DiagnosticType.error(
"CR_NOT_PROVIDED", "Namespace not provided by any srcs or direct deps of {0}.");
public static final DiagnosticType REDECLARED_PROVIDES =
DiagnosticType.error("CR_REDECLARED_PROVIDES", "Namespace already provided by deps of {0}.");
private final AbstractCompiler compiler;
private CheckStrictDeps(AbstractCompiler compiler) {
this.compiler = compiler;
}
@Override
public final void process(Node externs, Node root) {
NodeTraversal.traverse(compiler, root, this);
}
@Override
public final void hotSwapScript(Node scriptRoot, Node originalRoot) {
NodeTraversal.traverse(compiler, scriptRoot, this);
}
static final class FirstPass extends CheckStrictDeps {
private final JsCheckerState state;
FirstPass(JsCheckerState state, AbstractCompiler compiler) {
super(compiler);
this.state = state;
}
@Override
public final void visit(NodeTraversal t, Node n, Node parent) {
if (!n.isCall()) {
return;
}
Node callee = n.getFirstChild();
Node parameter = n.getLastChild();
if (parameter.isString()
&& (callee.matchesQualifiedName("goog.provide")
|| callee.matchesQualifiedName("goog.module"))) {
String namespace = JsCheckerHelper.normalizeClosureNamespace(parameter.getString());
if (!state.mysterySources.contains(t.getSourceName())) {
if (!state.provides.add(namespace)) {
t.report(parameter, DUPLICATE_PROVIDES, state.label);
}
if (state.provided.contains(namespace)
&& state.redeclaredProvides.add(namespace)) {
t.report(parameter, REDECLARED_PROVIDES, state.label);
}
// Since this file uses Google namespaces, it can no longer be loaded as an ES6 module.
state.provides.removeAll(convertPathToModuleName(t.getSourceName(), state.roots).asSet());
} else {
state.provided.add(namespace);
state.provided.removeAll(convertPathToModuleName(t.getSourceName(), state.roots).asSet());
}
}
}
}
static final class SecondPass extends CheckStrictDeps {
private final JsCheckerState state;
SecondPass(JsCheckerState state, AbstractCompiler compiler) {
super(compiler);
this.state = state;
}
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
switch (n.getToken()) {
case CALL:
visitFunctionCall(t, n);
break;
case IMPORT:
visitEs6Import(t, n);
break;
default:
break;
}
}
private void visitFunctionCall(NodeTraversal t, Node n) {
Node callee = n.getFirstChild();
Node parameter = n.getLastChild();
if (!parameter.isString()) {
return;
}
if (callee.matchesQualifiedName("goog.require")) {
checkNamespaceIsProvided(t, parameter,
JsCheckerHelper.normalizeClosureNamespace(parameter.getString()));
}
}
private void visitEs6Import(NodeTraversal t, Node n) {
Node namespace = n.getChildAtIndex(2);
if (!namespace.isString()) {
return;
}
checkNamespaceIsProvided(t, namespace, namespace.getString());
}
private void checkNamespaceIsProvided(NodeTraversal t, Node n, String namespace) {
if (namespace.startsWith("/") || namespace.startsWith(".")) {
// TODO(jart): Unify path resolution with ModuleLoader.
// NOTE(robfig): To enable usage of extensionless ES6 modules,
// copy these 3 lines from NodeModuleResolver.java.
if (!namespace.endsWith(".js")) {
namespace += ".js";
}
Webpath me = Webpath.get(t.getSourceName());
if (!me.isAbsolute()) {
me = Webpath.get("/").resolve(me);
}
namespace = me.lookup(Webpath.get(namespace)).toString();
}
if (!state.provided.contains(namespace)
&& !state.provides.contains(namespace)
&& state.notProvidedNamespaces.add(namespace)) {
t.report(n, NOT_PROVIDED, state.label);
}
}
}
}