@@ -202,31 +202,30 @@ function parseSourceFileForImports(code, fileName) {
202202 * @returns {ComparisonResult }
203203 */
204204function compareDeps ( { buildGN, sourceCode} ) {
205- const sourceImportsWithFileNameRemoved =
206- sourceCode . imports
207- . map ( importPath => {
208- // If a file imports `../core/sdk/sdk.js`, in the BUILD.gn that is
209- // listed as `../core/sdk:bundle`. In BUILD.gn DEPS you never depend on the
210- // specific file, but the directory that file is in. Therefore we drop the
211- // filename from the import.
212- return path . dirname ( importPath ) ;
213- } )
214- . filter ( dirName => {
215- // This caters for the case where the import is `import X from './Foo.js'`.
216- // Any sibling import doesn't need to be a DEP in BUILD.GN as they are in
217- // the same module, but we will check later that it's in the sources entry.
218- return dirName !== '.' ;
219- } )
220- . map ( importPath => {
221- // If we now have './helpers' we want to replace that with just
222- // 'helpers'. We do this because in the BUILD.gn file the dep will be
223- // listed as something like "helpers:bundle", so the starting "./" will
224- // break comparisons if we keep it around.
225- if ( importPath . startsWith ( './' ) ) {
226- return importPath . slice ( 2 ) ;
227- }
228- return importPath ;
229- } ) ;
205+ const sourceImportsWithFileNameRemoved = sourceCode . imports
206+ . map ( importPath => {
207+ // If a file imports `../core/sdk/sdk.js`, in the BUILD.gn that is
208+ // listed as `../core/sdk:bundle`. In BUILD.gn DEPS you never depend on the
209+ // specific file, but the directory that file is in. Therefore we drop the
210+ // filename from the import.
211+ return path . dirname ( importPath ) ;
212+ } )
213+ . filter ( dirName => {
214+ // This caters for the case where the import is `import X from './Foo.js'`.
215+ // Any sibling import doesn't need to be a DEP in BUILD.GN as they are in
216+ // the same module, but we will check later that it's in the sources entry.
217+ return dirName !== '.' ;
218+ } )
219+ . map ( importPath => {
220+ // If we now have './helpers' we want to replace that with just
221+ // 'helpers'. We do this because in the BUILD.gn file the dep will be
222+ // listed as something like "helpers:bundle", so the starting "./" will
223+ // break comparisons if we keep it around.
224+ if ( importPath . startsWith ( './' ) ) {
225+ return importPath . slice ( 2 ) ;
226+ }
227+ return importPath ;
228+ } ) ;
230229
231230 // Now we have to find the BUILD.gn module that contains this source file
232231 // We check each section of the BUILD.gn and look for this file in the `sources` list.
@@ -239,7 +238,9 @@ function compareDeps({buildGN, sourceCode}) {
239238 } ) ;
240239
241240 if ( ! buildGNModule ) {
242- throw new Error ( `Could not find module in BUILD.gn for ${ sourceCode . filePath } ` ) ;
241+ throw new Error (
242+ `Could not find module in BUILD.gn for ${ sourceCode . filePath } ` ,
243+ ) ;
243244 }
244245
245246 // Special case: if we are linting an entrypoint, we have to find the
@@ -251,11 +252,15 @@ function compareDeps({buildGN, sourceCode}) {
251252 // We are going to use the dependencies from the relevant devtools_module
252253 // find the `deps = [':foo']` line, and return 'foo'
253254 const moduleDep = buildGNModule . deps [ 0 ] . slice ( 1 ) ;
254- buildGNModule = buildGN . find ( buildModule => buildModule . moduleName === moduleDep ) ;
255+ buildGNModule = buildGN . find (
256+ buildModule => buildModule . moduleName === moduleDep ,
257+ ) ;
255258 }
256259
257260 if ( ! buildGNModule ) {
258- throw new Error ( `Could not find devtools_module in BUILD.gn for the devtools_entrypoint of ${ sourceCode . filePath } ` ) ;
261+ throw new Error (
262+ `Could not find devtools_module in BUILD.gn for the devtools_entrypoint of ${ sourceCode . filePath } ` ,
263+ ) ;
259264 }
260265
261266 const buildGNDepsWithTargetRemoved = buildGNModule . deps . map ( dep => {
@@ -334,30 +339,35 @@ function validateDirectory(dirPath) {
334339 const sourceFiles = directoryChildren . filter ( child => {
335340 const isFile = fs . lstatSync ( path . join ( dirPath , child ) ) . isFile ( ) ;
336341 // TODO: longer term we may want to support .css files here too.
337- return ( isFile && path . extname ( child ) === '.ts' ) && ! child . endsWith ( '.test.ts' ) ;
342+ return ( isFile && path . extname ( child ) === '.ts' && ! child . endsWith ( '.test.ts' ) ) ;
338343 } ) ;
339344
340345 /** @type {ValidateDirectoryResult } */
341346 const result = {
342347 missingBuildGNDeps : [ ] ,
343348 // We assume that all BUILD.GN deps are unused, and as we find them in
344349 // source code files we remove them from this set.
345- unusedBuildGNDeps : new Set ( parsedBuildGN . flatMap ( mod => {
346- // We don't worry about any deps that start with a colon, we are only
347- // interested in DEPS that are actual files.
348- return mod . deps . filter ( dep => ! dep . startsWith ( ':' ) ) . map ( dep => {
349- // Drop the :bundle part from a dep, otherwise we can't compare it
350- // against the import statements from the source code.
351- const withoutBundle = dep . split ( ':' ) [ 0 ] ;
352- return withoutBundle ;
353- } ) ;
354- } ) )
350+ unusedBuildGNDeps : new Set (
351+ parsedBuildGN . flatMap ( mod => {
352+ // We don't worry about any deps that start with a colon, we are only
353+ // interested in DEPS that are actual files.
354+ return mod . deps . filter ( dep => ! dep . startsWith ( ':' ) ) . map ( dep => {
355+ // Drop the :bundle part from a dep, otherwise we can't compare it
356+ // against the import statements from the source code.
357+ const withoutBundle = dep . split ( ':' ) [ 0 ] ;
358+ return withoutBundle ;
359+ } ) ;
360+ } ) ,
361+ ) ,
355362 } ;
356363
357364 for ( const sourceFile of sourceFiles ) {
358365 const sourceCode = fs . readFileSync ( path . join ( dirPath , sourceFile ) , 'utf8' ) ;
359366 const parsedSource = parseSourceFileForImports ( sourceCode , sourceFile ) ;
360- const diffWithGN = compareDeps ( { buildGN : parsedBuildGN , sourceCode : parsedSource } ) ;
367+ const diffWithGN = compareDeps ( {
368+ buildGN : parsedBuildGN ,
369+ sourceCode : parsedSource ,
370+ } ) ;
361371 if ( ! diffWithGN ) {
362372 continue ;
363373 }
@@ -377,15 +387,19 @@ module.exports = {
377387 compareDeps,
378388 parseBuildGN,
379389 parseSourceFileForImports,
380- validateDirectory
390+ validateDirectory,
381391} ;
382392
383393// If invoked as CLI
384394if ( require . main === module ) {
385395 const yargs = require ( 'yargs' )
386- . option ( 'directory' , { type : 'string' , desc : 'The directory to validate' , demandOption : true } )
396+ . option ( 'directory' , {
397+ type : 'string' ,
398+ desc : 'The directory to validate' ,
399+ demandOption : true ,
400+ } )
387401 . strict ( )
388- . argv ;
402+ . parseSync ( ) ;
389403
390404 const directory = path . join ( process . cwd ( ) , yargs . directory ) ;
391405 const result = validateDirectory ( directory ) ;
0 commit comments