Skip to content

Commit 7311935

Browse files
committed
Move JavaScript to parser backend
1 parent e963f62 commit 7311935

18 files changed

+42
-291
lines changed

Dockerfile

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ RUN apt-get update && \
1313
RUNLEVEL=1 apt-get install --yes --no-install-recommends \
1414
python2.7
1515

16-
COPY Gemfile* package.json package-lock.json ./
16+
COPY Gemfile* ./
1717
COPY vendor/php-parser/composer* ./vendor/php-parser/
1818

1919
RUN bundle install --jobs 4 --quiet && \
20-
composer install --no-interaction --quiet --working-dir ./vendor/php-parser && \
21-
npm install
20+
composer install --no-interaction --quiet --working-dir ./vendor/php-parser
2221

2322
COPY . ./
2423
RUN chown -R app:app ./

lib/cc/engine/analyzers/analyzer_base.rb

+32
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
require "cc/engine/analyzers/parser_error"
44
require "cc/engine/analyzers/parser_base"
5+
require "cc/engine/analyzers/file_list"
6+
require "cc/engine/processed_source"
7+
require "cc/engine/sexp_builder"
58

69
module CC
710
module Engine
@@ -120,6 +123,35 @@ def file_list
120123
def skip?(_path)
121124
nil
122125
end
126+
127+
def parse(file, request_path)
128+
processed_source = ProcessedSource.new(file, request_path)
129+
130+
SexpBuilder.new(processed_source.ast, file).build
131+
rescue CC::Parser::Client::HTTPError => ex
132+
unexpected_parse_error!(ex) unless ex.response_status.to_s.start_with?("4")
133+
134+
CC.logger.warn("Skipping #{processed_source.path} due to #{ex.class}")
135+
CC.logger.warn("Response status: #{ex.response_status}")
136+
CC.logger.debug { "Contents:\n#{processed_source.raw_source}" }
137+
CC.logger.debug { "Response:\n#{ex.response_body}" }
138+
nil
139+
rescue CC::Parser::Client::NestingDepthError => ex
140+
CC.logger.warn("Skipping #{processed_source.path} due to #{ex.class}")
141+
CC.logger.warn(ex.message)
142+
CC.logger.debug { "Contents:\n#{processed_source.raw_source}" }
143+
nil
144+
rescue => ex
145+
unexpected_parse_error!(ex)
146+
end
147+
148+
def unexpected_parse_error!(ex)
149+
CC.logger.error("Error processing file: #{processed_source.path}")
150+
CC.logger.error(ex.message)
151+
CC.logger.debug { "Contents:\n#{processed_source.raw_source}" }
152+
153+
raise
154+
end
123155
end
124156
end
125157
end

lib/cc/engine/analyzers/java/main.rb

+1-29
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
require "json"
55
require "cc/engine/analyzers/reporter"
66
require "cc/engine/analyzers/analyzer_base"
7-
require "cc/engine/processed_source"
8-
require "cc/engine/sexp_builder"
97

108
module CC
119
module Engine
@@ -21,42 +19,16 @@ class Main < CC::Engine::Analyzers::Base
2119
].freeze
2220
POINTS_PER_OVERAGE = 10_000
2321
REQUEST_PATH = "/java".freeze
24-
TIMEOUT = 300
2522

2623
private
2724

2825
def process_file(file)
29-
processed_source = ProcessedSource.new(file, REQUEST_PATH)
30-
31-
SexpBuilder.new(processed_source.ast, file).build
32-
rescue => ex
33-
if unparsable_file_error?(ex)
34-
CC.logger.warn("Skipping #{processed_source.path} due to #{ex.class}")
35-
CC.logger.warn("Response status: #{ex.response_status}")
36-
CC.logger.debug { "Contents:\n#{processed_source.raw_source}" }
37-
CC.logger.debug { "Response:\n#{ex.response_body}" }
38-
nil
39-
elsif ex.is_a?(CC::Parser::Client::NestingDepthError)
40-
CC.logger.warn("Skipping #{processed_source.path} due to #{ex.class}")
41-
CC.logger.warn(ex.message)
42-
CC.logger.debug { "Contents:\n#{processed_source.raw_source}" }
43-
nil
44-
else
45-
CC.logger.error("Error processing file: #{processed_source.path}")
46-
CC.logger.error(ex.message)
47-
CC.logger.debug { "Contents:\n#{processed_source.raw_source}" }
48-
raise
49-
end
26+
parse(file, REQUEST_PATH)
5027
end
5128

