Class: RuboCop::Tailwindcss::Sorter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/tailwindcss/sorter.rb

Overview

The order oracle: Tailwind's design system, bundled into a single JS file and evaluated inside an embedded engine - warm for the life of the process, no node, no subprocesses. Sorting asks the same design.getClassOrder that prettier-plugin-tailwindcss asks; the vendored bundle is proven against the official plugin by this repo's factory.

Note: the engines' eval APIs only ever receive this gem's own vendored bundle and JSON-encoded class strings - no untrusted code is evaluated.

Defined Under Namespace

Classes: Error, MiniRacerEngine, QuickjsEngine

Constant Summary collapse

TAILWIND_VERSION =

The Tailwind version the vendored bundle must carry - the one number this gem's entire behavior derives from. The factory stamps the bundle at build time and the suite asserts agreement, so bumping either side alone turns the tests red.

"4.3.1"
BUNDLE_PATH =
File.expand_path("../../../vendor/tw-bundle.js", __dir__)
DEFAULT_CSS =
%(@import "tailwindcss";)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stylesheet: nil, engine: "quickjs") ⇒ Sorter

Returns a new instance of Sorter.



39
40
41
42
43
# File 'lib/rubocop/tailwindcss/sorter.rb', line 39

def initialize(stylesheet: nil, engine: "quickjs")
  @stylesheet = stylesheet
  @engine = build_engine(engine)
  boot
end

Class Method Details

.for(stylesheet:, engine:) ⇒ Object

One warm oracle per (process, stylesheet, engine). The PID in the key makes forked rubocop workers each boot their own engine lazily - an embedded VM must never be shared across a fork.



29
30
31
32
33
34
35
36
# File 'lib/rubocop/tailwindcss/sorter.rb', line 29

def for(stylesheet:, engine:)
  key = [ Process.pid, stylesheet, engine ]
  if @key != key
    @key = key
    @instance = new(stylesheet: stylesheet, engine: engine)
  end
  @instance
end

Instance Method Details

#entry_shaObject



53
54
55
# File 'lib/rubocop/tailwindcss/sorter.rb', line 53

def entry_sha
  @engine.run("__twMeta.entrySha")
end

#sort(class_string) ⇒ Object



45
46
47
# File 'lib/rubocop/tailwindcss/sorter.rb', line 45

def sort(class_string)
  @engine.run("__twSort(#{class_string.to_json})")
end

#tailwind_versionObject



49
50
51
# File 'lib/rubocop/tailwindcss/sorter.rb', line 49

def tailwind_version
  @engine.run("__twMeta.tailwind")
end