-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(extract): adds first sketch to get basic stats from the parsed A…
…ST's (#926) ## Description - adds basic stats per module, derived from the AST ## Motivation and Context - will help identify barrels (and enable defining rules on them) - curiosity ## How Has This Been Tested? - [x] green ci - [x] additional automated non-regression tests ## Types of changes - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] Documentation only change - [ ] Refactor (non-breaking change which fixes an issue without changing functionality) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change)
- Loading branch information
Showing
38 changed files
with
479 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default function extractStats(pAST) { | ||
return { | ||
topLevelStatementCount: pAST?.body?.length || 0, | ||
size: pAST?.end || 0, | ||
}; | ||
} |
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,48 @@ | ||
import { | ||
getStats as tscStats, | ||
shouldUse as tscShouldUse, | ||
} from "./tsc/extract.mjs"; | ||
import { getStats as acornStats } from "./acorn/extract.mjs"; | ||
|
||
/** | ||
* @param {IStrictCruiseOptions} pCruiseOptions | ||
* @param {string} pFileName | ||
* @returns {(IStrictCruiseOptions, string, any) => import("../../types/cruise-result.mjs").IDependency[]} | ||
*/ | ||
function determineExtractionFunction(pCruiseOptions, pFileName) { | ||
let lExtractionFunction = acornStats; | ||
|
||
if (tscShouldUse(pCruiseOptions, pFileName)) { | ||
lExtractionFunction = tscStats; | ||
} | ||
|
||
return lExtractionFunction; | ||
} | ||
|
||
/** | ||
* Returns some stats for the module in pFileName | ||
* | ||
* @param {string} pFileName path to the file | ||
* @param {import("../../types/dependency-cruiser.js").IStrictCruiseOptions} pCruiseOptions cruise options | ||
* @param {import("../../types/dependency-cruiser.js").ITranspileOptions} pTranspileOptions an object with tsconfig ('typescript project') options | ||
* ('flattened' so there's no need for file access on any | ||
* 'extends' option in there) | ||
* @return {import("../../types/dependency-cruiser.js").IDependency[]} an array of dependency objects (see above) | ||
*/ | ||
export default function extractStats( | ||
pFileName, | ||
pCruiseOptions, | ||
pTranspileOptions, | ||
) { | ||
try { | ||
return determineExtractionFunction(pCruiseOptions, pFileName)( | ||
pCruiseOptions, | ||
pFileName, | ||
pTranspileOptions, | ||
); | ||
} catch (pError) { | ||
throw new Error( | ||
`Extracting stats ran afoul of...\n\n ${pError.message}\n... in ${pFileName}\n\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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default function extractStats(pAST) { | ||
return { | ||
topLevelStatementCount: pAST?.statements?.length || 0, | ||
size: pAST?.end || 0, | ||
}; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,52 @@ | ||
[ | ||
{ | ||
"source": "test/extract/__mocks__/with-stats/aap.mjs", | ||
"experimentalStats": { | ||
"topLevelStatementCount": 1, | ||
"size": 36 | ||
}, | ||
"dependencies": [ | ||
{ | ||
"module": "./noot.mjs", | ||
"moduleSystem": "es6", | ||
"dynamic": false, | ||
"exoticallyRequired": false, | ||
"dependencyTypes": ["local", "import"], | ||
"resolved": "test/extract/__mocks__/with-stats/noot.mjs", | ||
"coreModule": false, | ||
"followable": true, | ||
"couldNotResolve": false, | ||
"matchesDoNotFollow": false | ||
} | ||
] | ||
}, | ||
{ | ||
"source": "test/extract/__mocks__/with-stats/noot.mjs", | ||
"experimentalStats": { | ||
"topLevelStatementCount": 2, | ||
"size": 56 | ||
}, | ||
"dependencies": [ | ||
{ | ||
"module": "./mies.mjs", | ||
"moduleSystem": "es6", | ||
"dynamic": false, | ||
"exoticallyRequired": false, | ||
"dependencyTypes": ["local", "import"], | ||
"resolved": "test/extract/__mocks__/with-stats/mies.mjs", | ||
"coreModule": false, | ||
"followable": true, | ||
"couldNotResolve": false, | ||
"matchesDoNotFollow": false | ||
} | ||
] | ||
}, | ||
{ | ||
"source": "test/extract/__mocks__/with-stats/mies.mjs", | ||
"experimentalStats": { | ||
"topLevelStatementCount": 1, | ||
"size": 20 | ||
}, | ||
"dependencies": [] | ||
} | ||
] |
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,37 @@ | ||
import { deepEqual } from "node:assert/strict"; | ||
import extractStats from "#extract/acorn/extract-stats.mjs"; | ||
import { getStats } from "#extract/acorn/extract.mjs"; | ||
|
||
describe("[U] extract/acorn/extractStats", () => { | ||
it("should return stats for a given AST", () => { | ||
const lAST = { | ||
body: [{ type: "FunctionDeclaration" }, { type: "VariableDeclaration" }], | ||
end: 100, | ||
}; | ||
deepEqual(extractStats(lAST), { | ||
topLevelStatementCount: 2, | ||
size: 100, | ||
}); | ||
}); | ||
|
||
it("should return 0 for empty AST", () => { | ||
deepEqual(extractStats({}), { | ||
topLevelStatementCount: 0, | ||
size: 0, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("[I] extract/acorn/extract - getStats", () => { | ||
it("should return stats for a given file containing valid javascript", () => { | ||
const lStats = getStats( | ||
{ baseDir: "./test/extract/acorn" }, | ||
"extract-stats.spec.mjs", | ||
{ extension: ".mjs" }, | ||
); | ||
deepEqual(lStats, { | ||
topLevelStatementCount: 5, | ||
size: 1010, | ||
}); | ||
}); | ||
}); |
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 @@ | ||
import * as noot from "./noot.mjs"; |
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 @@ | ||
export default 456; |
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,2 @@ | ||
import * as mies from "./mies.mjs"; | ||
export default 123; |
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,37 @@ | ||
import { deepEqual } from "node:assert/strict"; | ||
import extractStats from "#extract/acorn/extract-stats.mjs"; | ||
import { getStats } from "#extract/acorn/extract.mjs"; | ||
|
||
describe("[U] extract/acorn/extractStats", () => { | ||
it("should return stats for a given AST", () => { | ||
const lAST = { | ||
body: [{ type: "FunctionDeclaration" }, { type: "VariableDeclaration" }], | ||
end: 100, | ||
}; | ||
deepEqual(extractStats(lAST), { | ||
topLevelStatementCount: 2, | ||
size: 100, | ||
}); | ||
}); | ||
|
||
it("should return 0 for empty AST", () => { | ||
deepEqual(extractStats({}), { | ||
topLevelStatementCount: 0, | ||
size: 0, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("[I] extract/acorn/extract - getStats", () => { | ||
it("should return stats for a given file containing valid javascript", () => { | ||
const lStats = getStats( | ||
{ baseDir: "./test/extract/__mocks__" }, | ||
"extract-stats-testfile.mjs", | ||
{ extension: ".mjs" }, | ||
); | ||
deepEqual(lStats, { | ||
topLevelStatementCount: 5, | ||
size: 1010, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.