Skip to content

Commit

Permalink
Merge pull request #30 from unexpectedjs/feature/prettier
Browse files Browse the repository at this point in the history
Add prettier setup
  • Loading branch information
papandreou committed Jan 1, 2019
2 parents 76e57c7 + 1316d99 commit 9538182
Show file tree
Hide file tree
Showing 18 changed files with 1,343 additions and 1,164 deletions.
11 changes: 5 additions & 6 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

root = true

[*]
end_of_line = lf
charset = utf-8
insert_final_newline = true
indent_size = 4
trim_trailing_whitespace = true
indent_style = space

[Makefile]
indent_style = tab

[*.json]
indent_size = 2
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
testdata
node_modules
coverage
21 changes: 14 additions & 7 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
module.exports = {
extends: [
'onelint'
],
env: {
es6: false
}
const config = {
extends: ['pretty-standard']
};

if (process.stdin.isTTY) {
// Enable plugin-prettier when running in a terminal. Allows us to have
// eslint verify prettier formatting, while not being bothered by it in our
// editors.
config.plugins = config.plugins || [];
config.plugins.push('prettier');
config.rules = config.rules || {};
config.rules['prettier/prettier'] = 'error';
}

module.exports = config;
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package.json
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
67 changes: 35 additions & 32 deletions lib/Snippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,49 @@ var evaluateSnippets = require('./evaluateSnippets');
var passError = require('passerror');

function Snippets(snippets) {
this.items = snippets;
this.items = snippets;
}

Snippets.prototype.get = function (index) {
return this.items[index];
Snippets.prototype.get = function(index) {
return this.items[index];
};

Snippets.prototype.getTests = function () {
var tests = [];
var evaluatedExampleIndex;
this.items.forEach(function (snippet, index) {
var flags = snippet.flags;
Snippets.prototype.getTests = function() {
var tests = [];
var evaluatedExampleIndex;
this.items.forEach(function(snippet, index) {
var flags = snippet.flags;

switch (snippet.lang) {
case 'javascript':
if (flags.evaluate) {
evaluatedExampleIndex = index;
tests.push({
code: snippet.code,
flags: flags
});
}
break;
case 'output':
if (evaluatedExampleIndex === index - 1) {
tests[tests.length - 1].output = snippet.code;
}
break;
switch (snippet.lang) {
case 'javascript':
if (flags.evaluate) {
evaluatedExampleIndex = index;
tests.push({
code: snippet.code,
flags: flags
});
}
});
break;
case 'output':
if (evaluatedExampleIndex === index - 1) {
tests[tests.length - 1].output = snippet.code;
}
break;
}
});

return tests;
return tests;
};


module.exports = {
fromMarkdown: function (markdown, options, cb) {
var snippets = extractSnippets(markdown);
evaluateSnippets(snippets, options, passError(cb, function () {
cb(null, new Snippets(snippets));
}));
}
fromMarkdown: function(markdown, options, cb) {
var snippets = extractSnippets(markdown);
evaluateSnippets(
snippets,
options,
passError(cb, function() {
cb(null, new Snippets(snippets));
})
);
}
};
193 changes: 106 additions & 87 deletions lib/UnexpectedMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,122 +13,141 @@ var maps = {};

// Make the md-to-mocha transpiler avaliable as a side effect of requiring unexpected-markdown
// so that mocha --compilers md:unexpected-markdown will work:
require.extensions['.md'] = function (module, fileName) {
var sourceMapWithCode = convertMarkdownToMocha(fs.readFileSync(fileName, 'utf-8'), fileName);
require.extensions['.md'] = function(module, fileName) {
var sourceMapWithCode = convertMarkdownToMocha(
fs.readFileSync(fileName, 'utf-8'),
fileName
);

var absoluteFileName = pathModule.resolve(process.cwd(), fileName);
var absoluteFileName = pathModule.resolve(process.cwd(), fileName);

// Register the source map with the main UnexpectedMarkdown, which tries to detect the source-map-support
// module and registers a handler with it:
var map = sourceMapWithCode.map.toString();
var code = sourceMapWithCode.code;
// Register the source map with the main UnexpectedMarkdown, which tries to detect the source-map-support
// module and registers a handler with it:
var map = sourceMapWithCode.map.toString();
var code = sourceMapWithCode.code;

maps[absoluteFileName] = map;
maps[absoluteFileName] = map;

module._compile(code, fileName);
module._compile(code, fileName);
};

var sourceMapSupport;
try {
sourceMapSupport = require.main.require('source-map-support');
sourceMapSupport = require.main.require('source-map-support');
} catch (e) {
sourceMapSupport = require('source-map-support');
sourceMapSupport = require('source-map-support');
}

if (sourceMapSupport) {
sourceMapSupport.install({
handleUncaughtExceptions: false,
retrieveSourceMap: function (source) {
return maps[source] ? { url: null, map: maps[source] } : null;
}
});
sourceMapSupport.install({
handleUncaughtExceptions: false,
retrieveSourceMap: function(source) {
return maps[source] ? { url: null, map: maps[source] } : null;
}
});
}

var styleRegex = /style=".*?"/;

function UnexpectedMarkdown(content) {
if (!(this instanceof UnexpectedMarkdown)) {
return new UnexpectedMarkdown(content);
}
this.content = content;
if (!(this instanceof UnexpectedMarkdown)) {
return new UnexpectedMarkdown(content);
}
this.content = content;
}

UnexpectedMarkdown.prototype.toHtml = function (options, cb) {
var content = this.content;
var expect = createExpect(options);
this.getSnippets(options, passError(cb, function (snippets) {
var index = 0;
var renderer = new marked.Renderer();
renderer.code = function (code) {
var snippet = snippets.get(index);
var previousSnippet = snippets.get(index - 1);
index += 1;
var lang = snippet.lang;
if (lang === 'output') {
if (previousSnippet && previousSnippet.lang === 'javascript') {
if (previousSnippet.htmlErrorMessage) {
return previousSnippet.htmlErrorMessage
.replace(styleRegex, 'class="output"');
}

return '<div class="output"></div>';
} else {
throw new Error('No matching javascript block for output:\n' + snippet.code);
}
UnexpectedMarkdown.prototype.toHtml = function(options, cb) {
var content = this.content;
var expect = createExpect(options);
this.getSnippets(
options,
passError(cb, function(snippets) {
var index = 0;
var renderer = new marked.Renderer();
renderer.code = function(code) {
var snippet = snippets.get(index);
var previousSnippet = snippets.get(index - 1);
index += 1;
var lang = snippet.lang;
if (lang === 'output') {
if (previousSnippet && previousSnippet.lang === 'javascript') {
if (previousSnippet.htmlErrorMessage) {
return previousSnippet.htmlErrorMessage.replace(
styleRegex,
'class="output"'
);
}

return expect.output.clone('html').code(code, lang).toString()
.replace(styleRegex, 'class="code ' + this.options.langPrefix + lang + '"');
};

try {
cb(null, marked(content, extend({ renderer: renderer }, options)));
} catch (err) {
cb(err);
return '<div class="output"></div>';
} else {
throw new Error(
'No matching javascript block for output:\n' + snippet.code
);
}
}
}));

return expect.output
.clone('html')
.code(code, lang)
.toString()
.replace(
styleRegex,
'class="code ' + this.options.langPrefix + lang + '"'
);
};

try {
cb(null, marked(content, extend({ renderer: renderer }, options)));
} catch (err) {
cb(err);
}
})
);
};

UnexpectedMarkdown.prototype.getSnippets = function (options, cb) {
var that = this;
if (this.snippets) {
cb(null, this.snippets);
} else {
// eslint-disable-next-line handle-callback-err
Snippets.fromMarkdown(this.content, options, function (err, snippets) {
that.snippets = snippets;
cb(null, snippets);
});
}
UnexpectedMarkdown.prototype.getSnippets = function(options, cb) {
var that = this;
if (this.snippets) {
cb(null, this.snippets);
} else {
// eslint-disable-next-line handle-callback-err
Snippets.fromMarkdown(this.content, options, function(err, snippets) {
that.snippets = snippets;
cb(null, snippets);
});
}
};

UnexpectedMarkdown.prototype.withUpdatedExamples = function (options, cb) {
var content = this.content;
this.getSnippets(options, passError(cb, function (snippets) {
var index = 0;
var updateContent = content.replace(snippetRegexp, function ($0, lang) {
var currentIndex = index;
index += 1;
var snippet = snippets.get(currentIndex);
if (snippet.lang === 'output') {
var example = snippets.get(currentIndex - 1);
var output = '';
if (example && example.lang === 'javascript') {
if (example.errorMessage) {
output = example.errorMessage;
}
}
return '```' + lang + '\n' + output + '\n```';
} else {
return $0;
UnexpectedMarkdown.prototype.withUpdatedExamples = function(options, cb) {
var content = this.content;
this.getSnippets(
options,
passError(cb, function(snippets) {
var index = 0;
var updateContent = content.replace(snippetRegexp, function($0, lang) {
var currentIndex = index;
index += 1;
var snippet = snippets.get(currentIndex);
if (snippet.lang === 'output') {
var example = snippets.get(currentIndex - 1);
var output = '';
if (example && example.lang === 'javascript') {
if (example.errorMessage) {
output = example.errorMessage;
}
});
cb(null, new UnexpectedMarkdown(updateContent));
}));
}
return '```' + lang + '\n' + output + '\n```';
} else {
return $0;
}
});
cb(null, new UnexpectedMarkdown(updateContent));
})
);
};

UnexpectedMarkdown.prototype.toString = function () {
return this.content;
UnexpectedMarkdown.prototype.toString = function() {
return this.content;
};

module.exports = UnexpectedMarkdown;
Loading

0 comments on commit 9538182

Please sign in to comment.