-
-
Notifications
You must be signed in to change notification settings - Fork 67
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
Add a way to validate the memory usage of your dts files #34
Open
orta
wants to merge
2
commits into
tsdjs:main
Choose a base branch
from
orta:check_type_count
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,85 @@ | ||
import * as path from 'path'; | ||
import {ProjectSizeStats, Diagnostic, Context} from './interfaces'; | ||
import * as pathExists from 'path-exists'; | ||
import {writeFileSync, readFileSync} from 'fs'; | ||
|
||
const statsFilename = '.tsd-memory-check-results.json'; | ||
|
||
const findStatsFile = async (context: Context) => { | ||
const rootFileExists = await pathExists(path.join(context.options.cwd, statsFilename)); | ||
if (rootFileExists) { | ||
return path.join(context.options.cwd, statsFilename); | ||
} | ||
|
||
const subfolderFileExists = await pathExists(path.join(context.options.cwd, '.tsd', statsFilename)); | ||
if (subfolderFileExists) { | ||
return path.join(context.options.cwd, '.tsd', statsFilename); | ||
} | ||
|
||
return; | ||
}; | ||
|
||
/** | ||
* Get a list of TypeScript diagnostics, and memory size within the current context. | ||
* | ||
* @param context - The context object. | ||
* @param runStats - An object which has the memory stats from a run. | ||
* @returns An array of diagnostics | ||
*/ | ||
export const verifyMemorySize = async (context: Context, runStats: ProjectSizeStats): Promise<Diagnostic[]> => { | ||
const existingStatsPath = await findStatsFile(context); | ||
|
||
const writeJSON = (message: string) => { | ||
const rootFilePath = path.join(context.options.cwd, statsFilename); | ||
writeFileSync(rootFilePath, JSON.stringify(runStats, null, ' '), 'utf8'); | ||
return [{ | ||
fileName: rootFilePath, | ||
message, | ||
severity: 'warning' as 'warning' | ||
}]; | ||
}; | ||
|
||
if (!existingStatsPath) { | ||
return writeJSON('Recording the stats for memory size in your types'); | ||
} | ||
|
||
if (context.options.writeSnapshot) { | ||
return writeJSON('Updated the stats for memory size in your types'); | ||
} | ||
|
||
const existingResults = JSON.parse(readFileSync(existingStatsPath, 'utf8')) as ProjectSizeStats; | ||
const validate = (prev: number, current: number) => { | ||
const largerPrev = (prev / 100) * (context.options.sizeDelta + 100); | ||
return current < largerPrev; | ||
}; | ||
|
||
const names: string[] = []; | ||
|
||
// This is an approximation, and would likely change between versions, so the number is | ||
// conservative | ||
const typescriptTypes = 8500; | ||
|
||
if (!validate(existingResults.typeCount - typescriptTypes, runStats.typeCount - typescriptTypes)) { | ||
names.push(`- Type Count raised by ${runStats.typeCount - existingResults.typeCount}`); | ||
} | ||
|
||
if (!validate(existingResults.memoryUsage, runStats.memoryUsage)) { | ||
names.push(`- Memory usage raised by ${runStats.typeCount / existingResults.typeCount * 100}%`); | ||
} | ||
|
||
if (names.length) { | ||
const messages = [ | ||
'Failed due to memory changes for types being raised. Higher numbers', | ||
'can slow down the editor experience for consumers.', | ||
'If you\'d like to update the fixtures to keep these changes re-run tsd with --write', | ||
'', ...names]; | ||
|
||
return [{ | ||
fileName: existingStatsPath, | ||
message: messages.join('\n '), | ||
severity: 'error' as 'error' | ||
}]; | ||
} | ||
|
||
return []; | ||
}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Kinda seems like these should be in a subcommand, like
tsd memory-usage --write
. For example,tsd --write
is way too generic naming for the top-level interface. However, we can't really do subcommands as we expect paths. Alternatively, we could namespace the flags:--mem-write
,--mem-verify-size
, ...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.
@sindresorhus We could however do
tsd --cwd=path
instead oftsd path
so we can still accept subcommands?Or
tsd verify path
or something? Not sure, just thinking out loud.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.
I'd like to ideally make it run at the same time as normal tests (rather than have to run it as a 2nd command) - which is why I opted for using flags on the main command.
I copied
--write
from jest, but I'm open to having it more verbose and specific too 👍