5229
def default_filters
5330
DEFAULT_FILTERS.map { |filter| Sexp::Matcher.parse filter }
5431
end
55-
56-
def unparsable_file_error?(ex)
57-
ex.is_a?(CC::Parser::Client::HTTPError) &&
58-
ex.response_status.to_s.start_with?("4")
59-
end
6032
end
6133
end
6234
end

lib/cc/engine/analyzers/javascript/main.rb

+3-23
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
# frozen_string_literal: true
22

33
require "cc/engine/analyzers/analyzer_base"
4-
require "cc/engine/analyzers/javascript/parser"
5-
require "cc/engine/analyzers/javascript/minification_checker"
6-
require "cc/engine/analyzers/javascript/node"
7-
require "cc/engine/analyzers/file_list"
8-
require "flay"
9-
require "json"
104

115
module CC
126
module Engine
@@ -20,26 +14,12 @@ class Main < CC::Engine::Analyzers::Base
2014
LANGUAGE = "javascript"
2115
DEFAULT_MASS_THRESHOLD = 40
2216
POINTS_PER_OVERAGE = 30_000
23-
24-
def transform_sexp(sexp)
25-
sexp.flatter
26-
end
17+
REQUEST_PATH = "/javascript".freeze
2718

2819
private
2920

30-
def process_file(path)
31-
ast = js_parser.new(File.read(path), path).parse
32-
Node.new(ast.syntax_tree, path).format if ast
33-
end
34-
35-
def js_parser
36-
::CC::Engine::Analyzers::Javascript::Parser
37-
end
38-
39-
def skip?(path)
40-
if MinificationChecker.new(path).minified?
41-
"the file is minified"
42-
end
21+
def process_file(file)
22+
parse(file, REQUEST_PATH)
4323
end
4424
end
4525
end

lib/cc/engine/analyzers/javascript/minification_checker.rb

-30
This file was deleted.

lib/cc/engine/analyzers/javascript/node.rb

-38
This file was deleted.

lib/cc/engine/analyzers/javascript/parser.js

-54
This file was deleted.

lib/cc/engine/analyzers/javascript/parser.rb

-48
This file was deleted.

lib/cc/engine/analyzers/python/main.rb

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require "cc/engine/analyzers/python/parser"
44
require "cc/engine/analyzers/python/node"
55
require "cc/engine/analyzers/reporter"
6-
require "cc/engine/analyzers/file_list"
76
require "cc/engine/analyzers/analyzer_base"
87
require "flay"
98

package-lock.json

-13
This file was deleted.

package.json

-13
This file was deleted.

spec/cc/engine/analyzers/analyzer_base_spec.rb

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require 'spec_helper'
22
require 'cc/engine/analyzers/analyzer_base'
33
require 'cc/engine/analyzers/engine_config'
4-
require 'cc/engine/analyzers/file_list'
54

65
module CC::Engine::Analyzers
76
RSpec.describe Base, in_tmpdir: true do

spec/cc/engine/analyzers/java/java_spec.rb

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require "spec_helper"
22
require "cc/engine/analyzers/java/main"
33
require "cc/engine/analyzers/engine_config"
4-
require "cc/engine/analyzers/file_list"
54

65
module CC::Engine::Analyzers
76
RSpec.describe Java::Main, in_tmpdir: true do

0 commit comments

Comments
 (0)