-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lint: added unused l10n tokens linter
- Loading branch information
Sergey Melyukov
committed
Jun 16, 2017
1 parent
349f3f5
commit b241c96
Showing
8 changed files
with
217 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module.exports = function collectFiles(flow, callback){ | ||
var stack = [flow.files.queue[0]]; | ||
var collectedFiles = {}; | ||
var handled = {}; | ||
var cursor; | ||
|
||
while (cursor = stack.pop()) | ||
{ | ||
// mark file as handled | ||
handled[cursor.uniqueId] = true; | ||
callback(cursor); | ||
cursor.linkTo.forEach(function(link){ | ||
// prevent handling files that are already handled | ||
if (link[0] && !handled[link[0].uniqueId]) { | ||
stack.push(link[0]); | ||
} | ||
}); | ||
} | ||
|
||
return collectedFiles; | ||
}; |
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,51 @@ | ||
var collect = require('./collector'); | ||
|
||
function isTarget(basePath, collectPath, file){ | ||
return file.filename && (basePath + file.filename).indexOf(collectPath + '/') === 0; | ||
} | ||
|
||
module.exports = function collectUsedFiles(flow){ | ||
var options = flow.options; | ||
var basePath = options.base; | ||
var collectPath = flow.files.abspath(basePath, options.warnUnusedFiles); | ||
var usedFiles = {}; | ||
|
||
collect(flow, function(file){ | ||
if (isTarget(basePath, collectPath, file)) | ||
usedFiles[file.filename] = true; | ||
|
||
if (file.type == 'template') | ||
{ | ||
if (file.decl.deps) | ||
{ | ||
file.decl.deps.forEach(function(resource){ | ||
if (!resource.virtual && isTarget(basePath, collectPath, { filename: resource.url })) | ||
usedFiles[resource.url] = true; | ||
}); | ||
} | ||
if (file.decl.l10n) | ||
{ | ||
file.decl.l10n.forEach(function(item){ | ||
var l10nInfo = item.split('@'); | ||
var dictFilename = l10nInfo[1]; | ||
|
||
if (isTarget(basePath, collectPath, { filename: dictFilename })) | ||
usedFiles[dictFilename] = true; | ||
}); | ||
} | ||
if (file.decl.styles) | ||
{ | ||
file.decl.styles.forEach(function(style){ | ||
if (!style.resource && isTarget(basePath, collectPath, { filename: style.sourceUrl })) | ||
usedFiles[style.sourceUrl] = true; | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
flow.usedFiles = { | ||
basePath: basePath, | ||
collectPath: collectPath, | ||
items: Object.keys(usedFiles) | ||
}; | ||
}; |
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,39 @@ | ||
function isTarget(flow, basePath, collectPath, file){ | ||
return file.filename && (basePath + file.filename).indexOf(collectPath + '/') === 0 && !flow.ignoreWarning(file.filename); | ||
} | ||
|
||
module.exports = function collectUsedL10n(flow){ | ||
var options = flow.options; | ||
var basePath = options.base; | ||
var collectPath = flow.files.abspath(basePath, options.warnUnusedL10n); | ||
var usedTokens = {}; | ||
|
||
if (!flow.l10n || !flow.l10n.dictionaries) | ||
return; | ||
|
||
for (var dictFilename in flow.l10n.dictionaries) | ||
{ | ||
var dict = flow.l10n.dictionaries[dictFilename]; | ||
var dictFile = dict.file; | ||
var dictTokens = dict.tokens; | ||
var dictObj = dictFile.jsResourceContent; | ||
var dictUsage = dictObj._meta && dictObj._meta.usage || []; | ||
|
||
if (isTarget(flow, basePath, collectPath, { filename: dictFilename })) | ||
{ | ||
for (var tokenName in dictTokens) | ||
{ | ||
var tokenInfo = dictTokens[tokenName]; | ||
|
||
usedTokens[dictFilename] = usedTokens[dictFilename] || {}; | ||
usedTokens[dictFilename][tokenName] = Boolean(tokenInfo.ref.length || dictUsage.indexOf(tokenName) > -1); | ||
} | ||
} | ||
} | ||
|
||
flow.usedL10nTokens = { | ||
basePath: basePath, | ||
collectPath: collectPath, | ||
items: usedTokens | ||
}; | ||
}; |
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