Commit 52cb6b34 authored by irekg's avatar irekg

Convert code to prettier plugin and support options

parent 4e8979be
......@@ -221,3 +221,18 @@ pon, 20 sty 2020, 12:03:57 CET
Nareszcie mogłem wypróbować kod na dużej ilości JSDocków. Generalnie
super. Były problemy z zawijaniem wierszy i formatowaniem kodu
w @example, ale już jest ok.
pon, 20 sty 2020, 13:38:03 CET
Odnoszę wrażenie, że opcje same zaczną działać jeśli tylko parser
będzie używany w formie dodatku. Dlatego spróbuję zamienić to na
plugin. Byłoby super jakby zadziałało.
pon, 20 sty 2020, 14:13:07 CET
O kurde. Chyba zaskoczyło. Testy przeszły. Ale super. Ciekawe czy
opcje też działają.
pon, 20 sty 2020, 14:16:12 CET
DZIAŁAJĄ!!! Super to commit i można podpinać pod projekt i Emacsa.
const cwd = process.cwd()
const path = require('path')
const doctrine = require('doctrine')
const prettier = require('prettier')
// TODO This also could be taken from options.
const printWidth = 80
const babelFlow = require(path.join(cwd, 'node_modules/prettier/parser-babylon.js')).parsers['babel-flow']
const tagSynonyms = {
// One TAG TYPE can have different titles called SYNONYMS. We want
......@@ -67,7 +67,8 @@ function getTagOrderWeight(tagTitle) {
'see' : 3,
'description' : 4,
'examples' : 5,
// Evertthing else will have 6
// TODO everything else could be sorted alphabetically
// Everything else will have 6
'param' : 7,
'return' : 8,
}
......@@ -75,13 +76,12 @@ function getTagOrderWeight(tagTitle) {
}
/** {@link https://prettier.io/docs/en/api.html#custom-parser-api} */
module.exports = function jsdocParser(text, parsers, options) {
function jsdocParser(text, parsers, options) {
const ast = parsers['babel-flow'](text)
if (!options.jsdoc) options.jsdoc = { spaces: 1 }
if (!options.jsdoc.spaces) options.jsdoc.spaces = 1
const gap = ' '.repeat(options.jsdoc.spaces)
// Options
const gap = ' '.repeat(options.jsdocSpaces)
const printWidth = options.jsdocPrintWidth
ast.comments.forEach(comment => {
// Parse only comment blocks
......@@ -125,6 +125,7 @@ module.exports = function jsdocParser(text, parsers, options) {
}
}
// Additional operations on tag.name
if (tag.name) {
// Figure out if tag type have default value
const part1 = commentString.split(tag.name)[1]
......@@ -138,7 +139,7 @@ module.exports = function jsdocParser(text, parsers, options) {
}
}
if (!['example', 'memberof'].includes(tag.title))
if (['description', 'param', 'return'].includes(tag.title))
tag.description = formatDescription(tag.description)
if (!tag.description && ['description', 'param', 'return', 'memberof'].includes(tag.title))
......@@ -205,3 +206,32 @@ module.exports = function jsdocParser(text, parsers, options) {
return ast
}
// jsdoc-parser
module.exports = {
languages: [{
name: 'JavaScript',
parsers: ['jsdoc-parser'],
}],
parsers: {
'jsdoc-parser': Object.assign({}, babelFlow, { parse: jsdocParser })
},
options: {
jsdocSpaces: {
type: 'int',
category: 'Global',
default: 1,
description: 'SPACES SPACES',
},
jsdocPrintWidth: {
type: 'int',
category: 'Global',
default: 80,
description: 'wrap wrap wrap',
}
},
defaultOptions: {
jsdocSpaces: 1,
jsdocPrintWidth: 80,
}
}
This diff is collapsed.
......@@ -4,7 +4,12 @@ const path = require('path')
const jsdocParser = require(path.join(cwd, 'jsdoc_parser'))
function subject(code, options = {}) {
return prettier.format(code, Object.assign(options, { parser: jsdocParser }))
return prettier.format(code, {
parser: 'jsdoc-parser',
plugins: ['.'],
jsdocSpaces: 1,
jsdocPrintWidth: 80
});
}
test('JS code should be formatted as usuall', () => {
......@@ -67,12 +72,12 @@ const testFunction = (text, defaultValue, optionalNumber) => true
* with a dot.
*
* @example
* var one = 5;
* var two = 10;
* var one = 5;
* var two = 10;
*
* if (one > 2) {
* two += one;
* }
* if (one > 2) {
* two += one;
* }
*
* @param {String|Number} text Some text description that is very long and needs
* to be wrapped.
......
* Golden Hour
:LOGBOOK:
CLOCK: [2020-01-20 pon 13:37]--[2020-01-20 pon 14:23] => 0:46
CLOCK: [2020-01-20 pon 11:17]--[2020-01-20 pon 12:02] => 0:45
CLOCK: [2020-01-19 nie 18:38]--[2020-01-19 nie 19:20] => 0:42
CLOCK: [2020-01-19 nie 15:55]--[2020-01-19 nie 16:38] => 0:43
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment