Class: RBS::CLI::Validate

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/cli/validate.rb,
sig/cli/validate.rbs

Defined Under Namespace

Classes: Errors

Instance Method Summary collapse

Constructor Details

#initialize(args:, options:) ⇒ Validate

Returns a new instance of Validate.

Parameters:



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
# File 'lib/rbs/cli/validate.rb', line 48

def initialize(args:, options:)
  loader = options.loader()
  @env = Environment.from_loader(loader).resolve_type_names
  @builder = DefinitionBuilder.new(env: @env)
  @validator = Validator.new(env: @env)
  limit = nil #: Integer?
  OptionParser.new do |opts|
    opts.banner = <<EOU
Usage: rbs validate

Validate RBS files. It ensures the type names in RBS files are present and the type applications have correct arity.

Examples:

  $ rbs validate
EOU

    opts.on("--silent", "This option has been deprecated and does nothing.") do
      RBS.print_warning { "`--silent` option is deprecated because it's silent by default. You can use --log-level option of rbs command to display more information." }
    end
    opts.on("--[no-]exit-error-on-syntax-error", "exit(1) if syntax error is detected") {|bool|
      RBS.print_warning { "`--exit-error-on-syntax-error` option is deprecated because it's validated during parsing.." }
    }
    opts.on("--fail-fast", "Exit immediately as soon as a validation error is found.") do |arg|
      limit = 1
    end
  end.parse!(args)

  @errors = Errors.new(limit: limit)
end

Instance Method Details

#runInteger

Returns:

  • (Integer)


79
80
81
82
83
84
85
86
87
88
# File 'lib/rbs/cli/validate.rb', line 79

def run
  @errors.try do
    validate_class_module_definition
    validate_class_module_alias_definition
    validate_interface
    validate_constant
    validate_global
    validate_type_alias
  end
end

#validate_class_module_alias_definitionvoid

This method returns an undefined value.



190
191
192
193
194
195
196
197
# File 'lib/rbs/cli/validate.rb', line 190

def validate_class_module_alias_definition
  @env.class_alias_decls.each do |name, entry|
    RBS.logger.info "Validating class/module alias definition: `#{name}`..."
    @validator.validate_class_alias(entry: entry)
  rescue BaseError => error
    @errors.add error
  end
end

#validate_class_module_definitionvoid

This method returns an undefined value.



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
186
187
188
# File 'lib/rbs/cli/validate.rb', line 92

def validate_class_module_definition
  @env.class_decls.each do |name, entry|
    RBS.logger.info "Validating class/module definition: `#{name}`..."
    @builder.build_instance(name).each_type do |type|
      @validator.validate_type type, context: nil
    rescue BaseError => error
      @errors.add(error)
    end
    @builder.build_singleton(name).each_type do |type|
      @validator.validate_type type, context: nil
    rescue BaseError => error
      @errors.add(error)
    end

    case entry
    when Environment::ClassEntry
      entry.each_decl do |decl|
        if super_class = decl.super_class
          super_class.args.each do |arg|
            @validator.validate_type(arg, context: nil)
          end
        end
      end
    when Environment::ModuleEntry
      entry.each_decl do |decl|
        decl.self_types.each do |self_type|
          self_type.args.each do |arg|
            @validator.validate_type(arg, context: nil)
          end

          self_params =
            if self_type.name.class?
              @env.module_entry(self_type.name, normalized: true)&.type_params
            else
              @env.interface_decls[self_type.name]&.decl&.type_params
            end

          if self_params
            InvalidTypeApplicationError.check!(type_name: self_type.name, params: self_params, args: self_type.args, location: self_type.location)
          end
        end
      end
    end

    d = entry.primary_decl

    @validator.validate_type_params(
      d.type_params,
      type_name: name,
      location: d.location&.aref(:type_params)
    )

    d.type_params.each do |param|
      if ub = param.upper_bound_type
        @validator.validate_type(ub, context: nil)
      end

      if lb = param.lower_bound_type
        @validator.validate_type(lb, context: nil)
      end

      if dt = param.default_type
        @validator.validate_type(dt, context: nil)
      end
    end

    TypeParamDefaultReferenceError.check!(d.type_params)

    entry.each_decl do |decl|
      case decl
      when AST::Declarations::Base
        decl.each_member do |member|
          case member
          when AST::Members::MethodDefinition
            @validator.validate_method_definition(member, type_name: name)
          when AST::Members::Mixin
            params =
              if member.name.class?
                module_decl = @env.module_entry(member.name, normalized: true) or raise
                module_decl.type_params
              else
                interface_decl = @env.interface_decls.fetch(member.name)
                interface_decl.decl.type_params
              end
            InvalidTypeApplicationError.check!(type_name: member.name, params: params, args: member.args, location: member.location)
          when AST::Members::Var
            @validator.validate_variable(member)
          end
        end
      else
        raise "Unknown declaration: #{decl.class}"
      end
    end
  rescue BaseError => error
    @errors.add(error)
  end
