-
Notifications
You must be signed in to change notification settings - Fork 44
Optional configuration for multiple imports per line
and strictly one import per line
.
#50
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
Changes from 2 commits
24d465b
c1e84da
8a1d5e5
aed9a25
59e1013
6dda58a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
}, | ||
"homepage": "https://github.com/TypeScript-Heroes/node-typescript-parser#readme", | ||
"devDependencies": { | ||
"@types/lodash-es": "^4.17.0", | ||
"@types/jest": "^21.1.8", | ||
"@types/mock-fs": "^3.6.30", | ||
"@types/node": "^8.0.57", | ||
|
@@ -45,7 +46,8 @@ | |
"typedoc": "^0.9.0" | ||
}, | ||
"dependencies": { | ||
"lodash": "^4.17.4", | ||
"lodash": "^4.17.5", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so if you want to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both are needed, I only had to add these because I was facing errors in running There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright |
||
"lodash-es": "^4.17.5", | ||
"tslib": "^1.8.1", | ||
"typescript": "^2.6.2" | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,14 @@ export interface TypescriptGenerationOptions { | |
*/ | ||
spaceBraces: boolean; | ||
|
||
/** | ||
* The wrapping method to be used in multiline imports. | ||
* | ||
* @type {'MULTIPLE_IMPORTS_PER_LINE' | 'ONE_IMPORT_PER_LINE'} | ||
* @memberof TypescriptGenerationOptions | ||
*/ | ||
multiLineWrapMethod: 'ONE_IMPORT_PER_LINE' | 'MULTIPLE_IMPORTS_PER_LINE'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd use a export enum MultiLineImportRule {
onePerLine = 'onePerLine',
multiplePerLine = 'multiplePerLine',
} With this method, we can implement this feature as a normal config in typescript hero and use the enum directly (as string value) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. |
||
|
||
/** | ||
* The threshold where an import is written as multiline. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,9 @@ const multiLineImport = stringTemplate`import ${3}{ | |
${0}${1} | ||
} from ${2}`; | ||
|
||
const aliasOnlyMultiLineImport = stringTemplate`import ${0} | ||
from ${1}`; | ||
|
||
/** | ||
* Sort function for symbol specifiers. Does sort after the specifiers name (to lowercase). | ||
* | ||
|
@@ -45,6 +48,7 @@ export function generateNamedImport( | |
stringQuoteStyle, | ||
spaceBraces, | ||
tabSize, | ||
multiLineWrapMethod, | ||
multiLineWrapThreshold, | ||
multiLineTrailingComma, | ||
}: TypescriptGenerationOptions, | ||
|
@@ -53,26 +57,54 @@ export function generateNamedImport( | |
const lib = `${stringQuoteStyle}${imp.libraryName}${stringQuoteStyle}${eol}`; | ||
|
||
const specifiers = imp.specifiers.sort(specifierSort).map(o => generateSymbolSpecifier(o)).join(', '); | ||
let importSpecifiers = `${space}${specifiers}${space}`; | ||
if (importSpecifiers.trim().length === 0) { | ||
importSpecifiers = ' '; | ||
} | ||
|
||
const importString = importTemplate( | ||
getImportSpecifiers(imp, spaceBraces), | ||
lib, | ||
); | ||
let retVal:string = ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing space in code formatting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And please use the whole name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup. |
||
|
||
if (importString.length > multiLineWrapThreshold) { | ||
if (specifiers.length > multiLineWrapThreshold) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This actually changes the behaviour of the multi line wrap threshold. before, the generated string was checked for his length. Now only the specifiers. Or did I misread something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should behave like before, or we need to introduce a BREAKING CHANGE There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually intended this change, because I thought that it is the right thing to do, didn't realize that it could be considered a Breaking change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I will rever that part for now. Its small thing to change later if think that it should be done. |
||
const spacings = Array(tabSize + 1).join(' '); | ||
return multiLineImport( | ||
imp.specifiers.sort(specifierSort).map(o => `${spacings}${generateSymbolSpecifier(o)}`).join(',\n'), | ||
multiLineTrailingComma ? ',' : '', | ||
`${stringQuoteStyle}${imp.libraryName}${stringQuoteStyle}${eol}`, | ||
imp.defaultAlias ? `${imp.defaultAlias}, ` : '', | ||
const sortedImportSpecifiers: SymbolSpecifier[] = imp.specifiers.sort(specifierSort); | ||
let importSpecifierStrings: string = ''; | ||
|
||
if (multiLineWrapMethod === 'MULTIPLE_IMPORTS_PER_LINE') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use the string enum, as mentioned above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes. |
||
importSpecifierStrings = sortedImportSpecifiers.reduce( | ||
(acc, curr) => { | ||
const symbolSpecifier: string = generateSymbolSpecifier(curr); | ||
const dist: number = acc.out.length - acc.lastWrapOffset + symbolSpecifier.length; | ||
const needsWrap: boolean = dist >= multiLineWrapThreshold; | ||
return { | ||
out: acc.out + (needsWrap ? `,\n${spacings}` : (acc.out.length ? `, ` : `${spacings}`)) + | ||
symbolSpecifier, | ||
lastWrapOffset: acc.lastWrapOffset + (needsWrap ? dist : 0), | ||
}; | ||
}, | ||
{ | ||
out: '', | ||
lastWrapOffset: 0, | ||
}, | ||
).out; | ||
} else { | ||
// For 'ONE_IMPORT_PER_LINE' which also happens to be the default case. | ||
importSpecifierStrings = sortedImportSpecifiers.map(o => `${spacings}${generateSymbolSpecifier(o)}`).join(',\n'); | ||
} | ||
if (imp.specifiers.length > 0) { | ||
retVal = multiLineImport( | ||
importSpecifierStrings, | ||
multiLineTrailingComma ? ',' : '', | ||
`${stringQuoteStyle}${imp.libraryName}${stringQuoteStyle}${eol}`, | ||
imp.defaultAlias ? `${imp.defaultAlias}, ` : '', | ||
); | ||
} else { | ||
retVal = aliasOnlyMultiLineImport( | ||
imp.defaultAlias ? `${imp.defaultAlias}, ` : '', | ||
`${stringQuoteStyle}${imp.libraryName}${stringQuoteStyle}${eol}`, | ||
); | ||
} | ||
} else { | ||
retVal = importTemplate( | ||
getImportSpecifiers(imp, spaceBraces), | ||
lib, | ||
); | ||
} | ||
return importString; | ||
return retVal; | ||
} | ||
|
||
function getImportSpecifiers(namedImport: NamedImport, spaceBraces: boolean): string { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please not in the base config. The
tsconfig.json
configuration does specifysourceMap: true
. The base should not change that since the production config does not overwrite the setting again.->
false
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok