Skip to content

Commit

Permalink
See #18. Most of the language tests are successful.
Browse files Browse the repository at this point in the history
Tests that start with underscores (_) are hidden and not tested.
Those must eventually be solved.
  • Loading branch information
Glavin001 committed Dec 28, 2014
1 parent f8e0e44 commit bc99f6c
Show file tree
Hide file tree
Showing 32 changed files with 234 additions and 36 deletions.
2 changes: 1 addition & 1 deletion examples/nested-jsbeautifyrc/css/expected/test.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
p {
color: red;
}
}
2 changes: 1 addition & 1 deletion examples/nested-jsbeautifyrc/html/expected/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ <h1>Hello</h1>
</p>
</body>

</html>
</html>
2 changes: 1 addition & 1 deletion examples/nested-jsbeautifyrc/javascript/expected/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function hell() {
console.log('world');
}
}
1 change: 1 addition & 0 deletions examples/nested-jsbeautifyrc/markdown/expected/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
1. one
2. two
3. three

2 changes: 1 addition & 1 deletion examples/simple-jsbeautifyrc/css/expected/test.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
p {
color: red;
}
}
2 changes: 1 addition & 1 deletion examples/simple-jsbeautifyrc/handlebars/expected/test.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
content
{{/if}}
{{/each}}
{{/if}}
{{/if}}
2 changes: 1 addition & 1 deletion examples/simple-jsbeautifyrc/html/expected/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ <h1>Hello</h1>
</p>
</body>

</html>
</html>
2 changes: 1 addition & 1 deletion examples/simple-jsbeautifyrc/javascript/expected/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function hell() {
console.log('world');
}
}
2 changes: 1 addition & 1 deletion examples/simple-jsbeautifyrc/javascript/expected/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
/* test *
"jslint_happy": true
}
*/
*/
2 changes: 1 addition & 1 deletion examples/simple-jsbeautifyrc/javascript/expected/test2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Q($.ajax(...))
.catch(function (response) {
console.error(response);
});
});
2 changes: 1 addition & 1 deletion examples/simple-jsbeautifyrc/less/expected/test.less
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ a {
background-color: @green;
}
}
}
}
2 changes: 1 addition & 1 deletion examples/simple-jsbeautifyrc/less/expected/test2.less
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,4 @@
opacity: 0;
display: none;
}
}
}
2 changes: 1 addition & 1 deletion examples/simple-jsbeautifyrc/less/expected/test3.less
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
// Ok
.mixin(@variable) {

}
}
2 changes: 1 addition & 1 deletion examples/simple-jsbeautifyrc/less/expected/test4.less
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ div.photo-view {
display: block;
}
}
}
}
2 changes: 1 addition & 1 deletion examples/simple-jsbeautifyrc/less/expected/test5.less
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
&:hover {
color: green;
}
}
}
1 change: 1 addition & 0 deletions examples/simple-jsbeautifyrc/markdown/expected/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
1. one
2. two
3. three

Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@

{{#empty}}
<p>The list is empty.</p>
{{/empty}}
{{/empty}}
2 changes: 1 addition & 1 deletion examples/simple-jsbeautifyrc/sass/expected/alphasort1.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// Sets base apple color to red
.apple-color(@color-green);
// Sets apple color to green
}
}
2 changes: 1 addition & 1 deletion examples/simple-jsbeautifyrc/sass/expected/test.sass
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ a {
&:hover {
color: blue;
}
}
}
2 changes: 1 addition & 1 deletion examples/simple-jsbeautifyrc/sass/expected/test.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
}
}
}
}
}
2 changes: 1 addition & 1 deletion examples/simple-jsbeautifyrc/typescript/expected/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ module TestModule {
}
}
export class B extends A {
}
}
};
194 changes: 194 additions & 0 deletions lib/options.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
# Lazy loaded dependencies
_ = require("lodash")
path = require("path")
fs = null
strip = null
yaml = null
editorconfig = null
beautifier = require("./language-options")
languages = beautifier.languages
defaultLanguageOptions = beautifier.defaultLanguageOptions

module.exports =
findFileResults: {}

# CLI
getUserHome: ->
process.env.HOME or process.env.HOMEPATH or process.env.USERPROFILE

verifyExists: (fullPath) ->
fs ?= require("fs")
(if fs.existsSync(fullPath) then fullPath else null)

# Storage for memoized results from find file
# Should prevent lots of directory traversal &
# lookups when liniting an entire project

