Class: Oxc::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/oxc/options.rb,
sig/oxc/options.rbs

Constant Summary collapse

MINIFY =

Returns:

  • (Array[Symbol])
[
  :filename,
  :lang,
  :source_type,
  :compress,
  :mangle,
  :codegen,
  :sourcemap,
  :strict
].freeze
TRANSFORM =

Signature:

  • Array[Symbol]

Returns:

  • (Array[Symbol])
[
  :filename,
  :lang,
  :source_type,
  :cwd,
  :target,
  :jsx,
  :typescript,
  :assumptions,
  :decorator,
  :helpers,
  :define,
  :inject,
  :minify,
  :codegen,
  :sourcemap,
  :strict
].freeze
PARSE =

Signature:

  • Array[Symbol]

Returns:

  • (Array[Symbol])
[
  :filename,
  :lang,
  :source_type,
  :ast_type,
  :ast,
  :ranges,
  :preserve_parens,
  :comments,
  :module_record,
  :symbols,
  :semantic_errors
].freeze
KNOWN =

Signature:

  • Array[Symbol]

Returns:

  • (Array[Symbol])
(MINIFY | TRANSFORM | PARSE).freeze
RUBY_ONLY =

Signature:

  • Array[Symbol]

Returns:

  • (Array[Symbol])
[:strict].freeze
UNSUPPORTED =

TODO: support mangle_props. It needs lazy-regex and rustc-hash as direct dependencies of the Rust crate, because oxc_minifier::ManglePropertiesOptions types include and exclude as lazy_regex::Regex and reserved as FxHashSet<CompactStr>.

Returns:

  • (Array[Symbol])
[:mangle_props].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, allowed = KNOWN, subject = "a call") ⇒ Options

: (Hash[Symbol, untyped], ?Array, ?String) -> void

Parameters:

  • (Hash[Symbol, untyped])
  • (Array[Symbol])
  • (String)


65
66
67
68
69
70
71
72
73
# File 'lib/oxc/options.rb', line 65

def initialize(options, allowed = KNOWN, subject = "a call")
  given = options.transform_keys(&:to_sym)

  validate!(given.keys, allowed, subject)

  @to_h = normalize(given).freeze

  freeze
end

Instance Attribute Details

#to_hHash[Symbol, untyped] (readonly)

Signature:

  • Hash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


57
58
59
# File 'lib/oxc/options.rb', line 57

def to_h
  @to_h
end

Class Method Details

.serialize(options, allowed = KNOWN, subject = "a call") ⇒ String

: (Hash[Symbol, untyped], ?Array, ?String) -> String

Parameters:

  • (Hash[Symbol, untyped])
  • (Array[Symbol])
  • (String)

Returns:

  • (String)


60
61
62
# File 'lib/oxc/options.rb', line 60

def self.serialize(options, allowed = KNOWN, subject = "a call")
  new(options, allowed, subject).to_json
end

Instance Method Details

#inspectString

: () -> String

Returns:

  • (String)


81
82
83
# File 'lib/oxc/options.rb', line 81

def inspect
  "#<#{self.class.name} #{to_h.inspect}>"
end

#normalize(options) ⇒ Hash[Symbol, untyped]

: (Hash[Symbol, untyped]) -> Hash[Symbol, untyped]

Parameters:

  • (Hash[Symbol, untyped])

Returns:

  • (Hash[Symbol, untyped])


105
106
107
108
109
110
111
# File 'lib/oxc/options.rb', line 105

def normalize(options)
  normalized = options.dup

  RUBY_ONLY.each { |name| normalized.delete(name) }

  normalized.compact
end

#to_json(state = nil) ⇒ String

: (?JSON::options?) -> String

Parameters:

  • (JSON::options, nil)

Returns:

  • (String)


76
77
78
# File 'lib/oxc/options.rb', line 76

def to_json(state = nil)
  state ? JSON.generate(to_h, state) : JSON.generate(to_h)
end

#validate!(names, allowed, subject) ⇒ void

This method returns an undefined value.

: (Array, Array, String) -> void

Parameters:

  • (Array[Symbol])
  • (Array[Symbol])
  • (String)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/oxc/options.rb', line 88

def validate!(names, allowed, subject)
  unsupported = names & UNSUPPORTED

  raise OptionError, "#{unsupported.join(", ")} is not supported yet" if unsupported.any?

  unknown = names - KNOWN

  raise OptionError, "Unknown option#{"s" if unknown.length > 1}: #{unknown.join(", ")}" if unknown.any?

  unsupported = names - allowed

  return if unsupported.empty?

  raise OptionError, "#{unsupported.join(", ")} #{unsupported.one? ? "is not an option" : "are not options"} for #{subject}"
end