diff --git a/README.md b/README.md index 6307f655d..08138d7b3 100644 --- a/README.md +++ b/README.md @@ -271,7 +271,8 @@ Prettier ships with a handful of customizable format options, usable in both the | **JSX Brackets on Same Line** - Put the `>` of a multi-line JSX element at the end of the last line instead of being alone on the next line | `false` | `--jsx-bracket-same-line` | `jsxBracketSameLine: ` | | **Range Start** - Format code starting at a given character offset. The range will extend backwards to the start of the first line containing the selected statement. | `0` | `--range-start ` | `rangeStart: ` | | **Range End** - Format code ending at a given character offset (exclusive). The range will extend forwards to the end of the selected statement. | `Infinity` | `--range-end ` | `rangeEnd: ` | -| **Parser** - Specify which parser to use. Both parsers support the same set of JavaScript features (including Flow). You shouldn't have to change this setting. | `babylon` | --parser | parser: "" | +| **Parser** - Specify which parser to use. Both parsers support the same set of JavaScript features (including Flow). You shouldn't have to change this setting. | `babylon` | --parser | parser: "" | +| **Filepath** - Specify the input filepath this will be used to do parser inference.

Example:
`cat foo \| prettier --stdin-filepath foo.css`
will default to use `postcss` parser | | `--stdin-filepath` | `filepath: ` | ### Excluding code from formatting diff --git a/bin/prettier.js b/bin/prettier.js index 667a4146a..8e7e69d0f 100755 --- a/bin/prettier.js +++ b/bin/prettier.js @@ -41,7 +41,8 @@ const argv = minimist(process.argv.slice(2), { "parser", "trailing-comma", "range-start", - "range-end" + "range-end", + "stdin-filepath" ], default: { semi: true, @@ -160,6 +161,7 @@ const options = { bracketSpacing: argv["bracket-spacing"], singleQuote: argv["single-quote"], jsxBracketSameLine: argv["jsx-bracket-same-line"], + filepath: argv["stdin-filepath"], trailingComma: getTrailingComma(), parser: getParserOption() }; @@ -233,6 +235,7 @@ if (argv["help"] || (!filepatterns.length && !stdin)) { " --write Edit the file in-place. (Beware!)\n" + " --list-different or -l Print filenames of files that are different from Prettier formatting.\n" + " --stdin Read input from stdin.\n" + + " --stdin-filepath Path to the file used to read from stdin.\n" + " --print-width Specify the length of line that the printer will wrap on. Defaults to 80.\n" + " --tab-width Specify the number of spaces per indentation-level. Defaults to 2.\n" + " --use-tabs Indent lines with tabs instead of spaces.\n" + diff --git a/src/options.js b/src/options.js index e0b6d72e9..353188457 100644 --- a/src/options.js +++ b/src/options.js @@ -18,7 +18,7 @@ const defaults = { }; const exampleConfig = Object.assign({}, defaults, { - filename: "testFilename", + filepath: "path/to/Filename", printWidth: 80, originalText: "text" }); @@ -26,6 +26,13 @@ const exampleConfig = Object.assign({}, defaults, { // Copy options and fill in default values. function normalize(options) { const normalized = Object.assign({}, options || {}); + const filepath = normalized.filepath; + + if (/\.(css|less|scss)$/.test(filepath)) { + normalized.parser = "postcss"; + } else if (/\.(ts|tsx)$/.test(filepath)) { + normalized.parser = "typescript"; + } if (typeof normalized.trailingComma === "boolean") { // Support a deprecated boolean type for the trailing comma config