Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature eslint warnings #31

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Dockerfile

This file was deleted.

2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source 'https://rubygems.org'

gemspec

gem 'mumukit-inspection', github: 'mumuki/mumukit-inspection', branch: 'feature-eslint-warnings'
29 changes: 29 additions & 0 deletions lib/expectations_hook.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
class JavascriptExpectationsHook < Mumukit::Templates::MulangExpectationsHook
include_smells true

ESLINT_RULES = {
'semi' => 'JavaScript#LacksOfEndingSemicolon'
}.freeze

def run!(spec)
super(spec) + run_eslint(spec[:request][:content])
end

def run_eslint(content)
lines = content.lines.map(&:rstrip)
out, status = Open3.capture2("eslint --format json --stdin", stdin_data: content)
result = JSON.parse(out)[0]

if result["fatalErrorCount"] == 0
result["messages"].map do |it|
{
expectation: {
binding: lines[it["line"] - 1],
inspection: ESLINT_RULES[it["ruleId"]]
},
result: false
}
end
else
[]
end

end

def language
'JavaScript'
end
Expand Down
4 changes: 3 additions & 1 deletion lib/javascript_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Mumukit.runner_name = 'javascript'
Mumukit.configure do |config|
config.docker_image = 'mumuki/mumuki-mocha-worker:2.0'
config.docker_image = 'mumuki/mumuki-mocha-worker:2.1'
config.structured = true
config.stateful = true
config.content_type = 'markdown'
Expand All @@ -18,3 +18,5 @@
require_relative './feedback_hook'
require_relative './query_hook'
require_relative './try_hook'

Mulang::Inspection.register_extension! Mumukit::Inspection::JavaScript
1 change: 1 addition & 0 deletions mumuki-javascript-runner.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']

spec.add_dependency 'mumukit', '~> 2.40'
spec.add_dependency 'mumukit-inspection', '~> 6.0'

spec.add_development_dependency 'bundler', '~> 2.0'
spec.add_development_dependency 'rake', '~> 10.0'
Expand Down
19 changes: 18 additions & 1 deletion spec/expectations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def compile_and_run(request)

let(:runner) { JavascriptExpectationsHook.new }
let(:result) { compile_and_run(req(expectations, code)) }
let(:translations) { result.select { |it| !it[:result] }.map { |it| Mulang::Expectation.parse(it[:expectation]).translate } }

describe 'HasTooShortIdentifiers' do
let(:code) { "function f(x) { retun g(x); }" }
Expand All @@ -20,7 +21,7 @@ def compile_and_run(request)
end

describe 'HasWrongCaseIdentifiers' do
let(:code) { "function a_function_with_bad_case() { return 3 }" }
let(:code) { "function a_function_with_bad_case() { return 3; }" }
let(:expectations) { [] }

it { expect(result).to eq [{expectation: {binding: 'a_function_with_bad_case', inspection: 'HasWrongCaseIdentifiers'}, result: false}] }
Expand Down Expand Up @@ -86,4 +87,20 @@ def compile_and_run(request)
{expectation: expectations[2], result: false}] }
end

describe "eslint smells" do
let(:expectations) { [] }

describe 'JavaScript#LacksOfEndingSemicolon' do
let(:code) { "let variable1 = 1\nlet variable2 = 2;\nlet variable3 = 3" }

it { expect(result).to eq [
{expectation: {binding: "let variable1 = 1", inspection: 'JavaScript#LacksOfEndingSemicolon'}, result: false},
{expectation: {binding: 'let variable3 = 3', inspection: 'JavaScript#LacksOfEndingSemicolon'}, result: false}] }
it { expect(translations).to eq [
"the following line should end with <code>;</code>: <pre><code>let variable1 = 1</code></pre>",
"the following line should end with <code>;</code>: <pre><code>let variable3 = 3</code></pre>"
] }
end
end

end
28 changes: 14 additions & 14 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
it 'answers a valid hash when submission is ok' do
response = bridge.run_tests!(test: 'describe("foo", () => it("bar", () => assert.equal(aVariable, 3)))',
extra: '',
content: 'let aVariable = 3',
content: 'let aVariable = 3;',
expectations: [])

