console1984 0.1.9 → 0.1.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2f1312b244e339b0196d4baa80fc989ed3871cd722f166a278f4877ca1dee4da
4
- data.tar.gz: a66ab76f426a50cb99c4de2223d420c9d2d246b5dd0a6ec8f1712deca56244f9
3
+ metadata.gz: 78f591775f469668e6d50741f421c188e048e42942ce4a9c9c707e9a34328c69
4
+ data.tar.gz: 190d0069e0bc4268400e59f1884539403233fd66fb873892d41ecf5e3d888323
5
5
  SHA512:
6
- metadata.gz: d46797d8751dde38fccb25fef4e86e3a8ee42b17251d932e5cbc9ffc381bba2bd42c2a946ce8e7e867ff8e6270700dec73d8425534f52970c87bffbc682365f8
7
- data.tar.gz: cd1a43abe5170dfb94a83ed375b8517b4263354ba30a53cb2287f3ffd240ab18984134df79801657fcb07fea1826d6fc3f978f6ae752c013ead820330d8e024a
6
+ metadata.gz: d6930e488260091857f8f2196ef88ec3c364391de1b1a9e9385a246153c80a1b5c7cd3cafd76d988d54f3ded5b4b3f66b2ae1d9735cc5ebeacadabd4a3bbe12e
7
+ data.tar.gz: 19746bce408667fb06d48e5a98032999e46a44b47f3620d510b5ce4a707a9414c76f57b996d5a707372bf533b5904aa2af19bd0b9c2866fc8d00e4b2e5228f70
@@ -0,0 +1,69 @@
1
+ # Naming class with dot so that it doesn't get loaded eagerly by Zeitwork. We want to load
2
+ # only when a console session is started, when +parser+ is loaded.
3
+ #
4
+ # See +Console1984::Supervisor#require_dependencies+
5
+ class Console1984::CommandValidator::CommandParser < ::Parser::AST::Processor
6
+ include AST::Processor::Mixin
7
+ include Console1984::Freezeable
8
+
9
+ def initialize
10
+ @constants = []
11
+ @declared_classes_or_modules = []
12
+ @constant_assignments = []
13
+ end
14
+
15
+ # We define accessors to define lists without duplicates. We are not using a +SortedSet+ because we want
16
+ # to mutate strings in the list while the processing is happening. And we don't use metapgroamming to define the
17
+ # accessors to prevent having problems with freezable and its instance_variable* protection.
18
+
19
+ def constants
20
+ @constants.uniq
21
+ end
22
+
23
+ def declared_classes_or_modules
24
+ @declared_classes_or_modules.uniq
25
+ end
26
+
27
+ def constant_assignments
28
+ @constant_assignments.uniq
29
+ end
30
+
31
+ def on_class(node)
32
+ super
33
+ const_declaration, _, _ = *node
34
+ constant = extract_constants(const_declaration).first
35
+ @declared_classes_or_modules << constant if constant.present?
36
+ end
37
+
38
+ alias_method :on_module, :on_class
39
+
40
+ def on_const(node)
41
+ super
42
+ name, const_name = *node
43
+ const_name = const_name.to_s
44
+ last_constant = @constants.last
45
+
46
+ if name.nil? || (name && name.type == :cbase) # cbase = leading ::
47
+ if last_constant&.end_with?("::")
48
+ last_constant << const_name
49
+ else
50
+ @constants << const_name
51
+ end
52
+ elsif last_constant
53
+ last_constant << "::#{const_name}"
54
+ end
55
+ end
56
+
57
+ def on_casgn(node)
58
+ super
59
+ scope_node, name, value_node = *node
60
+ @constant_assignments.push(*extract_constants(value_node))
61
+ end
62
+
63
+ private
64
+ def extract_constants(node)
65
+ self.class.new.tap do |processor|
66
+ processor.process(node)
67
+ end.constants
68
+ end
69
+ end
@@ -6,85 +6,19 @@ class Console1984::CommandValidator::ParsedCommand
6
6
 
7
7
  attr_reader :raw_command
8
8
 
9
- delegate :declared_classes_or_modules, :constants, :constant_assignments, to: :processed_ast
9
+ delegate :declared_classes_or_modules, :constants, :constant_assignments, to: :command_parser
10
10
 
11
11
  def initialize(raw_command)
12
12
  @raw_command = Array(raw_command).join("\n")
13
13
  end
14
14
 
15
15
  private
16
- def processed_ast
17
- @processed_ast ||= CommandProcessor.new.tap do |processor|
16
+ def command_parser
17
+ @command_parser ||= Console1984::CommandValidator::CommandParser.new.tap do |processor|
18
18
  ast = Parser::CurrentRuby.parse(raw_command)
