예제 #1
0
파일: check.go 프로젝트: rakyll/GCSolutions
// checkInFileBlock performs safety checks for renames of objects in the file block,
// i.e. imported package names.
func (r *Unexporter) checkInFileBlock(objsToUpdate map[types.Object]string, from *types.PkgName, to string) {
	// Check import name is not "init".
	if to == "init" {
		r.errorf(from.Pos(), "%q is not a valid imported package name", to)
	}

	// Check for conflicts between file and package block.
	if prev := from.Pkg().Scope().Lookup(to); prev != nil {
		r.warn(from,
			r.errorf(from.Pos(), "renaming this %s %q to %q would conflict",
				objectKind(from), from.Name(), to),
			r.errorf(prev.Pos(), "\twith this package member %s",
				objectKind(prev)))
		return // since checkInPackageBlock would report redundant errors
	}

	// Check for conflicts in lexical scope.
	r.checkInLexicalScope(objsToUpdate, from, to, r.packages[from.Pkg()])

	// Finally, modify ImportSpec syntax to add or remove the Name as needed.
	info, path, _ := r.iprog.PathEnclosingInterval(from.Pos(), from.Pos())
	if from.Imported().Name() == to {
		// ImportSpec.Name not needed
		path[1].(*ast.ImportSpec).Name = nil
	} else {
		// ImportSpec.Name needed
		if spec := path[1].(*ast.ImportSpec); spec.Name == nil {
			spec.Name = &ast.Ident{NamePos: spec.Path.Pos(), Name: to}
			info.Defs[spec.Name] = from
		}
	}
}