###
Searches for a file with a specified name starting with
'dir' and going all the way up either until it finds the file
or hits the root.
@param {string} name filename to search for (e.g. .jshintrc)
@param {string} dir directory to start search from (default:
current working directory)
@param {boolean} upwards should recurse upwards on failure? (default: true)
@returns {string} normalized filename
###
findFile: (name, dir, upwards=true) ->
path ?= require("path")
dir = dir or process.cwd()
filename = path.normalize(path.join(dir, name))
return @findFileResults[filename] if @findFileResults[filename] isnt `undefined`
parent = path.resolve(dir, "../")
if @verifyExists(filename)
@findFileResults[filename] = filename
return filename
if dir is parent
@findFileResults[filename] = null
return null
if upwards
findFile name, parent
else
return null

###
Tries to find a configuration file in either project directory
or in the home directory. Configuration files are named
'.jsbeautifyrc'.
@param {string} config name of the configuration file
@param {string} file path to the file to be linted
@param {boolean} upwards should recurse upwards on failure? (default: true)
@returns {string} a path to the config file
###
findConfig: (config, file, upwards=true) ->
path ?= require("path")
dir = path.dirname(path.resolve(file))
envs = @getUserHome()
home = path.normalize(path.join(envs, config))
proj = @findFile(config, dir, upwards)
return proj if proj
return home if @verifyExists(home)
null
getConfigOptionsFromSettings: (langs) ->
config = atom.config.get('atom-beautify')
options = {}
# console.log(langs, config);
# Iterate over keys of the settings
_.every _.keys(config), (k) ->
# Check if keys start with a language
p = k.split("_")[0]
idx = _.indexOf(langs, p)
# console.log(k, p, idx);
if idx >= 0
# Remove the language prefix and nest in options
lang = langs[idx]
opt = k.replace(new RegExp("^" + lang + "_"), "")
options[lang] = options[lang] or {}
options[lang][opt] = config[k]
# console.log(lang, opt);
true
# console.log(options);
options


# Look for .jsbeautifierrc in file and home path, check env variables
getConfig: (startPath, upwards=true) ->
# Verify that startPath is a string
startPath = (if (typeof startPath is "string") then startPath else "")
return {} unless startPath
# Get the path to the config file
configPath = @findConfig(".jsbeautifyrc", startPath, upwards)
externalOptions = undefined
if configPath
fs ?= require("fs")
contents = fs.readFileSync(configPath,
encoding: "utf8"
)
unless contents
externalOptions = {}
else
try
strip ?= require("strip-json-comments")
externalOptions = JSON.parse(strip(contents))
catch e
console.log "Failed parsing config as JSON: " + configPath
# Attempt as YAML
try
yaml ?= require("js-yaml")
externalOptions = yaml.safeLoad(contents)
catch e
console.log "Failed parsing config as YAML: " + configPath
externalOptions = {}
else
externalOptions = {}
externalOptions

getOptionsForPath: (editedFilePath, editor) ->

# Editor Options
editorOptions = {}
if editor?
# Get current Atom editor configuration
isSelection = !!editor.getSelectedText()
softTabs = editor.softTabs
tabLength = editor.getTabLength()
editorOptions =
indent_size: (if softTabs then tabLength else 1)
indent_char: (if softTabs then " " else "\t")
indent_with_tabs: not softTabs

# From Package Settings
configOptions = @getConfigOptionsFromSettings(languages)

# Get configuration in User's Home directory
userHome = @getUserHome()
# FAKEFILENAME forces `path` to treat as file path and it's parent directory
# is the userHome. See implementation of findConfig
# and how path.dirname(DIRECTORY) returns the parent directory of DIRECTORY
homeOptions = @getConfig(path.join(userHome,"FAKEFILENAME"), false)

if editedFilePath?
# Handle EditorConfig options
# http://editorconfig.org/
editorconfig ?= require('editorconfig');
editorConfigOptions = editorconfig.parse(editedFilePath);
# Transform EditorConfig to Atom Beautify's config structure and naming
if editorConfigOptions.indent_style is 'space'
editorConfigOptions.indent_char = " "
# if (editorConfigOptions.indent_size)
# editorConfigOptions.indent_size = config.indent_size
else if editorConfigOptions.indent_style is 'tab'
editorConfigOptions.indent_char = "\t"
editorConfigOptions.indent_with_tabs = true
if (editorConfigOptions.tab_width)
editorConfigOptions.indent_size = editorConfigOptions.tab_width

# Get all options in configuration files from this directory upwards to root
projectOptions = []
p = path.dirname(editedFilePath)
# Check if p is root (top directory)
while p isnt path.resolve(p,"../")
# Get config for p
pf = path.join(p, "FAKEFILENAME")
pc = @getConfig(pf, false)
# Add config for p to project's config options
projectOptions.push(pc)
# console.log p, pc
# Move upwards
p = path.resolve(p,"../")
else
editorConfigOptions = {}
projectOptions = []

# Combine all options together
allOptions = [
editorOptions
configOptions
homeOptions
editorConfigOptions
]
allOptions = allOptions.concat(projectOptions)
Loading

0 comments on commit bc99f6c

Please sign in to comment.