19
19
  processor.process(ast)
20
20
  rescue Parser::SyntaxError
21
21
  # Fail open with syntax errors
22
22
  end
23
23
  end
24
-
25
- class CommandProcessor < ::Parser::AST::Processor
26
- include AST::Processor::Mixin
27
- include Console1984::Freezeable
28
-
29
- def initialize
30
- @constants = []
31
- @declared_classes_or_modules = []
32
- @constant_assignments = []
33
- end
34
-
35
- # We define accessors to define lists without duplicates. We are not using a +SortedSet+ because we want
36
- # to mutate strings in the list while the processing is happening. And we don't use metapgroamming to define the
37
- # accessors to prevent having problems with freezable and its instance_variable* protection.
38
-
39
- def constants
40
- @constants.uniq
41
- end
42
-
43
- def declared_classes_or_modules
44
- @declared_classes_or_modules.uniq
45
- end
46
-
47
- def constant_assignments
48
- @constant_assignments.uniq
49
- end
50
-
51
- def on_class(node)
52
- super
53
- const_declaration, _, _ = *node
54
- constant = extract_constants(const_declaration).first
55
- @declared_classes_or_modules << constant if constant.present?
56
- end
57
-
58
- alias_method :on_module, :on_class
59
-
60
- def on_const(node)
61
- super
62
- name, const_name = *node
63
- const_name = const_name.to_s
64
- last_constant = @constants.last
65
-
66
- if name.nil? || (name && name.type == :cbase) # cbase = leading ::
67
- if last_constant&.end_with?("::")
68
- last_constant << const_name
69
- else
70
- @constants << const_name
71
- end
72
- elsif last_constant
73
- last_constant << "::#{const_name}"
74
- end
75
- end
76
-
77
- def on_casgn(node)
78
- super
79
- _scope_node, name, value_node = *node
80
- @constant_assignments.push(*extract_constants(value_node))
81
- end
82
-
83
- private
84
- def extract_constants(node)
85
- self.class.new.tap do |processor|
86
- processor.process(node)
87
- end.constants
88
- end
89
- end
90
24
  end
@@ -11,14 +11,17 @@ class Console1984::Refrigerator
11
11
  end
12
12
 
13
13
  private
14
- EXTERNAL_MODULES_AND_CLASSES_TO_FREEZE = [Parser::CurrentRuby]
15
-
16
14
  def freeze_internal_instances
17
15
  Console1984.config.freeze unless Console1984.config.test_mode
18
16
  end
19
17
 
20
18
  def freeze_external_modules_and_classes
21
- EXTERNAL_MODULES_AND_CLASSES_TO_FREEZE.each { |klass| klass.include(Console1984::Freezeable) }
19
+ external_modules_and_classes_to_freeze.each { |klass| klass.include(Console1984::Freezeable) }
20
+ end
21
+
22
+ def external_modules_and_classes_to_freeze
23
+ # Not using a constant because we want this to run lazily (console-dependant dependencies might not be loaded).
24
+ [Parser::CurrentRuby]
22
25
  end
23
26
 
24
27
  def eager_load_all_classes
@@ -41,6 +41,16 @@ class Console1984::Supervisor
41
41
  require 'parser/current'
42
42
  end
43
43
  require 'colorized_string'
44
+
45
+ # Explicit lazy loading because it depends on +parser+, which we want to only load
46
+ # in console sessions.
47
+ require_relative "./command_validator/.command_parser"
48
+
49
+ # This solves a weird class loading error where ActiveRecord dosn't resolve +Relation+ properly.
50
+ # See https://github.com/basecamp/console1984/issues/29
51
+ #
52
+ # TODO: This is a temporary fix. Need to figure out why/when this happens.
53
+ require "active_record/relation"
44
54
  end
45
55
 
46
56
  def start_session
@@ -1,3 +1,3 @@
1
1
  module Console1984
2
- VERSION = '0.1.9'
2
+ VERSION = '0.1.13'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console1984
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jorge Manrubia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-06 00:00:00.000000000 Z
11
+ date: 2021-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -200,6 +200,7 @@ files:
200
200
  - lib/console1984.rb
201
201
  - lib/console1984/command_executor.rb
202
202
  - lib/console1984/command_validator.rb
203
+ - lib/console1984/command_validator/.command_parser.rb
203
204
  - lib/console1984/command_validator/forbidden_constant_reference_validation.rb
204
205
  - lib/console1984/command_validator/forbidden_reopening_validation.rb
205
206
  - lib/console1984/command_validator/parsed_command.rb