expect(response).to eq(response_type: :structured,
Expand All @@ -50,7 +50,7 @@
it 'answers a valid hash when submission is ok but has part of blacklisted words' do
response = bridge.run_tests!(test: 'describe("foos", () => it("bar", () => assert.equal(aVariable, 3)))',
extra: 'let flos = 75;',
content: 'let aVariable = 3',
content: 'let aVariable = 3;',
expectations: [])

expect(response).to eq(response_type: :structured,
Expand All @@ -64,7 +64,7 @@
it 'answers a valid hash when submission is ok with warnings' do
response = bridge.run_tests!(test: 'describe("foo", () => it("bar", () => assert.equal(x, 3)))',
extra: '',
content: 'let x = 3',
content: 'let x = 3;',
expectations: [])

expect(response).to eq(response_type: :structured,
Expand All @@ -78,7 +78,7 @@
it 'answers a valid hash when submission is ok with JS-specific warnings' do
response = bridge.run_tests!(test: 'describe("foo", () => it("bar", () => assert.equal(something, 3)))',
extra: '',
content: 'var something = 3',
content: 'var something = 3;',
expectations: [])

expect(response).to eq(response_type: :structured,
Expand All @@ -93,7 +93,7 @@
response = bridge.
run_tests!(test: 'describe("foo", () => it("bar", () => assert.equal(aVariable, 3)))',
extra: '',
content: 'let aVariable = 2',
content: 'let aVariable = 2;',
expectations: [])

expect(response).to eq(response_type: :structured,
Expand All @@ -109,7 +109,7 @@
response = bridge.
run_tests!(test: 'describe("foo", () => it("bar", function (done) { this.timeout(5000); setTimeout(() => { assert.equal(x, 3); done() }, 5000) }))',
extra: '',
content: 'let x = 2',
content: 'let x = 2;',
expectations: [])

expect(response).to eq(response_type: :unstructured,
Expand Down Expand Up @@ -138,7 +138,7 @@
it 'answers a valid hash when given a known locale' do
response = bridge.run_tests!(test: 'describe("foo", () => it("bar", () => assert.equal(aVariable, 4)))',
extra: '',
content: 'let aVariable = 3',
content: 'let aVariable = 3;',
expectations: [],
locale: 'pt')

Expand All @@ -164,7 +164,7 @@
});
},
extra: '',
content: 'function average(list) { return sum(list) / list.length }',
content: 'function average(list) { return sum(list) / list.length; }',
expectations: [])

expect(response).to eq(response_type: :structured,
Expand Down Expand Up @@ -194,9 +194,9 @@
},
extra: '',
content: %q{
function resumenLector(quien) {
return quien.nombre + " se suscribió hace " + (2020 - quien.anioSuscripcion) + " años";
}},
function resumenLector(quien) {
return quien.nombre + " se suscribió hace " + (2020 - quien.anioSuscripcion) + " años";
}},
expectations: [])

expect(response).to eq(response_type: :structured,
Expand Down Expand Up @@ -244,9 +244,9 @@
},
extra: 'function longitud(unStringOLista) { return unStringOLista.length;}',
content: %q{
function resumenSuscripcion(registro) {
return registro.nombre + " " + "se suscribio hace " + ( 2020 - registro.anioSuscripcion) + " " + "años" + " " + "y leyo " + " " + longitud(registro.librosLeidos) + " " + "libros";
}},
function resumenSuscripcion(registro) {
return registro.nombre + " " + "se suscribio hace " + ( 2020 - registro.anioSuscripcion) + " " + "años" + " " + "y leyo " + " " + longitud(registro.librosLeidos) + " " + "libros";
}},
expectations: [])

expect(response).to eq(response_type: :structured,
Expand Down
9 changes: 9 additions & 0 deletions worker/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
env:
browser: true
es2021: true
parserOptions:
ecmaVersion: 12
rules:
semi:
- error
- always
5 changes: 5 additions & 0 deletions worker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM node:6.14
MAINTAINER Federico Scarpa

COPY .eslintrc.yml .eslintrc.yml
RUN npm install -g [email protected] [email protected] [email protected]