Class: Isort::Section

Inherits:
Object
  • Object
show all
Defined in:
lib/isort/section.rb

Overview

Categorizes imports into sections based on their source

  • STDLIB: Ruby standard library
  • THIRDPARTY: Gems/external packages
  • FIRSTPARTY: Project's own modules
  • LOCALFOLDER: Relative imports

Constant Summary collapse

SECTIONS =
%i[stdlib thirdparty firstparty localfolder].freeze
SECTION_ORDER =
{
  stdlib: 0,
  thirdparty: 1,
  firstparty: 2,
  localfolder: 3
}.freeze
STDLIB_MODULES =

Ruby standard library modules (partial list of common ones) This list covers the most commonly used stdlib modules

Set.new(%w[
  abbrev
  base64
  benchmark
  bigdecimal
  cgi
  csv
  date
  delegate
  digest
  drb
  english
  erb
  etc
  fcntl
  fiddle
  fileutils
  find
  forwardable
  getoptlong
  io/console
  io/nonblock
  io/wait
  ipaddr
  irb
  json
  logger
  matrix
  minitest
  monitor
  mutex_m
  net/ftp
  net/http
  net/https
  net/imap
  net/pop
  net/smtp
  nkf
  objspace
  observer
  open-uri
  open3
  openssl
  optparse
  ostruct
  pathname
  pp
  prettyprint
  prime
  pstore
  psych
  racc
  rake
  rdoc
  readline
  resolv
  ripper
  rss
  securerandom
  set
  shellwords
  singleton
  socket
  stringio
  strscan
  syslog
  tempfile
  time
  timeout
  tmpdir
  tracer
  tsort
  un
  uri
  weakref
  webrick
  yaml
  zlib
]).freeze

Class Method Summary collapse

Class Method Details

.classify(statement) ⇒ Object

Classify an import statement into a section



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/isort/section.rb', line 105

def classify(statement)
  case statement.type
  when :require
    classify_require(statement)
  when :require_relative
    :localfolder
  when :include, :extend, :using
    classify_module(statement)
  when :autoload
    :firstparty  # autoload is typically project-specific
  else
    :thirdparty
  end
end

.order(section) ⇒ Object

Get the section order for sorting



121
122
123
# File 'lib/isort/section.rb', line 121

def order(section)
  SECTION_ORDER[section] || 999
end