Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allowed custom .js or .json file outputs with --config
As before, the output path defaults to `--eslintrc.js`. `--eslint` will default to `--config` which has that default. The format of the output path depends on its file extension, with JavaScript for `.js` files and JSON otherwise. The README.md list of flags is getting a little long so I added a quick manual table of contents. Will try to remember to split that into a separate PR...
- Loading branch information
Josh Goldberg
committed
Jul 5, 2019
1 parent
00ba52e
commit 1ddc3ad
Showing
15 changed files
with
198 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { formatOutput } from "./formatOutput"; | ||
import { EOL } from "os"; | ||
|
||
describe("formatOutput", () => { | ||
it("formats output as JavaScript for a .js file path", () => { | ||
// Arrange | ||
const outputPath = ".eslintrc.js"; | ||
const configuration = { rules: {} }; | ||
|
||
// Act | ||
const output = formatOutput(outputPath, configuration); | ||
|
||
// Assert | ||
expect(output).toBe( | ||
`module.exports = ${JSON.stringify(configuration, undefined, 4)};${EOL}`, | ||
); | ||
}); | ||
|
||
it("formats output as JSON for a .json file path", () => { | ||
// Arrange | ||
const outputPath = ".eslintrc.json"; | ||
const configuration = { rules: {} }; | ||
|
||
// Act | ||
const output = formatOutput(outputPath, configuration); | ||
|
||
// Assert | ||
expect(output).toBe(`${JSON.stringify(configuration, undefined, 4)}${EOL}`); | ||
}); | ||
|
||
it("formats output as JSON for an unknown dot file path", () => { | ||
// Arrange | ||
const outputPath = ".eslintrc"; | ||
const configuration = { rules: {} }; | ||
|
||
// Act | ||
const output = formatOutput(outputPath, configuration); | ||
|
||
// Assert | ||
expect(output).toBe(`${JSON.stringify(configuration, undefined, 4)}${EOL}`); | ||
}); | ||
|
||
it("formats output as JSON for an unknown raw file path", () => { | ||
// Arrange | ||
const outputPath = "eslintrc"; | ||
const configuration = { rules: {} }; | ||
|
||
// Act | ||
const output = formatOutput(outputPath, configuration); | ||
|
||
// Assert | ||
expect(output).toBe(`${JSON.stringify(configuration, undefined, 4)}${EOL}`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { formatJsonOutput } from "./formatters/formatJsonOutput"; | ||
import { formatJsOutput } from "./formatters/formatJsOutput"; | ||
|
||
const formatters = new Map([["js", formatJsOutput]]); | ||
|
||
export const formatOutput = (outputPath: string, configuration: unknown): string => { | ||
const customFormatter = formatters.get(getExtension(outputPath)); | ||
const formatter = customFormatter === undefined ? formatJsonOutput : formatJsOutput; | ||
|
||
return formatter(configuration); | ||
}; | ||
|
||
const getExtension = (outputPath: string) => { | ||
const periodIndex = outputPath.lastIndexOf("."); | ||
|
||
return periodIndex === -1 ? outputPath : outputPath.slice(periodIndex + 1); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { EOL } from "os"; | ||
|
||
export const formatJsOutput = (configuration: unknown) => | ||
`module.exports = ${JSON.stringify(configuration, undefined, 4)};${EOL}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { EOL } from "os"; | ||
|
||
export const formatJsonOutput = (configuration: unknown) => | ||
`${JSON.stringify(configuration, undefined, 4)}${EOL}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.