forked from sverweij/dependency-cruiser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(typescript): detect typescript dependencies that only exist b…
…efore compile time (sverweij#30) * feature(typescript): use the typescript AST to infer some of the dependencies: Some typescript features and dependencies get lost when the typescript compiler translates to javascript. By using the typescript AST instead of the javascript-AST-after-translation (e.g. when you depend only on a type from a module, if you depend on a module that isn't used (yet), have triple-slash directives, or use the import-from-export-equals construct) , we'll be able to catch these as well. Will solve issue sverweij#28 * refactor(extract): pull out the 'parse' step from extract.js and ... * ... rename all modules that extract dependencies from an AST from extract-<modulesystem> to etract-<modulesystem>-deps as that better conveys their purpose * ... put the extract-yadda-deps in a separate folder * ... separate parsing and extracting in the (new) typescript dependency extraction * style(depcruise): updates some of our own dependency-cruiser rules * test(extract): add a test for when dependencies aren't used and they still should show up in the output * doc(parse): document the parser wrappers * doc(samples): updates the tslint sample
- Loading branch information
Showing
29 changed files
with
661 additions
and
120 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
Binary file modified
BIN
+43.2 KB
(120%)
doc/real-world-samples/dependency-cruiser-without-node_modules.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
2 changes: 1 addition & 1 deletion
2
src/extract/extract-AMD.js → ...xtract/ast-extractors/extract-AMD-deps.js
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
File renamed without changes.
File renamed without changes.
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,71 @@ | ||
"use strict"; | ||
|
||
/* | ||
* Both extractImport* assume the imports/ exports can only occur at | ||
* top level. AFAIK this the only place they're allowed, so we should | ||
* be good. Otherwise we'll need to walk the tree. | ||
*/ | ||
function extractImportsAndExports(pAST) { | ||
const IMPORT_DECLARATION_KIND = 238; | ||
const EXPORT_DECLARATION_KIND = 244; | ||
|
||
return pAST.statements | ||
.filter(pStatement => | ||
( | ||
pStatement.kind === IMPORT_DECLARATION_KIND || | ||
pStatement.kind === EXPORT_DECLARATION_KIND | ||
) && | ||
Boolean(pStatement.moduleSpecifier) | ||
).map(pStatement => ({ | ||
moduleName: pStatement.moduleSpecifier.text, | ||
moduleSystem: 'es6' | ||
})); | ||
} | ||
|
||
function extractImportEquals(pAST) { | ||
const IMPORT_EQUALS_DECLARATION_KIND = 237; | ||
|
||
return pAST.statements | ||
.filter(pStatement => | ||
pStatement.kind === IMPORT_EQUALS_DECLARATION_KIND | ||
).map(pStatement => ({ | ||
moduleName: pStatement.moduleReference.expression.text, | ||
moduleSystem: 'cjs' | ||
})); | ||
} | ||
|
||
/** | ||
* might be wise to distinguish the three types of /// directive that | ||
* can come out of this as the resolution algorithm might differ | ||
* | ||
* @param {*} pAST - typescript syntax tree | ||
* @returns {object} - 'tripple slash' dependencies | ||
*/ | ||
function extractTrippleSlashDirectives(pAST) { | ||
return pAST.referencedFiles.map( | ||
pReference => ({ | ||
moduleName: pReference.fileName, | ||
moduleSystem: 'tsd' | ||
}) | ||
).concat( | ||
pAST.typeReferenceDirectives.map( | ||
pReference => ({ | ||
moduleName: pReference.fileName, | ||
moduleSystem: 'tsd' | ||
}) | ||
) | ||
).concat( | ||
pAST.amdDependencies.map( | ||
pReference => ({ | ||
moduleName: pReference.path, | ||
moduleSystem: 'tsd' | ||
}) | ||
) | ||
); | ||
} | ||
|
||
module.exports = (pTypeScriptAST) => | ||
extractImportsAndExports(pTypeScriptAST) | ||
.concat(extractImportEquals(pTypeScriptAST)) | ||
.concat(extractTrippleSlashDirectives(pTypeScriptAST)); | ||
|
Oops, something went wrong.