Skip to content

Commit 50be6b4

Browse files
committed
Lib files for latest release.
1 parent 3106421 commit 50be6b4

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

lib/cli.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var configure = function () {
2323
.option("-n, --ngram [number]", "Specify ngram length for comparing token sequences. " +
2424
("[default=" + similar.DEFAULT_NGRAM_LENGTH + ",2,3...]"))
2525
.option("-d, --disablecolors", "Disable color output")
26+
.option("--estype [value]", "Set the JavaScript parser source type [default=module,script]")
2627
.action(compare);
2728
program.on("--help", function () {
2829
console.log(" Command specific help:");

lib/similar.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,16 @@ var filter_redundencies = function (group) {
9898
});
9999
return group;
100100
};
101-
var _compare = function (files, ftype, n_len, t_len, sim_min) {
101+
var _compare = function (files, ftype, opts) {
102102
var is_ts = ftype === "ts";
103103
var is_file = is_ts ? /\.ts$/ : /\.js$/;
104104
files = _.filter(files, function (file) { return is_file.test(file); });
105105
var parse = is_ts ? parse_ts : parse_js;
106-
var items = parse.find(files);
106+
var items = parse.find(files, opts);
107107
var group = {};
108+
var t_len = parse_token_length(_.toString(opts.minlength));
109+
var n_len = parse_ngram_length(_.toString(opts.ngram));
110+
var sim_min = parse_threshold(_.toString(opts.similarity));
108111
each_pair(items, function (src, cmp) {
109112
if (false_positive(src, cmp, t_len))
110113
return;
@@ -122,11 +125,8 @@ var _compare = function (files, ftype, n_len, t_len, sim_min) {
122125
var compare = function (files, opts) {
123126
if (opts === void 0) { opts = {}; }
124127
files = _.concat([], files);
125-
var t_len = parse_token_length(_.toString(opts.minlength));
126-
var n_len = parse_ngram_length(_.toString(opts.ngram));
127-
var threshold = parse_threshold(_.toString(opts.similarity));
128-
var js_group = _compare(files, "js", n_len, t_len, threshold);
129-
var ts_group = _compare(files, "ts", n_len, t_len, threshold);
128+
var js_group = _compare(files, "js", opts);
129+
var ts_group = _compare(files, "ts", opts);
130130
return { js: js_group, ts: ts_group };
131131
};
132132
module.exports = {

lib/similar/javascript.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@ var codegen = require("escodegen");
55
var esprima = require("esprima");
66
var estraverse = require("estraverse");
77
var FUNCTION_OR_CLASS_NODE = [
8-
"ArrowFunctionExpression",
9-
"ClassDeclaration",
10-
"FunctionDeclaration",
11-
"FunctionExpression"
8+
esprima.Syntax.ArrowFunctionExpression,
9+
esprima.Syntax.ClassDeclaration,
10+
esprima.Syntax.FunctionDeclaration,
11+
esprima.Syntax.FunctionExpression
1212
];
1313
var normalize = function (token_list) {
1414
return _.map(token_list, function (t) { return t.value; });
1515
};
1616
var tokenize = function (code) {
1717
return normalize(esprima.tokenize(code));
1818
};
19-
var astify = function (code) {
20-
return esprima.parse(code, { loc: true });
19+
var astify = function (code, opts) {
20+
return esprima.parse(code, {
21+
loc: true,
22+
sourceType: _.get(opts, "estype", "module")
23+
});
2124
};
2225
var ast_to_code = function (node) {
2326
var opts = { format: { indent: { style: " " } } };
@@ -35,10 +38,11 @@ var parse_methods_and_classes = function (root_node, filepath) {
3538
return;
3639
var method = ast_to_code(node);
3740
var tokens = tokenize(method);
41+
var is_class = node.type === esprima.Syntax.ClassDeclaration;
3842
var result = {
3943
ast: node,
4044
code: method,
41-
is_class: node.type === "ClassDeclaration",
45+
is_class: is_class,
4246
path: filepath,
4347
pos: line_info(node),
4448
tokens: tokens,
@@ -49,10 +53,10 @@ var parse_methods_and_classes = function (root_node, filepath) {
4953
});
5054
return entries;
5155
};
52-
var find_similar_methods_and_classes = function (filepaths) {
56+
var find_similar_methods_and_classes = function (filepaths, opts) {
5357
return _.flatMap(filepaths, function (filepath) {
5458
var code = fs.readFileSync(filepath).toString();
55-
var node = astify(code);
59+
var node = astify(code, opts);
5660
return parse_methods_and_classes(node, filepath);
5761
});
5862
};

lib/similar/typescript.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var PASSTHROUGH_NODES = FUNCTION_OR_CLASS_NODE.concat([
4848
ts.SyntaxKind.VariableDeclarationList,
4949
ts.SyntaxKind.VariableStatement
5050
]);
51-
var astify = function (code, filepath) {
51+
var astify = function (code, filepath, opts) {
5252
ts.createProgram([filepath], {});
5353
return ts.createSourceFile(filepath, code, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
5454
};
@@ -112,10 +112,10 @@ var parse_methods_and_classes = function (node, root_node, path) {
112112
};
113113
});
114114
};
115-
var find_similar_methods_and_classes = function (filepaths) {
115+
var find_similar_methods_and_classes = function (filepaths, opts) {
116116
return _.flatMap(filepaths, function (filepath) {
117117
var code = fs.readFileSync(filepath).toString();
118-
var node = astify(code, filepath);
118+
var node = astify(code, filepath, opts);
119119
var root_node = node;
120120
return parse_methods_and_classes(node, root_node, filepath);
121121
});

0 commit comments

Comments
 (0)