Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

feat: project specific configuration #411

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor: removed useBundler & config class, naming conventions and more
- useBundler is not needed anymore, if we want tu run rubocop with bundler we should put it on the command line.
- The Config class is a good overprogramming example. It is removed in favour of CommandBuilder, more simple and cool.
- Drunken camel case for helpers.
- Utility functions extracted to helpers.
- SRP aproximation for Rubocop class, we explicitly pass arguments like current directory, etc...
  • Loading branch information
vzamanillo committed Apr 28, 2020
commit 46669ce6612353da359a2b30a057afff5160507a
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
"default": false,
"description": "Only run linter if a RuboCop config file is found somewhere in the path for the current file."
},
"useBundler": {
"type": "boolean",
"default": false,
"description": "Use `bundler` to execute Rubocop."
},
"detectProjectSettings": {
"type": "boolean",
"title": "Detect linter-rubocop specific project file.",
Expand Down
7 changes: 7 additions & 0 deletions src/helpers/currentDirectory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use babel'

import path from 'path'

export default function currentDirectory(filePath) {
return atom.project.relativizePath(filePath)[0] || path.dirname(filePath)
}
Empty file modified src/helpers/deserializeProjectFile.js
100644 → 100755
Empty file.
File renamed without changes.
9 changes: 6 additions & 3 deletions src/index.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import { CompositeDisposable } from 'atom'
import Rubocop from './rubocop/Rubocop'
import hasValidScope from './helpers/scope-validator'
import hasValidScope from './helpers/hasValidScope'
import deserializeProjectFile from './helpers/deserializeProjectFile'
import currentDirectory from './helpers/currentDirectory'

const PROJECT_CONFIG_FILE = '.lrproject-config.json'

Expand Down Expand Up @@ -121,7 +122,9 @@ export default {
if (text.length === 0) {
return
}
this.rubocop.autocorrect(editor.getPath(), onSave)

const filePath = editor.getPath()
this.rubocop.autocorrect(currentDirectory(filePath), filePath, onSave)
},

provideLinter() {
Expand All @@ -143,7 +146,7 @@ export default {
}
}

return this.rubocop.analyze(editor.getText(), filePath)
return this.rubocop.analyze(editor.getText(), currentDirectory(filePath), filePath)
},
}
},
Expand Down
22 changes: 22 additions & 0 deletions src/rubocop/CommandBuilder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use babel'

const DEFAULT_ARGS = [
'--force-exclusion',
'--format', 'json',
'--display-style-guide',
'--cache', 'false',
]

class CommandBuilder {
static build(baseCommand, args) {
if (baseCommand.length !== 0) {
return baseCommand.split(/\s+/)
.filter((i) => i)
.concat(DEFAULT_ARGS)
.concat(args)
}
return null
}
}

module.exports = CommandBuilder
83 changes: 0 additions & 83 deletions src/rubocop/Config.js

This file was deleted.

42 changes: 20 additions & 22 deletions src/rubocop/Rubocop.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import { findAsync } from 'atom-linter'
import pluralize from 'pluralize'
import path from 'path'
import parseFromStd from '../helpers/std-parser'
import Config from './Config'
import parseFromStd from '../helpers/parseFromStd'

import CommandBuilder from './CommandBuilder'
import Runner from './Runner'
import ErrorFormatter from '../ErrorFormatter'
import OffenseFormatter from './OffenseFormatter'
import ErrorFormatter from './formatters/ErrorFormatter'
import OffenseFormatter from './formatters/OffenseFormatter'

const CONFIG_FILE = '.rubocop.yml'

Expand All @@ -16,35 +16,33 @@ const UNEXPECTED_ERROR_MSG = 'Rubocop: Unexpected error'
const UNDEF_VERSION_ERROR_MSG = 'Unable to get rubocop version from linting output results.'
const NO_FIXES_INFO_MSG = 'Linter-Rubocop: No fixes were made'

const configFileFound = Symbol('configFileFound')

function currentDirectory(filePath) {
return atom.project.relativizePath(filePath)[0] || path.dirname(filePath)
}

class Rubocop {
constructor(config = {}) {
this.config = new Config(config)
constructor(config) {
this.config = config
this.offenseFormatter = new OffenseFormatter()
this.errorFormatter = new ErrorFormatter()
}

async [configFileFound](filePath) {
setConfig(newConfig) {
this.config = newConfig
}

async configFileExists(filePath) {
if (this.config.disableWhenNoConfigFile === true) {
return await findAsync(filePath, CONFIG_FILE) !== null
}
return true
}

async autocorrect(filePath, onSave = false) {
if (!filePath || !await this[configFileFound](filePath)) {
async autocorrect(cwd, filePath, onSave = false) {
if (!filePath || !await this.configFileExists(filePath)) {
return
}

try {
const output = await Runner.runSync(
currentDirectory(filePath),
this.config.baseCommand.concat(['--auto-correct', filePath]),
cwd,
CommandBuilder.build(this.config.command, ['--auto-correct', filePath]),
)
try {
const {
Expand Down Expand Up @@ -76,15 +74,15 @@ class Rubocop {
}
}

async analyze(text, filePath) {
if (!filePath || !await this[configFileFound](filePath)) {
async analyze(text, cwd, filePath) {
if (!filePath || !await this.configFileExists(filePath)) {
return null
}

try {
const output = await Runner.run(
currentDirectory(filePath),
this.config.baseCommand.concat(['--stdin', filePath]),
cwd,
CommandBuilder.build(this.config.command, ['--stdin', filePath]),
{ stdin: text },
)
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use babel'

import { satisfies } from 'semver'
import getRuleDocumentation from './helpers/doc-cache'
import getRuleDocumentation from '../helpers/getRuleDocumentation'

import ErrorFormatter from '../ErrorFormatter'
import ErrorFormatter from './ErrorFormatter'

const SEVERITY_MAPPING = {
refactor: 'info',
Expand Down