Skip to content

Commit

Permalink
tie into i18nliner skip logic
Browse files Browse the repository at this point in the history
don't parse the file if we don't need to
  • Loading branch information
jenseng committed May 8, 2015
1 parent 3c4d3f3 commit 93848a4
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,37 @@ var fs = require("fs");
var preprocess = require("./preprocess");
var recast = require("recast");

var escapeRegExp = function(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

var getTranslatePattern = function(config) {
var pattern = 'translate=["\']yes["\']';
var parts = config.autoTranslateTags || [];
if (parts.length)
pattern += "|<" + parts.map(escapeRegExp).join("|<");
return new RegExp(pattern);
};

module.exports = function(i18nliner) {
var JsProcessor = i18nliner.processors.JsProcessor;
var config = i18nliner.config;
var origPreProcess = JsProcessor.prototype.preProcess;
var pattern;

JsProcessor.prototype.preProcess = function(source) {
var ast = recast.parse(source, config.recastOptions);
return {ast: preprocess.ast(ast, config)};
var fileData = origPreProcess.call(this, source);
pattern = pattern || getTranslatePattern(config);

// avoid a parse if we can
fileData.skip = fileData.skip && !source.match(pattern);

if (!fileData.skip) {
var ast = fileData.ast || recast.parse(source, config.recastOptions);
preprocess.ast(ast, config);
fileData.ast = ast;
}

return fileData;
};
};

0 comments on commit 93848a4

Please sign in to comment.