Skip to content

Commit

Permalink
Add new FileAwareWarningsGuard that can give the script node for an e…
Browse files Browse the repository at this point in the history
…rror

The @Suppress based WarningsGuard currently uses this functionality, and we'd
like to also this same functionality to the CheckIjsWarningsGuard

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=185019159
  • Loading branch information
blickly committed Feb 9, 2018
1 parent bcd8cc7 commit 6dffb43
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
52 changes: 52 additions & 0 deletions src/com/google/javascript/jscomp/FileAwareWarningsGuard.java
@@ -0,0 +1,52 @@
/*
* Copyright 2018 The Closure Compiler Authors.
*
* 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.common.base.Preconditions.checkState;

import com.google.javascript.rhino.Node;
import javax.annotation.Nullable;

/**
* An abstract WarningsGuard that provides an additional getScriptNodeForError() method
* for accessing the containing SCRIPT node of the AST in a robust way.
*/
public abstract class FileAwareWarningsGuard extends WarningsGuard {

private final AbstractCompiler compiler;

protected FileAwareWarningsGuard(AbstractCompiler compiler) {
this.compiler = compiler;
}

@Nullable
protected final Node getScriptNodeForError(JSError error) {
if (error.sourceName == null) {
return null;
}
Node scriptNode = compiler.getScriptNode(error.sourceName);
if (scriptNode != null) {
// TODO(b/73088845): This should always be a SCRIPT node
if (!scriptNode.isScript()) {
return null;
}
checkState(scriptNode.isScript(), scriptNode);
return scriptNode;
}
return null;
}
}
10 changes: 4 additions & 6 deletions src/com/google/javascript/jscomp/SuppressDocWarningsGuard.java
Expand Up @@ -31,19 +31,17 @@
* *
* @author nicksantos@google.com (Nick Santos) * @author nicksantos@google.com (Nick Santos)
*/ */
class SuppressDocWarningsGuard extends WarningsGuard { class SuppressDocWarningsGuard extends FileAwareWarningsGuard {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;


private final AbstractCompiler compiler;

/** Warnings guards for each suppressible warnings group, indexed by name. */ /** Warnings guards for each suppressible warnings group, indexed by name. */
private final Map<String, DiagnosticGroupWarningsGuard> suppressors = private final Map<String, DiagnosticGroupWarningsGuard> suppressors =
new HashMap<>(); new HashMap<>();


/** The suppressible groups, indexed by name. */ /** The suppressible groups, indexed by name. */
SuppressDocWarningsGuard( SuppressDocWarningsGuard(
AbstractCompiler compiler, Map<String, DiagnosticGroup> suppressibleGroups) { AbstractCompiler compiler, Map<String, DiagnosticGroup> suppressibleGroups) {
this.compiler = compiler; super(compiler);
for (Map.Entry<String, DiagnosticGroup> entry : suppressibleGroups.entrySet()) { for (Map.Entry<String, DiagnosticGroup> entry : suppressibleGroups.entrySet()) {
suppressors.put( suppressors.put(
entry.getKey(), entry.getKey(),
Expand Down Expand Up @@ -84,8 +82,8 @@ class SuppressDocWarningsGuard extends WarningsGuard {
@Override @Override
public CheckLevel level(JSError error) { public CheckLevel level(JSError error) {
Node node = error.node; Node node = error.node;
if (node == null && error.sourceName != null) { if (node == null) {
node = compiler.getScriptNode(error.sourceName); node = getScriptNodeForError(error);
} }
if (node != null) { if (node != null) {
for (Node current = node; for (Node current = node;
Expand Down

0 comments on commit 6dffb43

Please sign in to comment.