-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See #105. WIP Improve OptionValues type, add beautifiers property
- Loading branch information
Showing
8 changed files
with
303 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// tslint:disable:newspaper-order no-shadowed-variable | ||
import { Languages } from "../src/languages"; | ||
import { Options } from "../src/options"; | ||
import { readFileSync, writeFileSync } from "fs"; | ||
import { resolve } from "path"; | ||
// import { replaceInterface, replaceType } from "./utils"; | ||
|
||
const [, , dest] = process.argv; | ||
|
||
if (!dest) { | ||
console.error("Missing required arguments: destination."); | ||
process.exit(1); | ||
} | ||
|
||
const destPath = resolve(process.cwd(), dest); | ||
const originalContents = readFileSync(destPath).toString(); | ||
|
||
let newContents: string = originalContents; | ||
newContents = updateBeautifierLanguageOptions(newContents); | ||
newContents = updateBeautifierOptionName(newContents); | ||
newContents = updateOptionValues(newContents); | ||
newContents = updateBeautifierLanguageOptionComplex(newContents); | ||
|
||
writeFileSync(destPath, newContents); | ||
console.log(`Done writing to ${destPath}!`); | ||
|
||
function updateBeautifierLanguageOptions(originalContents: string): string { | ||
const fields: string[] = Languages.map( | ||
language => ` "${language.name}"?: BeautifierLanguageOptions;` | ||
); | ||
const interfaceName = "BeautifierOptions"; | ||
const newInterfaceBody: string = ` "_"?: BeautifierLanguageOptions;\n${fields.join( | ||
"\n" | ||
)}`; | ||
return replaceInterface(originalContents, interfaceName, newInterfaceBody); | ||
} | ||
|
||
function updateBeautifierOptionName(originalContents: string): string { | ||
const optionNames = Object.keys(Options); | ||
const enums = optionNames.map(name => `"${name}"`); | ||
const newTypeBody = enums.join(" | "); | ||
return replaceType(originalContents, "BeautifierOptionName", newTypeBody); | ||
} | ||
|
||
function updateOptionValues(originalContents: string): string { | ||
const optionNames = Object.keys(Options); | ||
const fields: string[] = optionNames.map(optionName => { | ||
const option = Options[optionName]; | ||
const fieldType: string = typescriptTypeForOptionType(option.type); | ||
return ` "${optionName}"?: ${fieldType};`; | ||
}); | ||
const interfaceName = "OptionValues"; | ||
const newInterfaceBody: string = fields.join("\n"); | ||
return replaceInterface(originalContents, interfaceName, newInterfaceBody); | ||
} | ||
|
||
function updateBeautifierLanguageOptionComplex( | ||
originalContents: string | ||
): string { | ||
const interfaceName = "BeautifierLanguageOptionComplex"; | ||
const optionNames = Object.keys(Options); | ||
const fields: string[] = optionNames.map(optionName => { | ||
const option = Options[optionName]; | ||
const valueType: string = typescriptTypeForOptionType(option.type); | ||
const fieldType: string = `true | ((${optionName}: ${valueType}) => any) | BeautifierLanguageOption`; | ||
return ` "${optionName}"?: ${fieldType};`; | ||
}); | ||
const newInterfaceBody: string = fields.join("\n"); | ||
const newInterfaceContents: string = `export interface ${interfaceName} { | ||
${newInterfaceBody} | ||
}`; | ||
return originalContents + "\n" + newInterfaceContents + "\n"; | ||
} | ||
|
||
function typescriptTypeForOptionType(optionType: string): string { | ||
switch (optionType) { | ||
case "array": | ||
return "any[]"; | ||
case "integer": | ||
return "number"; | ||
default: | ||
return optionType; | ||
} | ||
} | ||
|
||
function replaceInterface( | ||
originalContents: string, | ||
interfaceName: string, | ||
newInterfaceBody: string | ||
): string { | ||
return [originalContents, ( | ||
`export interface ${interfaceName} { | ||
${newInterfaceBody} | ||
}` | ||
)].join("\n"); | ||
} | ||
|
||
function replaceType( | ||
originalContents: string, | ||
typeName: string, | ||
newTypeBody: string | ||
): string { | ||
return [originalContents, ( | ||
`export declare type ${typeName} = ${newTypeBody};` | ||
)].join("\n"); | ||
} |
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,49 @@ | ||
export type BeautifierOptionName = | ||
| "align_assignments" | ||
| "arrow_parens" | ||
| "brace_style" | ||
| "break_chained_methods" | ||
| "comma_first" | ||
| "end_of_line" | ||
| "end_with_comma" | ||
| "end_with_newline" | ||
| "end_with_semicolon" | ||
| "force_indentation" | ||
| "identifier_case" | ||
| "indent_chained_methods" | ||
| "indent_char" | ||
| "indent_comments" | ||
| "indent_inner_html" | ||
| "indent_level" | ||
| "indent_scripts" | ||
| "indent_size" | ||
| "indent_style" | ||
| "indent_with_tabs" | ||
| "jslint_happy" | ||
| "jsx_brackets" | ||
| "keep_array_indentation" | ||
| "keyword_case" | ||
| "max_preserve_newlines" | ||
| "multiline_ternary" | ||
| "newline_before_tags" | ||
| "newline_between_rules" | ||
| "no_leading_zero" | ||
| "object_curly_spacing" | ||
| "pragma_insert" | ||
| "pragma_require" | ||
| "preserve_newlines" | ||
| "quotes" | ||
| "remove_trailing_whitespace" | ||
| "selector_separator_newline" | ||
| "space_after_anon_function" | ||
| "space_before_conditional" | ||
| "space_in_empty_paren" | ||
| "space_in_paren" | ||
| "typesafe_equality_operators" | ||
| "unescape_strings" | ||
| "unformatted" | ||
| "unindent_chained_methods" | ||
| "wrap_attributes" | ||
| "wrap_attributes_indent_size" | ||
| "wrap_line_length" | ||
| "wrap_prose"; |
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.