2020-01-23 21:04:46 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2020-01-18 23:26:14 +01:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module parser
|
2020-04-25 17:49:16 +02:00
|
|
|
|
2020-02-20 11:13:18 +01:00
|
|
|
// return true if file being parsed imports `mod`
|
|
|
|
pub fn (p &Parser) known_import(mod string) bool {
|
|
|
|
return mod in p.imports
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p &Parser) prepend_mod(name string) string {
|
2020-02-27 03:30:17 +01:00
|
|
|
if p.expr_mod != '' {
|
|
|
|
return p.expr_mod + '.' + name
|
|
|
|
}
|
2020-02-20 11:13:18 +01:00
|
|
|
if p.builtin_mod || p.mod == 'main' {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
return '${p.mod}.$name'
|
|
|
|
}
|
2020-05-14 17:14:24 +02:00
|
|
|
|
|
|
|
fn (p &Parser) is_used_import(alias string) bool {
|
|
|
|
return alias in p.used_imports
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut p Parser) register_used_import(alias string) {
|
|
|
|
if alias !in p.used_imports {
|
|
|
|
p.used_imports << alias
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) check_unused_imports() {
|
|
|
|
mut output := ''
|
|
|
|
for alias, mod in p.imports {
|
|
|
|
if !p.is_used_import(alias) {
|
|
|
|
mod_alias := if alias == mod { alias } else { '$alias ($mod)' }
|
|
|
|
output += '\n * $mod_alias'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if output == '' {
|
|
|
|
return
|
|
|
|
}
|
2020-05-16 21:03:28 +02:00
|
|
|
if p.pref.is_repl {
|
|
|
|
// The REPL should be much more liberal, and should not warn about
|
|
|
|
// unused imports, because they probably will be in the next few lines...
|
|
|
|
return
|
|
|
|
}
|
2020-05-14 17:14:24 +02:00
|
|
|
eprintln('`$p.file_name` warning: the following imports were never used: $output')
|
|
|
|
}
|