end

#validate_constantvoid

This method returns an undefined value.



239
240
241
242
243
244
245
246
247
# File 'lib/rbs/cli/validate.rb', line 239

def validate_constant
  @env.constant_decls.each do |name, const|
    RBS.logger.info "Validating constant: `#{name}`..."
    @validator.validate_type const.decl.type, context: const.context
    @builder.ensure_namespace!(name.namespace, location: const.decl.location)
  rescue BaseError => error
    @errors.add(error)
  end
end

#validate_globalvoid

This method returns an undefined value.



249
250
251
252
253
254
255
256
# File 'lib/rbs/cli/validate.rb', line 249

def validate_global
  @env.global_decls.each do |name, global|
    RBS.logger.info "Validating global: `#{name}`..."
    @validator.validate_type global.decl.type, context: nil
  rescue BaseError => error
    @errors.add(error)
  end
end

#validate_interfacevoid

This method returns an undefined value.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/rbs/cli/validate.rb', line 199

def validate_interface
  @env.interface_decls.each do |name, decl|
    RBS.logger.info "Validating interface: `#{name}`..."
    @builder.build_interface(name).each_type do |type|
      @validator.validate_type type, context: nil
    end

    @validator.validate_type_params(
      decl.decl.type_params,
      type_name: name,
      location: decl.decl.location&.aref(:type_params)
    )

    decl.decl.type_params.each do |param|
      if ub = param.upper_bound_type
        @validator.validate_type(ub, context: nil)
      end

      if lb = param.lower_bound_type
        @validator.validate_type(lb, context: nil)
      end

      if dt = param.default_type
        @validator.validate_type(dt, context: nil)
      end
    end

    TypeParamDefaultReferenceError.check!(decl.decl.type_params)

    decl.decl.members.each do |member|
      case member
      when AST::Members::MethodDefinition
        @validator.validate_method_definition(member, type_name: name)
      end
    end
  rescue BaseError => error
    @errors.add(error)
  end
end

#validate_type_aliasvoid

This method returns an undefined value.



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/rbs/cli/validate.rb', line 258

def validate_type_alias
  @env.type_alias_decls.each do |name, decl|
    RBS.logger.info "Validating alias: `#{name}`..."
    @builder.expand_alias1(name).tap do |type|
      @validator.validate_type type, context: nil
    end

    @validator.validate_type_alias(entry: decl)

    @validator.validate_type_params(
      decl.decl.type_params,
      type_name: name,
      location: decl.decl.location&.aref(:type_params)
    )

    decl.decl.type_params.each do |param|
      if ub = param.upper_bound_type
        @validator.validate_type(ub, context: nil)
      end

      if lb = param.lower_bound_type
        @validator.validate_type(lb, context: nil)
      end

      if dt = param.default_type
        @validator.validate_type(dt, context: nil)
      end
    end

    TypeParamDefaultReferenceError.check!(decl.decl.type_params)
  rescue BaseError => error
    @errors.add(error)
  end
end