Skip to content
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

lint: collect unused files #22

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

lint: collect unused files #22

wants to merge 8 commits into from

Conversation

smelukov
Copy link
Member

@smelukov smelukov commented Jun 15, 2017

> basis lint --warn-unused-files src --reporter console

  ...

  unused files
    * /src/agent/app/page/field-demo/index.l10n
    * /src/agent/app/page/field-demo/index.js
    * /src/acti/ui/field/demo/template/demo.tmpl
    * /src/acti/ui/field/demo/template/demo-object.tmpl
    * /src/acti/ui/field/demo/template/demo-block.tmpl
    * /src/acti/ui/field/demo/template/demo-block.css
    * /src/acti/ui/field/demo/index.l10n
    * /src/acti/ui/field/demo/index.js

Merge after basisjs/basisjs#163 otherwise the linter will show the wrong result for <b:style src="./some.css"/>

Copy link
Member

@tyanas tyanas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some tests would be great!

var isChildProcess = typeof process.send == 'function'; // child process has send method
var collectFiles = require('./collectFiles');

module.exports = function(flow, options){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interface was changed.
Have you considered adding [js]docs? Like what kind of objects should be flow and options now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module should be untouched (see below).

}

cursor.linkTo.forEach(function(link){
// prevent to handling files that already handled
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prevent to handling files that are already handled

@smelukov smelukov force-pushed the unused-files-linter branch 2 times, most recently from 291afb4 to b241c96 Compare June 16, 2017 12:05
@smelukov smelukov changed the title lint: unused file linter lint: unused files and l10n tokens Jun 16, 2017
@smelukov smelukov force-pushed the unused-files-linter branch 4 times, most recently from e902dad to f1528ff Compare June 16, 2017 15:07
@smelukov
Copy link
Member Author

smelukov commented Jun 16, 2017

Now we supports:
dict.token('prop1').token('prop2').token('prop3')
prop1.prop2.prop3 will be marked as used.

And

<b:define name="someState" type="enum" values="prop1 prop2"/>

<div>
  {l10n:enum.{someState}}
</div>

enum.prop1 and enum.prop2 from template dictionary will be marked as used

@smelukov smelukov changed the title lint: unused files and l10n tokens lint: collect unused files Jun 20, 2017
@@ -20,6 +20,7 @@ module.exports = clap.create('lint', '[fileOrPreset]')
.option('--no-color', 'Suppress color output')
.option('--silent', 'No any output')

.option('--warn-unused-files <base>', 'Warn about unused files in specified path. Do not use with --js-cut-dev. It might cause incorrect result')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.option('--warn-unused-files <dir>', 'Warn about unused files for specified path. Avoid using with --js-cut-dev since it might cause to incorrect results')

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


/**
* @class File
*/

function File(manager, cfg){
this.uniqueId = lastFileId++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed, see below

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@@ -296,6 +298,10 @@ var FileManager = function(baseURI, relBaseURI, console, flow){
this.warns = [];

this.readInfo = [];

// helpers
this.unixpath = unixpath;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant, should be removed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

while (cursor = stack.pop())
{
// mark file as handled
handled[cursor.uniqueId] = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use [Weak]Map, Luke!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about Map, but it seems slow
But ok, I'll use it

@@ -91,7 +94,11 @@ function lint(config){
l10nInfo: true
}).concat([
function(flow){
flow.result = require('./reporter/process-warns')(flow.warns, options.filter);
if (options.warnUnusedFiles)
collectUsed.files(flow);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad pattern: function has a hidden side effect. As I realised it adds usedFiles to flow – it's confusing and should be explicit.

var isChildProcess = typeof process.send == 'function'; // child process has send method
var collectFiles = require('./collectFiles');

module.exports = function(flow, options){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module should be untouched (see below).

}
});

flow.usedFiles = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should returns something rather than assign to flow.

return file.filename && (basePath + file.filename).indexOf(collectPath + '/') === 0;
}

module.exports = function collectUsedFiles(flow){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is much simplier way to find used files for some path:

var usedFiles = flow.files.queue
  .filter(function(file) {
    return file.fsFilename.indexOf(flow.options.warnUnusedFiles + '/') === 0;
  })
  .map(...);

if (options.warnUnusedFiles)
collectUsed.files(flow);
},
function(flow){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for two functions here.

@@ -7,6 +7,9 @@ var extract = require('../extract');
var command = require('./command');
var chalk = require('chalk');
var isChildProcess = typeof process.send == 'function'; // child process has send method
var collectUsed = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No win to use an object here

Copy link
Member Author

@smelukov smelukov Jun 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l10n will be here soon :)

Sergey Melyukov added 2 commits June 21, 2017 12:57
- decoupled flow and collectors
- clear reporters
@smelukov smelukov self-assigned this Jun 22, 2017
}

// warn about unused files
for (var unusedFile in unusedFiles)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this loop doesn't needed and unusedFiles map is redundant

var collectedFiles = collectFiles(basePath);

usedFilesInfo.items.forEach(function(filename){
usedFiles[usedFilesInfo.basePath + filename] = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not delete collectedFiles[usedFile] here? usedFiles is redundant

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cause it will affect flow.usedFiles
seems like it's not good

delete collectedFiles[usedFile];

for (var unusedName in collectedFiles) {
unusedName = unusedName.slice(process.cwd().length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require('path').relative(...) ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants