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

feat(support-info): add options field #3433

Merged
merged 17 commits into from Dec 31, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
170 changes: 31 additions & 139 deletions src/common/support.js
@@ -1,18 +1,16 @@
"use strict";

const util = require("./util");
const dedent = require("dedent");
const semver = require("semver");
const currentVersion = require("../../package.json").version;
const loadPlugins = require("./load-plugins");

const CATEGORY_GLOBAL = "Global";
const CATEGORY_JAVASCRIPT = "JavaScript";
const CATEGORY_MARKDOWN = "Markdown";
const CATEGORY_SPECIAL = "Special";

/**
* @typedef {Object} OptionInfo
* @property {string} name
* @property {string} since - available since version
* @property {string} category
* @property {'int' | 'boolean' | 'choice' | 'path'} type
Expand Down Expand Up @@ -43,37 +41,9 @@ const CATEGORY_SPECIAL = "Special";
* @property {string?} deprecated - deprecated since version
* @property {OptionValueInfo?} redirect - redirect deprecated value
*/
/** @type {OptionInfo[]} */
const supportOptions = [
{
name: "arrowParens",
since: "1.9.0",
category: CATEGORY_JAVASCRIPT,
type: "choice",
default: "avoid",
description: "Include parentheses around a sole arrow function parameter.",
choices: [
{
value: "avoid",
description: "Omit parens when possible. Example: `x => x`"
},
{
value: "always",
description: "Always include parens. Example: `(x) => x`"
}
]
},
{
name: "bracketSpacing",
since: "0.0.0",
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: true,
description: "Print spaces between brackets.",
oppositeDescription: "Do not print spaces between brackets."
},
{
name: "cursorOffset",
/** @type {{ [name: string]: OptionInfo } */
const supportOptions = {
cursorOffset: {
since: "1.4.0",
category: CATEGORY_SPECIAL,
type: "int",
Expand All @@ -84,33 +54,22 @@ const supportOptions = [
This option cannot be used with --range-start and --range-end.
`
},
{
name: "filepath",
filepath: {
since: "1.4.0",
category: CATEGORY_SPECIAL,
type: "path",
default: undefined,
description:
"Specify the input filepath. This will be used to do parser inference."
},
{
name: "insertPragma",
insertPragma: {
since: "1.8.0",
category: CATEGORY_SPECIAL,
type: "boolean",
default: false,
description: "Insert @format pragma into file's first docblock comment."
},
{
name: "jsxBracketSameLine",
since: "0.17.0",
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description: "Put > on the last line instead of at a new line."
},
{
name: "parser",
parser: {
since: "0.0.10",
category: CATEGORY_GLOBAL,
type: "choice",
Expand All @@ -135,47 +94,15 @@ const supportOptions = [
{ value: "markdown", since: "1.8.0", description: "Markdown" }
]
},
{
name: "printWidth",
printWidth: {
since: "0.0.0",
category: CATEGORY_GLOBAL,
type: "int",
default: 80,
description: "The line length where Prettier will try wrap.",
range: { start: 0, end: Infinity, step: 1 }
},
{
name: "proseWrap",
since: "1.8.2",
category: CATEGORY_MARKDOWN,
type: "choice",
default: [
{ since: "1.8.2", value: true },
{ since: "1.9.0", value: "preserve" }
],
description: "How to wrap prose. (markdown)",
choices: [
{
since: "1.9.0",
value: "always",
description: "Wrap prose if it exceeds the print width."
},
{
since: "1.9.0",
value: "never",
description: "Do not wrap prose."
},
{
since: "1.9.0",
value: "preserve",
description: "Wrap prose as-is."
},
{ value: false, deprecated: "1.9.0", redirect: "never" },
{ value: true, deprecated: "1.9.0", redirect: "always" }
]
},
{
name: "rangeEnd",
rangeEnd: {
since: "1.4.0",
category: CATEGORY_SPECIAL,
type: "int",
Expand All @@ -187,8 +114,7 @@ const supportOptions = [
This option cannot be used with --cursor-offset.
`
},
{
name: "rangeStart",
rangeStart: {
since: "1.4.0",
category: CATEGORY_SPECIAL,
type: "int",
Expand All @@ -200,8 +126,7 @@ const supportOptions = [
This option cannot be used with --cursor-offset.
`
},
{
name: "requirePragma",
requirePragma: {
since: "1.7.0",
category: CATEGORY_SPECIAL,
type: "boolean",
Expand All @@ -211,60 +136,14 @@ const supportOptions = [
in order for it to be formatted.
`
},
{
name: "semi",
since: "1.0.0",
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: true,
description: "Print semicolons.",
oppositeDescription:
"Do not print semicolons, except at the beginning of lines which may need them."
},
{
name: "singleQuote",
since: "0.0.0",
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description: "Use single quotes instead of double quotes."
},
{
name: "tabWidth",
tabWidth: {
type: "int",
category: CATEGORY_GLOBAL,
default: 2,
description: "Number of spaces per indentation level.",
range: { start: 0, end: Infinity, step: 1 }
},
{
name: "trailingComma",
since: "0.0.0",
category: CATEGORY_JAVASCRIPT,
type: "choice",
default: [
{ since: "0.0.0", value: false },
{ since: "0.19.0", value: "none" }
],
description: "Print trailing commas wherever possible when multi-line.",
choices: [
{ value: "none", description: "No trailing commas." },
{
value: "es5",
description:
"Trailing commas where valid in ES5 (objects, arrays, etc.)"
},
{
value: "all",
description:
"Trailing commas wherever possible (including function arguments)."
},
{ value: true, deprecated: "0.19.0", redirect: "es5" },
{ value: false, deprecated: "0.19.0", redirect: "none" }
]
},
{
name: "useFlowParser",
useFlowParser: {
since: "0.0.0",
category: CATEGORY_GLOBAL,
type: "boolean",
Expand All @@ -273,15 +152,14 @@ const supportOptions = [
description: "Use flow parser.",
redirect: { option: "parser", value: "flow" }
},
{
name: "useTabs",
useTabs: {
since: "1.0.0",
category: CATEGORY_GLOBAL,
type: "boolean",
default: false,
description: "Indent with tabs instead of spaces."
}
];
};

function getSupportInfo(version, opts) {
opts = opts || {};
Expand All @@ -290,7 +168,21 @@ function getSupportInfo(version, opts) {
version = currentVersion;
}

const options = supportOptions
const plugins = loadPlugins();

const options = util
.arrayify(
Object.assign(
plugins.reduce(
(currentOptions, plugin) =>
Object.assign(currentOptions, plugin.options),
{}
),
supportOptions
),
"name"
)
.sort((a, b) => (a.name === b.name ? 0 : a.name < b.name ? -1 : 1))
.filter(filterSince)
.filter(filterDeprecated)
.map(mapDeprecated)
Expand All @@ -317,7 +209,7 @@ function getSupportInfo(version, opts) {

const usePostCssParser = semver.lt(version, "1.7.1");

const languages = loadPlugins()
const languages = plugins
.reduce((all, plugin) => all.concat(plugin.languages), [])
.filter(language => language.since && semver.gte(version, language.since))
.map(language => {
Expand Down
9 changes: 9 additions & 0 deletions src/common/util.js
Expand Up @@ -805,7 +805,16 @@ function hasNodeIgnoreComment(node) {
);
}

function arrayify(object, keyName) {
return Object.keys(object).reduce(
(array, key) =>
array.concat(Object.assign({ [keyName]: key }, object[key])),
[]
);
}

module.exports = {
arrayify,
punctuationRegex,
punctuationCharRange,
getStringWidth,
Expand Down
2 changes: 2 additions & 0 deletions src/language-css/index.js
@@ -1,6 +1,7 @@
"use strict";

const printer = require("./printer-postcss");
const options = require("./options");

// Based on:
// https://github.com/github/linguist/blob/master/lib/linguist/languages.yml
Expand Down Expand Up @@ -66,6 +67,7 @@ const printers = {
};

module.exports = {
options,
languages,
parsers,
printers
Expand Down
8 changes: 8 additions & 0 deletions src/language-css/options.js
@@ -0,0 +1,8 @@
"use strict";

const jsOptions = require("../language-js/options");

// format based on https://github.com/prettier/prettier/blob/master/src/common/support.js
module.exports = {
singleQuote: jsOptions.singleQuote
};
2 changes: 2 additions & 0 deletions src/language-graphql/index.js
@@ -1,6 +1,7 @@
"use strict";

const printer = require("./printer-graphql");
const options = require("./options");

// Based on:
// https://github.com/github/linguist/blob/master/lib/linguist/languages.yml
Expand Down Expand Up @@ -32,6 +33,7 @@ const printers = {
};

module.exports = {
options,
languages,
parsers,
printers
Expand Down
8 changes: 8 additions & 0 deletions src/language-graphql/options.js
@@ -0,0 +1,8 @@
"use strict";

const jsOptions = require("../language-js/options");

// format based on https://github.com/prettier/prettier/blob/master/src/common/support.js
module.exports = {
bracketSpacing: jsOptions.bracketSpacing
};
2 changes: 2 additions & 0 deletions src/language-js/index.js
@@ -1,6 +1,7 @@
"use strict";

const printer = require("./printer-estree");
const options = require("./options");

// Based on:
// https://github.com/github/linguist/blob/master/lib/linguist/languages.yml
Expand Down Expand Up @@ -132,6 +133,7 @@ const printers = {
};

module.exports = {
options,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options should probably be a property of a specific printer rather than a plugin, as a plugin can provide multiple languages/printers. In this case it should be printers.estree.options

languages,
parsers,
printers
Expand Down