Repository: brentlintner/synt Branch: master Commit: 50f91ad27a83 Files: 59 Total size: 94.2 KB Directory structure: gitextract_0w_fgwm_/ ├── .circleci/ │ └── config.yml ├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .release-it.json ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── appveyor.yml ├── bin/ │ ├── list-env-versions │ ├── release │ └── synt ├── coffeelint.json ├── lib/ │ ├── cli/ │ │ └── file_collector.js │ ├── cli.js │ ├── index.js │ ├── similar/ │ │ ├── javascript.js │ │ ├── ngram.js │ │ ├── print.js │ │ └── typescript.js │ └── similar.js ├── package.json ├── src/ │ ├── @types/ │ │ ├── @javascript-obfuscator/ │ │ │ └── index.d.ts │ │ ├── cardinal/ │ │ │ └── index.d.ts │ │ ├── espree/ │ │ │ └── index.d.ts │ │ ├── index.d.ts │ │ └── walk-sync/ │ │ └── index.d.ts │ ├── cli/ │ │ └── file_collector.ts │ ├── cli.ts │ ├── index.ts │ ├── similar/ │ │ ├── javascript.ts │ │ ├── ngram.ts │ │ ├── print.ts │ │ └── typescript.ts │ └── similar.ts ├── test/ │ ├── fixtures/ │ │ ├── cli_output/ │ │ │ ├── test-es-fail.js.txt │ │ │ ├── test-es.js.txt │ │ │ ├── test.dir.color.txt │ │ │ ├── test.dir.txt │ │ │ ├── test.js.txt │ │ │ ├── test.ngram.js.txt │ │ │ ├── test.similarity.js.txt │ │ │ ├── test.token.js.txt │ │ │ └── test.ts.txt │ │ └── system/ │ │ ├── test-es.js │ │ ├── test.copy.js │ │ ├── test.js │ │ └── test.ts │ ├── helpers/ │ │ ├── sinon_chai.coffee │ │ └── system.coffee │ ├── mocha.opts │ └── spec/ │ ├── index.coffee │ ├── similar/ │ │ └── ngram.coffee │ ├── similar.coffee │ └── system.coffee └── tsconfig.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: .circleci/config.yml ================================================ version: '2.1' orbs: node: circleci/node@5.1.1 jobs: build: docker: - image: cimg/base:stable steps: - checkout - node/install: node-version: '20.10' - restore_cache: key: dependency-cache-{{ checksum "package.json" }} - run: name: Versions command: sh bin/list-env-versions - run: name: Install command: npm i - run: name: Test command: npm run test-ci-build - save_cache: key: dependency-cache-{{ checksum "package.json" }} paths: - node_modules ================================================ FILE: .editorconfig ================================================ root = true [*] charset = utf-8 end_of_line = lf trim_trailing_whitespace = true insert_final_newline = true indent_style = space indent_size = 2 ================================================ FILE: .eslintrc.json ================================================ { "plugins": [ "eslint-plugin-jsdoc", "eslint-plugin-prefer-arrow", "@typescript-eslint", "@stylistic/ts" ], "env": { "browser": true, "es6": true, "node": true }, "ignorePatterns": [ "bin", "lib", "test/fixtures" ], "extends": [ "plugin:@typescript-eslint/recommended", "plugin:@typescript-eslint/recommended-requiring-type-checking" ], "parser": "@typescript-eslint/parser", "parserOptions": { "project": "tsconfig.json", "sourceType": "module" }, "root": true, "rules": { "@stylistic/ts/semi": ["error", "never"], "@stylistic/ts/comma-dangle": ["error", "never"], "@stylistic/ts/type-annotation-spacing": ["error", { "before": true, "after": true }], "@typescript-eslint/no-unused-vars": "off", "@typescript-eslint/no-var-requires": "off" } } ================================================ FILE: .gitattributes ================================================ lib/** linguist-generated test/fixtures/** linguist-generated bin/** linguist-vendored package-lock.json linguist-generated * text eol=lf ================================================ FILE: .gitignore ================================================ node_modules .nyc_output coverage npm-debug.log .ignore ================================================ FILE: .release-it.json ================================================ { "git": { "changelog": "", "requireCleanWorkingDir": true, "requireBranch": false, "requireUpstream": false, "requireCommits": false, "requireCommitsFail": true, "commitsPath": "", "addUntrackedFiles": false, "commit": true, "commitMessage": "v${version}", "commitArgs": [], "tag": true, "tagExclude": null, "tagName": null, "tagMatch": null, "getLatestTagFromAllRefs": false, "tagAnnotation": "v${version}", "tagArgs": [], "push": true, "pushArgs": ["--follow-tags"], "pushRepo": "" }, "npm": { "publish": true, "publishPath": ".", "publishArgs": [], "tag": null, "otp": null, "ignoreVersion": false, "allowSameVersion": false, "versionArgs": [], "skipChecks": false, "timeout": 10 } } ================================================ FILE: CHANGELOG.md ================================================ # Synt Changelog Please see the GitHub [releases](https://github.com/brentlintner/synt/releases) section. ================================================ FILE: CODE_OF_CONDUCT.md ================================================ # Contributor Covenant Code of Conduct ## Our Pledge In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. ## Our Standards Examples of behavior that contributes to creating a positive environment include: * Using welcoming and inclusive language * Being respectful of differing viewpoints and experiences * Gracefully accepting constructive criticism * Focusing on what is best for the community * Showing empathy towards other community members Examples of unacceptable behavior by participants include: * The use of sexualized language or imagery and unwelcome sexual attention or advances * Trolling, insulting/derogatory comments, and personal or political attacks * Public or private harassment * Publishing others' private information, such as a physical or electronic address, without explicit permission * Other conduct which could reasonably be considered inappropriate in a professional setting ## Our Responsibilities Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. ## Scope This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at brent.lintner@gmail.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. ## Attribution This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] [homepage]: http://contributor-covenant.org [version]: http://contributor-covenant.org/version/1/4/ ================================================ FILE: CONTRIBUTING.md ================================================ # Contributing Current list of [Contributors](https://github.com/brentlintner/synt/graphs/contributors). ## Issues Current issue tracker is on [GitHub](https://github.com/brentlintner/synt/issues). Even if you are uncomfortable with code, an Issue helps! ## Patches All you need to do is submit a [Pull Request](https://github.com/brentlintner/synt/pulls). 1. Please consider tests and code quality before submitting. 2. Please try to keep commits clean, atomic and well explained (for others). ## Code Of Conduct By participating in this project you agree to our [Code of Conduct](CODE_OF_CONDUCT.md). ## Developing git clone git@github.com:brentlintner/synt.git cd synt npm i npm run -s compile ### Testing npm test npm run -s test-cov ### Dev Scripts *watch and compile files on save*: npm run dev ================================================ FILE: LICENSE ================================================ Mozilla Public License Version 2.0 ================================== 1. Definitions -------------- 1.1. "Contributor" means each individual or legal entity that creates, contributes to the creation of, or owns Covered Software. 1.2. "Contributor Version" means the combination of the Contributions of others (if any) used by a Contributor and that particular Contributor's Contribution. 1.3. "Contribution" means Covered Software of a particular Contributor. 1.4. "Covered Software" means Source Code Form to which the initial Contributor has attached the notice in Exhibit A, the Executable Form of such Source Code Form, and Modifications of such Source Code Form, in each case including portions thereof. 1.5. "Incompatible With Secondary Licenses" means (a) that the initial Contributor has attached the notice described in Exhibit B to the Covered Software; or (b) that the Covered Software was made available under the terms of version 1.1 or earlier of the License, but not also under the terms of a Secondary License. 1.6. "Executable Form" means any form of the work other than Source Code Form. 1.7. "Larger Work" means a work that combines Covered Software with other material, in a separate file or files, that is not Covered Software. 1.8. "License" means this document. 1.9. "Licensable" means having the right to grant, to the maximum extent possible, whether at the time of the initial grant or subsequently, any and all of the rights conveyed by this License. 1.10. "Modifications" means any of the following: (a) any file in Source Code Form that results from an addition to, deletion from, or modification of the contents of Covered Software; or (b) any new file in Source Code Form that contains any Covered Software. 1.11. "Patent Claims" of a Contributor means any patent claim(s), including without limitation, method, process, and apparatus claims, in any patent Licensable by such Contributor that would be infringed, but for the grant of the License, by the making, using, selling, offering for sale, having made, import, or transfer of either its Contributions or its Contributor Version. 1.12. "Secondary License" means either the GNU General Public License, Version 2.0, the GNU Lesser General Public License, Version 2.1, the GNU Affero General Public License, Version 3.0, or any later versions of those licenses. 1.13. "Source Code Form" means the form of the work preferred for making modifications. 1.14. "You" (or "Your") means an individual or a legal entity exercising rights under this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with You. For purposes of this definition, "control" means (a) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (b) ownership of more than fifty percent (50%) of the outstanding shares or beneficial ownership of such entity. 2. License Grants and Conditions -------------------------------- 2.1. Grants Each Contributor hereby grants You a world-wide, royalty-free, non-exclusive license: (a) under intellectual property rights (other than patent or trademark) Licensable by such Contributor to use, reproduce, make available, modify, display, perform, distribute, and otherwise exploit its Contributions, either on an unmodified basis, with Modifications, or as part of a Larger Work; and (b) under Patent Claims of such Contributor to make, use, sell, offer for sale, have made, import, and otherwise transfer either its Contributions or its Contributor Version. 2.2. Effective Date The licenses granted in Section 2.1 with respect to any Contribution become effective for each Contribution on the date the Contributor first distributes such Contribution. 2.3. Limitations on Grant Scope The licenses granted in this Section 2 are the only rights granted under this License. No additional rights or licenses will be implied from the distribution or licensing of Covered Software under this License. Notwithstanding Section 2.1(b) above, no patent license is granted by a Contributor: (a) for any code that a Contributor has removed from Covered Software; or (b) for infringements caused by: (i) Your and any other third party's modifications of Covered Software, or (ii) the combination of its Contributions with other software (except as part of its Contributor Version); or (c) under Patent Claims infringed by Covered Software in the absence of its Contributions. This License does not grant any rights in the trademarks, service marks, or logos of any Contributor (except as may be necessary to comply with the notice requirements in Section 3.4). 2.4. Subsequent Licenses No Contributor makes additional grants as a result of Your choice to distribute the Covered Software under a subsequent version of this License (see Section 10.2) or under the terms of a Secondary License (if permitted under the terms of Section 3.3). 2.5. Representation Each Contributor represents that the Contributor believes its Contributions are its original creation(s) or it has sufficient rights to grant the rights to its Contributions conveyed by this License. 2.6. Fair Use This License is not intended to limit any rights You have under applicable copyright doctrines of fair use, fair dealing, or other equivalents. 2.7. Conditions Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in Section 2.1. 3. Responsibilities ------------------- 3.1. Distribution of Source Form All distribution of Covered Software in Source Code Form, including any Modifications that You create or to which You contribute, must be under the terms of this License. You must inform recipients that the Source Code Form of the Covered Software is governed by the terms of this License, and how they can obtain a copy of this License. You may not attempt to alter or restrict the recipients' rights in the Source Code Form. 3.2. Distribution of Executable Form If You distribute Covered Software in Executable Form then: (a) such Covered Software must also be made available in Source Code Form, as described in Section 3.1, and You must inform recipients of the Executable Form how they can obtain a copy of such Source Code Form by reasonable means in a timely manner, at a charge no more than the cost of distribution to the recipient; and (b) You may distribute such Executable Form under the terms of this License, or sublicense it under different terms, provided that the license for the Executable Form does not attempt to limit or alter the recipients' rights in the Source Code Form under this License. 3.3. Distribution of a Larger Work You may create and distribute a Larger Work under terms of Your choice, provided that You also comply with the requirements of this License for the Covered Software. If the Larger Work is a combination of Covered Software with a work governed by one or more Secondary Licenses, and the Covered Software is not Incompatible With Secondary Licenses, this License permits You to additionally distribute such Covered Software under the terms of such Secondary License(s), so that the recipient of the Larger Work may, at their option, further distribute the Covered Software under the terms of either this License or such Secondary License(s). 3.4. Notices You may not remove or alter the substance of any license notices (including copyright notices, patent notices, disclaimers of warranty, or limitations of liability) contained within the Source Code Form of the Covered Software, except that You may alter any license notices to the extent required to remedy known factual inaccuracies. 3.5. Application of Additional Terms You may choose to offer, and to charge a fee for, warranty, support, indemnity or liability obligations to one or more recipients of Covered Software. However, You may do so only on Your own behalf, and not on behalf of any Contributor. You must make it absolutely clear that any such warranty, support, indemnity, or liability obligation is offered by You alone, and You hereby agree to indemnify every Contributor for any liability incurred by such Contributor as a result of warranty, support, indemnity or liability terms You offer. You may include additional disclaimers of warranty and limitations of liability specific to any jurisdiction. 4. Inability to Comply Due to Statute or Regulation --------------------------------------------------- If it is impossible for You to comply with any of the terms of this License with respect to some or all of the Covered Software due to statute, judicial order, or regulation then You must: (a) comply with the terms of this License to the maximum extent possible; and (b) describe the limitations and the code they affect. Such description must be placed in a text file included with all distributions of the Covered Software under this License. Except to the extent prohibited by statute or regulation, such description must be sufficiently detailed for a recipient of ordinary skill to be able to understand it. 5. Termination -------------- 5.1. The rights granted under this License will terminate automatically if You fail to comply with any of its terms. However, if You become compliant, then the rights granted under this License from a particular Contributor are reinstated (a) provisionally, unless and until such Contributor explicitly and finally terminates Your grants, and (b) on an ongoing basis, if such Contributor fails to notify You of the non-compliance by some reasonable means prior to 60 days after You have come back into compliance. Moreover, Your grants from a particular Contributor are reinstated on an ongoing basis if such Contributor notifies You of the non-compliance by some reasonable means, this is the first time You have received notice of non-compliance with this License from such Contributor, and You become compliant prior to 30 days after Your receipt of the notice. 5.2. If You initiate litigation against any entity by asserting a patent infringement claim (excluding declaratory judgment actions, counter-claims, and cross-claims) alleging that a Contributor Version directly or indirectly infringes any patent, then the rights granted to You by any and all Contributors for the Covered Software under Section 2.1 of this License shall terminate. 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user license agreements (excluding distributors and resellers) which have been validly granted by You or Your distributors under this License prior to termination shall survive termination. ************************************************************************ * * * 6. Disclaimer of Warranty * * ------------------------- * * * * Covered Software is provided under this License on an "as is" * * basis, without warranty of any kind, either expressed, implied, or * * statutory, including, without limitation, warranties that the * * Covered Software is free of defects, merchantable, fit for a * * particular purpose or non-infringing. The entire risk as to the * * quality and performance of the Covered Software is with You. * * Should any Covered Software prove defective in any respect, You * * (not any Contributor) assume the cost of any necessary servicing, * * repair, or correction. This disclaimer of warranty constitutes an * * essential part of this License. No use of any Covered Software is * * authorized under this License except under this disclaimer. * * * ************************************************************************ ************************************************************************ * * * 7. Limitation of Liability * * -------------------------- * * * * Under no circumstances and under no legal theory, whether tort * * (including negligence), contract, or otherwise, shall any * * Contributor, or anyone who distributes Covered Software as * * permitted above, be liable to You for any direct, indirect, * * special, incidental, or consequential damages of any character * * including, without limitation, damages for lost profits, loss of * * goodwill, work stoppage, computer failure or malfunction, or any * * and all other commercial damages or losses, even if such party * * shall have been informed of the possibility of such damages. This * * limitation of liability shall not apply to liability for death or * * personal injury resulting from such party's negligence to the * * extent applicable law prohibits such limitation. Some * * jurisdictions do not allow the exclusion or limitation of * * incidental or consequential damages, so this exclusion and * * limitation may not apply to You. * * * ************************************************************************ 8. Litigation ------------- Any litigation relating to this License may be brought only in the courts of a jurisdiction where the defendant maintains its principal place of business and such litigation shall be governed by laws of that jurisdiction, without reference to its conflict-of-law provisions. Nothing in this Section shall prevent a party's ability to bring cross-claims or counter-claims. 9. Miscellaneous ---------------- This License represents the complete agreement concerning the subject matter hereof. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. Any law or regulation which provides that the language of a contract shall be construed against the drafter shall not be used to construe this License against a Contributor. 10. Versions of the License --------------------------- 10.1. New Versions Mozilla Foundation is the license steward. Except as provided in Section 10.3, no one other than the license steward has the right to modify or publish new versions of this License. Each version will be given a distinguishing version number. 10.2. Effect of New Versions You may distribute the Covered Software under the terms of the version of the License under which You originally received the Covered Software, or under the terms of any subsequent version published by the license steward. 10.3. Modified Versions If you create software not governed by this License, and you want to create a new license for such software, you may create and use a modified version of this License if you rename the license and remove any references to the name of the license steward (except to note that such modified license differs from this License). 10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses If You choose to distribute Source Code Form that is Incompatible With Secondary Licenses under the terms of this version of the License, the notice described in Exhibit B of this License must be attached. Exhibit A - Source Code Form License Notice ------------------------------------------- This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice. You may add additional accurate notices of copyright ownership. Exhibit B - "Incompatible With Secondary Licenses" Notice --------------------------------------------------------- This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. ================================================ FILE: README.md ================================================ # synt [![Circle CI](https://circleci.com/gh/brentlintner/synt.svg?style=shield)](https://circleci.com/gh/brentlintner/synt) [![Windows Build status](https://ci.appveyor.com/api/projects/status/t2hy3nxlqc685n1m/branch/master?svg=true)](https://ci.appveyor.com/project/brentlintner/synt/branch/master) [![npm version](https://badge.fury.io/js/synt.svg)](https://badge.fury.io/js/synt) Find similar functions and classes in your JavaScript/TypeScript code. ## Supported Languages * JavaScript ([ES3-ES15](https://github.com/eslint/espree?tab=readme-ov-file#options)) * TypeScript ([5.x](https://github.com/microsoft/TypeScript/tree/v5.3.3)) ## System Requirements * [NodeJS](http://nodejs.org) ## Installation & Usage ```sh npm i synt ``` ### CLI *install* ```sh npm i -g synt ``` *help* ```sh synt -h ``` *example* ```sh git clone https://github.com/brentlintner/synt.git cd synt synt analyze src ``` ### Library *example* ```javascript const synt = require("synt") const files = [ "a.js", "b.ts" ] const { js, ts } = synt.compare(files, { similarity: 80, ngram: 1, minLength: 20, sourceType: "module", ecmaVersion: 6 }) synt.print(js) synt.print(ts) ``` ## Licensing This project is licensed under the [MPL-2.0](LICENSE) license. Any contributions made to this project are made under the current license. ## Contributing Any contributions are welcome and appreciated! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for more info. ## Versioning This project ascribes to [semantic versioning](http://semver.org). ## Name `synt` is short for [synteny](http://en.wikipedia.org/wiki/Synteny), and is an (attempted) play on comparing code evolution to genetic (evolution). ================================================ FILE: appveyor.yml ================================================ version: "1.0.{build}" cache: - node_modules -> package.json environment: nodejs_version: 20 matrix: fast_finish: true platform: - x64 init: - git config --global core.autocrlf input skip_tags: true clone_depth: 10 build: off install: - ps: Install-Product node $env:nodejs_version - ver && node --version && npm --version - npm install test_script: - npm run compile - npm run test ================================================ FILE: bin/list-env-versions ================================================ #!/usr/bin/env sh uname -a node --version npm --version ================================================ FILE: bin/release ================================================ #!/usr/bin/env sh level=$1 set -e if [ -z "${level}" ]; then level=patch fi npm run -s compile npm t git add -f lib npm run -s release -- "$level" ================================================ FILE: bin/synt ================================================ #!/usr/bin/env node require('./../lib/cli').interpret(process.argv) ================================================ FILE: coffeelint.json ================================================ { "arrow_spacing": { "level": "warn" }, "camel_case_classes": { "level": "error" }, "coffeescript_error": { "level": "error" }, "colon_assignment_spacing": { "level": "ignore", "spacing": { "left": 0, "right": 0 } }, "cyclomatic_complexity": { "value": 4, "level": "warn" }, "duplicate_key": { "level": "error" }, "empty_constructor_needs_parens": { "level": "warn" }, "indentation": { "value": 2, "level": "error" }, "line_endings": { "level": "warn", "value": "unix" }, "max_line_length": { "value": 100, "level": "error", "limitComments": true }, "missing_fat_arrows": { "level": "warn" }, "newlines_after_classes": { "value": 3, "level": "warn" }, "no_backticks": { "level": "error" }, "no_debugger": { "level": "warn" }, "no_empty_functions": { "level": "warn" }, "no_empty_param_list": { "level": "ignore" }, "no_implicit_braces": { "level": "ignore", "strict": true }, "no_implicit_parens": { "strict": true, "level": "ignore" }, "no_interpolation_in_single_quotes": { "level": "warn" }, "no_plusplus": { "level": "warn" }, "no_stand_alone_at": { "level": "warn" }, "no_tabs": { "level": "error" }, "no_throwing_strings": { "level": "error" }, "no_trailing_semicolons": { "level": "error" }, "no_trailing_whitespace": { "level": "error", "allowed_in_comments": false, "allowed_in_empty_lines": false }, "no_unnecessary_double_quotes": { "level": "ignore" }, "no_unnecessary_fat_arrows": { "level": "warn" }, "non_empty_constructor_needs_parens": { "level": "ignore" }, "space_operators": { "level": "warn" } } ================================================ FILE: lib/cli/file_collector.js ================================================ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.files = void 0; const fs = require("fs"); const path = require("path"); const _ = require("lodash"); const walk_sync = require("walk-sync"); const all_files = (target) => { if (fs.statSync(target).isDirectory()) { const dirs = walk_sync(target, { directories: false }); return _.map(dirs, (dir) => path.join(target, dir)); } else { return [target]; } }; const normalize_cli_targets = (targets) => { targets = _.concat([], targets); let files = _.uniq(_.reduce(targets, (paths, target) => _.concat(paths, all_files(target)), [])); files = _.map(files, (file) => path.relative(process.cwd(), file)); return _.filter(files, (file) => /\.(js|ts)$/.test(file)); }; exports.files = normalize_cli_targets; ================================================ FILE: lib/cli.js ================================================ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.interpret = void 0; const commander_1 = require("commander"); const espree = require("espree"); const similar = require("./similar"); const fs_collector = require("./cli/file_collector"); const pkg = require("./../package.json"); const compare = (targets, opts) => { const files = fs_collector.files(targets); const ecmaVersion = opts.ecmaVersion && opts.ecmaVersion != "latest" ? parseInt(opts.ecmaVersion) : opts.ecmaVersion; const { js, ts } = similar.compare(files, Object.assign(Object.assign({}, opts), { ecmaVersion })); similar.print(js, opts.color); similar.print(ts, opts.color); if (opts.exitCode && (Object.keys(js).length > 0 || Object.keys(ts).length > 0)) { process.exit(1); } }; const configure = () => { commander_1.program .version(pkg.version) .command("analyze [paths...]") .alias("a") .option("-s, --similarity [number]", `Lowest % similarity to look for ` + `[default=${similar.DEFAULT_THRESHOLD}].`) .option("-m, --min-length [number]", `Default token length a function needs to be to compare it ` + `[default=${similar.DEFAULT_TOKEN_LENGTH}].`) .option("-g, --ngram [number]", `Specify ngram length for comparing token sequences. ` + `[default=${similar.DEFAULT_NGRAM_LENGTH},2,3...]`) .option("-n, --no-color", "Disable color output") .option("-e, --exit-code", "Exit with a nonzero code when issues found") .option("-t, --source-type [value]", "Set JS source type [default=module,script,commonjs]") .option("-a, --ecma-version [value]", `Set JS version [default=latest,${espree.supportedEcmaVersions.join(",")}]`) .action(compare); commander_1.program.on("--help", () => { console.log(" Command specific help:"); console.log(""); console.log(" {cmd} -h, --help"); console.log(""); console.log(" Examples:"); console.log(""); console.log(" $ synt analyze lib"); console.log(" $ synt analyze -s 90 foo.js bar.js baz.js"); console.log(""); }); }; const interpret = (argv) => { configure(); commander_1.program.parse(argv); }; exports.interpret = interpret; ================================================ FILE: lib/index.js ================================================ "use strict"; const similar = require("./similar"); const library = similar; module.exports = library; ================================================ FILE: lib/similar/javascript.js ================================================ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.find = void 0; const escodegen = require("@javascript-obfuscator/escodegen"); const espree = require("espree"); const estraverse = require("estraverse"); const fs = require("fs"); const _ = require("lodash"); const FUNCTION_OR_CLASS_NODE = [ espree.Syntax.ArrowFunctionExpression, espree.Syntax.ClassDeclaration, espree.Syntax.FunctionDeclaration, espree.Syntax.FunctionExpression ]; const normalize = (token_list) => _.map(token_list, (t) => t.value); const tokenize = (code, opts) => { const tokenize_opts = { ecmaVersion: opts.ecmaVersion || "latest", sourceType: opts.sourceType || "module" }; return normalize(espree.tokenize(code, tokenize_opts)); }; const astify = (code, opts) => { const module_type = opts.sourceType || "module"; const ecma_version = opts.ecmaVersion || "latest"; const parse_opts = { ecmaVersion: ecma_version, loc: true, sourceType: module_type }; return espree.parse(code, parse_opts); }; const ast_to_code = (node) => { const opts = { format: { indent: { style: " " } } }; return escodegen.generate(node, opts); }; const is_a_method_or_class = (node) => _.some(FUNCTION_OR_CLASS_NODE, (type) => type === node.type); const line_info = (node) => node.loc; const parse_methods_and_classes = (root_node, filepath, opts) => { const entries = []; estraverse.traverse(root_node, { enter(node, _parent) { if (!is_a_method_or_class(node)) return; const method = ast_to_code(node); const tokens = tokenize(method, opts); const is_class = node.type === espree.Syntax.ClassDeclaration; const result = { ast: node, code: method, is_class, path: filepath, pos: line_info(node), tokens, type: node.type }; entries.push(result); } }); return entries; }; const find_similar_methods_and_classes = (filepaths, opts) => _.flatMap(filepaths, (filepath) => { const code = fs.readFileSync(filepath).toString(); let node; try { node = astify(code, opts); } catch (err) { throw new Error(`in ${filepath}\n\n${_.get(err, "stack", err)}`); } return parse_methods_and_classes(node, filepath, opts); }); exports.find = find_similar_methods_and_classes; ================================================ FILE: lib/similar/ngram.js ================================================ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generate = void 0; const generate = (arr, len = 1) => { if (len > arr.length) len = 1; if (len == 1) return arr; const sets = []; arr.forEach((token, index) => { const s_len = index + len; if (s_len <= arr.length) { sets.push(arr.slice(index, s_len).join("")); } }); return sets; }; exports.generate = generate; ================================================ FILE: lib/similar/print.js ================================================ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.print = void 0; const _ = require("lodash"); const cardinal = require("cardinal"); const chalk = require("chalk"); const print = (group, color) => { _.each(group, (results, sim) => { _.each(results, (result) => { const [src, cmp] = result; console.log(""); const match_sim = sim + "% similar"; if (color) { console.log(chalk.red.bold(match_sim)); } else { console.log(match_sim); } console.log(""); if (color) { console.log(chalk.gray("in: ") + chalk.green(src.path)); } else { console.log(`in: ${src.path}`); } console.log(""); if (color) { console.log(cardinal.highlight(src.code, { firstline: src.pos.start.line, linenos: true })); } else { console.log(src.code.split("\n").map((line, idx) => { return `${src.pos.start.line + idx + 1}: ${line}`; }).join("\n")); } console.log(""); if (src.path !== cmp.path) { if (color) { console.log(chalk.gray("in: ") + chalk.green(cmp.path)); } else { console.log(`in: ${cmp.path}`); } console.log(""); } if (color) { console.log(cardinal.highlight(cmp.code, { firstline: cmp.pos.start.line, linenos: true })); } else { console.log(cmp.code.split("\n").map((line, idx) => { return `${cmp.pos.start.line + idx + 1}: ${line}`; }).join("\n")); } }); }); }; exports.print = print; ================================================ FILE: lib/similar/typescript.js ================================================ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.find = void 0; const _ = require("lodash"); const fs = require("fs"); const ts = require("typescript"); const STOP_AT_NODES = [ ts.SyntaxKind.ArrayType, ts.SyntaxKind.PrefixUnaryExpression, ts.SyntaxKind.RegularExpressionLiteral ]; const FUNCTION_OR_CLASS_NODE = [ ts.SyntaxKind.ArrowFunction, ts.SyntaxKind.ClassDeclaration, ts.SyntaxKind.Constructor, ts.SyntaxKind.FunctionDeclaration, ts.SyntaxKind.FunctionExpression, ts.SyntaxKind.MethodDeclaration ]; const PASSTHROUGH_NODES = FUNCTION_OR_CLASS_NODE.concat([ ts.SyntaxKind.ArrowFunction, ts.SyntaxKind.ArrayLiteralExpression, ts.SyntaxKind.BinaryExpression, ts.SyntaxKind.Block, ts.SyntaxKind.CallExpression, ts.SyntaxKind.CatchClause, ts.SyntaxKind.ConditionalExpression, ts.SyntaxKind.ElementAccessExpression, ts.SyntaxKind.ExpressionStatement, ts.SyntaxKind.FirstNode, ts.SyntaxKind.FunctionExpression, ts.SyntaxKind.FunctionDeclaration, ts.SyntaxKind.FunctionType, ts.SyntaxKind.IfStatement, ts.SyntaxKind.IndexSignature, ts.SyntaxKind.NewExpression, ts.SyntaxKind.ObjectLiteralExpression, ts.SyntaxKind.Parameter, ts.SyntaxKind.ParenthesizedExpression, ts.SyntaxKind.PropertyAccessExpression, ts.SyntaxKind.PropertyAssignment, ts.SyntaxKind.PropertyDeclaration, ts.SyntaxKind.ReturnStatement, ts.SyntaxKind.TemplateExpression, ts.SyntaxKind.TemplateSpan, ts.SyntaxKind.TryStatement, ts.SyntaxKind.TypeReference, ts.SyntaxKind.TypeLiteral, ts.SyntaxKind.VariableDeclaration, ts.SyntaxKind.VariableDeclarationList, ts.SyntaxKind.VariableStatement ]); const astify = (code, filepath, _opts) => { ts.createProgram([filepath], {}); return ts.createSourceFile(filepath, code, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS); }; const is_a_base_node = (node) => _.some(STOP_AT_NODES, (kind) => kind === node.kind); const is_a_passthrough_node = (node) => _.some(PASSTHROUGH_NODES, (kind) => kind === node.kind); const is_a_method_or_class = (node) => _.some(FUNCTION_OR_CLASS_NODE, (kind) => kind === node.kind); const _tokenize = (tokens) => (node) => { if (is_a_base_node(node)) { tokens.push(node.getText()); } else if (is_a_passthrough_node(node)) { ts.forEachChild(node, _tokenize(tokens)); } else { tokens.push(node.getText()); ts.forEachChild(node, _tokenize(tokens)); } }; const tokenize = (ast_node) => { const tokens = []; _tokenize(tokens)(ast_node); return tokens; }; const find_all_methods_and_classes = (list) => (node) => { if (is_a_method_or_class(node)) { list.push(node); } ts.forEachChild(node, find_all_methods_and_classes(list)); }; const line_info = (node, root_node) => { const { line, character } = root_node .getLineAndCharacterOfPosition(node.getStart()); return { start: { column: character, line: line + 1 } }; }; const parse_methods_and_classes = (node, root_node, path) => { const methods_and_classes = []; find_all_methods_and_classes(methods_and_classes)(node); return _.map(methods_and_classes, (method_or_class) => { const tokens = tokenize(method_or_class); const code = method_or_class.getText(); const is_class = method_or_class.kind == ts.SyntaxKind.ClassDeclaration; return { ast: method_or_class, code, is_class, path, pos: line_info(method_or_class, root_node), tokens, type: ts.SyntaxKind[method_or_class.kind] }; }); }; const find_similar_methods_and_classes = (filepaths, opts) => _.flatMap(filepaths, (filepath) => { const code = fs.readFileSync(filepath).toString(); const node = astify(code, filepath, opts); const root_node = node; return parse_methods_and_classes(node, root_node, filepath); }); exports.find = find_similar_methods_and_classes; ================================================ FILE: lib/similar.js ================================================ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.print = exports.compare = exports.DEFAULT_TOKEN_LENGTH = exports.DEFAULT_THRESHOLD = exports.DEFAULT_NGRAM_LENGTH = void 0; const _ = require("lodash"); const ngram = require("./similar/ngram"); const parse_js = require("./similar/javascript"); const parse_ts = require("./similar/typescript"); const print_1 = require("./similar/print"); Object.defineProperty(exports, "print", { enumerable: true, get: function () { return print_1.print; } }); const DEFAULT_NGRAM_LENGTH = 1; exports.DEFAULT_NGRAM_LENGTH = DEFAULT_NGRAM_LENGTH; const DEFAULT_THRESHOLD = 70; exports.DEFAULT_THRESHOLD = DEFAULT_THRESHOLD; const DEFAULT_TOKEN_LENGTH = 10; exports.DEFAULT_TOKEN_LENGTH = DEFAULT_TOKEN_LENGTH; const similarity = (src, cmp) => { const a = _.uniq(src); const b = _.uniq(cmp); const i = _.intersection(a, b); const u = _.union(a, b); return _.toNumber(_.toNumber((i.length / u.length) * 100) .toFixed(0)); }; const parse_token_length = (str) => _.isEmpty(str) ? DEFAULT_TOKEN_LENGTH : _.toNumber(str); const parse_ngram_length = (str) => _.isEmpty(str) ? DEFAULT_NGRAM_LENGTH : _.toNumber(str); const parse_threshold = (str) => { const threshold = _.toNumber(str); return threshold || DEFAULT_THRESHOLD; }; const is_ts_ancestor = (src, cmp) => { let match = false; let last = src.ast; while (true) { const { parent } = last; if (parent === cmp.ast) match = true; if (!parent || last === parent || match) break; last = parent; } return match; }; const false_positive = (src, cmp, t_len) => { const same_node = () => cmp.ast === src.ast; const size_is_too_different = () => { const l1 = src.tokens.length; const l2 = cmp.tokens.length; return l1 * 2 < l2 || l2 * 2 < l1; }; const one_is_too_short = () => src.tokens.length < t_len || cmp.tokens.length < t_len; const subset_of_other = () => { const is_eithers_ancestor = () => is_ts_ancestor(src, cmp) || is_ts_ancestor(cmp, src); const is_eithers_middle = () => { const src_j = src.tokens.join(""); const cmp_j = cmp.tokens.join(""); return src_j !== cmp_j && (_.includes(src_j, cmp_j) || _.includes(cmp_j, src_j)); }; return is_eithers_ancestor() || is_eithers_middle(); }; const both_are_not_classes = () => (src.is_class && !cmp.is_class) || (!src.is_class && cmp.is_class); return same_node() || both_are_not_classes() || subset_of_other() || one_is_too_short() || size_is_too_different(); }; const each_pair = (items, callback) => { _.each(items, (src) => { _.each(items, (cmp) => { callback(src, cmp); }); }); }; const filter_redundencies = (group) => { _.each(group, (results, sim) => { group[sim] = _.reduce(results, (new_arr, result) => { const already_added = _.some(new_arr, (result_two) => _.xor(result, result_two).length === 0); if (!already_added) { new_arr.push(result); } return new_arr; }, []); }); return group; }; const _compare = (files, ftype, opts) => { const is_ts = ftype === "ts"; const is_file = is_ts ? /\.ts$/ : /\.js$/; files = _.filter(files, (file) => is_file.test(file)); const parse = is_ts ? parse_ts : parse_js; const items = parse.find(files, opts); const group = {}; const t_len = parse_token_length(_.toString(opts.minLength)); const n_len = parse_ngram_length(_.toString(opts.ngram)); const sim_min = parse_threshold(_.toString(opts.similarity)); each_pair(items, (src, cmp) => { if (false_positive(src, cmp, t_len)) return; const src_grams = ngram.generate(src.tokens, n_len); const cmp_grams = ngram.generate(cmp.tokens, n_len); const val = similarity(src_grams, cmp_grams); if (val < sim_min) return; if (_.isEmpty(group[val])) group[val] = []; group[val].push([src, cmp]); }); return filter_redundencies(group); }; const compare = (files, opts = {}) => { files = _.concat([], files); const js_group = _compare(files, "js", opts); const ts_group = _compare(files, "ts", opts); return { js: js_group, ts: ts_group }; }; exports.compare = compare; ================================================ FILE: package.json ================================================ { "name": "synt", "version": "0.5.0", "description": "Find similar functions and classes in your JavaScript/TypeScript code", "author": "Brent Lintner ", "repository": { "type": "git", "url": "git://github.com/brentlintner/synt.git" }, "main": "lib/index.js", "types": "src/@types/index.d.ts", "bin": { "synt": "bin/synt" }, "files": [ "bin/synt", "src/@types", "lib", "LICENSE", "README.md" ], "engines": { "node": ">=4.0.0", "npm": ">=2.0.0" }, "keywords": [ "duplicate", "similar", "functions", "methods", "classes", "code", "analysis", "compare", "javascript", "typescript" ], "license": "MPL-2.0", "bugs": { "url": "https://github.com/brentlintner/synt/issues" }, "homepage": "https://github.com/brentlintner/synt", "scripts": { "compile": "tsc", "clean": "rm -rf {.nyc_output,coverage,lib} && git checkout -- lib", "dev": "tsc -w", "lint": "eslint .; coffeelint -q test", "lint-cov": "nyc check-coverage --statements 100 --functions 100 --branches 100 --lines 100", "release": "release-it", "test": "mocha --require coffeescript/register -R dot -s 2000 --recursive 'test/spec/**/*.coffee'", "test-cov": "nyc -r lcov -r text -x test/fixtures npm run test", "test-ci-build": "npm run lint && npm run compile && npm run test-cov && npm run lint-cov", "tsc": "tsc" }, "dependencies": { "@javascript-obfuscator/escodegen": "^2.3.0", "@types/escodegen": "0.0.10", "@types/esprima": "^4.0.6", "@types/estraverse": "^5.1.7", "@types/estree": "^1.0.5", "@types/lodash": "^4.14.202", "@types/node": "^20.10.5", "cardinal": "^2.1.1", "chalk": "^4.1.2", "commander": "^11.1.0", "espree": "^9.6.1", "estraverse": "^5.3.0", "lodash": "^4.17.21", "typescript": "^5.3.3", "walk-sync": "^3.0.0" }, "devDependencies": { "@stylistic/eslint-plugin-ts": "^1.5.1", "@typescript-eslint/eslint-plugin": "^6.15.0", "@typescript-eslint/parser": "^6.15.0", "chai": "^4.3.10", "coffeelint": "^2.1.0", "coffeescript": "^2.7.0", "eslint": "^8.56.0", "eslint-plugin-jsdoc": "^46.9.1", "eslint-plugin-prefer-arrow": "^1.2.3", "mocha": "^10.2.0", "npm-check-updates": "^16.14.12", "nyc": "^15.1.0", "release-it": "^17.0.1", "retire": "^4.3.4", "sinon": "^17.0.1", "sinon-chai": "^3.7.0" } } ================================================ FILE: src/@types/@javascript-obfuscator/index.d.ts ================================================ declare module "@javascript-obfuscator/escodegen" { import * as ec from "escodegen" import * as et from "estree" export function generate(ast : et.Node, options ?: ec.GenerateOptions) : string } ================================================ FILE: src/@types/cardinal/index.d.ts ================================================ declare module "cardinal" { export interface CardinalOptions { linenos ?: boolean firstline ?: number theme ?: string } function highlight( code : string, opts ?: CardinalOptions ) : string } ================================================ FILE: src/@types/espree/index.d.ts ================================================ declare module "espree" { import * as es from "esprima" export const supportedEcmaVersions : string[] export interface Options { range ?: boolean loc ?: boolean comment ?: boolean tokens ?: boolean ecmaVersion ?: string | number allowReserved ?: boolean sourceType ?: string ecmaFeatures ?: { jsx ?: boolean globalReturn ?: boolean impliedStrict ?: boolean } } export const Syntax : typeof es.Syntax export type Token = es.Token export function parse(code : string, options ?: Options) : es.Program export function tokenize(code : string, options ?: Options) : es.Token[] } ================================================ FILE: src/@types/index.d.ts ================================================ import * as ts from "typescript" import * as es from "estree" import * as commander from "commander" declare const synt : synt.Lib export = synt export as namespace synt declare namespace synt { export type TokenList = string[] interface Package { version : string } interface BaseOptions { minLength ?: string | number ngram ?: string | number similarity ?: string | number sourceType ?: string } interface EcmaVersionOptions { ecmaVersion ?: string | number } export interface CLIOptions extends BaseOptions, commander.Command { color ?: boolean exitCode ?: boolean ecmaVersion ?: string } export interface CompareOptions extends BaseOptions, EcmaVersionOptions {} interface ParseResultBase { code : string is_class : boolean path : string pos : LineInfo tokens : string[] type : string } export interface TSParseResult extends ParseResultBase { ast : ts.Node } export interface JSParseResult extends ParseResultBase { ast : es.Node } export type ParseResult = JSParseResult | TSParseResult export type ParseResultMatchList = ParseResult[][] export interface ParseResultGroup { [sim : string] : ParseResultMatchList } export interface ParseResultGroups { js : ParseResultGroup ts : ParseResultGroup } export interface LineInfo { start ?: { line : number column ?: number } end ?: { line : number column ?: number } } export interface Lib { compare : ( files : string[], opts : CompareOptions ) => ParseResultGroups print : ( group : synt.ParseResultGroup, nocolors : boolean ) => void DEFAULT_NGRAM_LENGTH : number DEFAULT_THRESHOLD : number DEFAULT_TOKEN_LENGTH : number } } ================================================ FILE: src/@types/walk-sync/index.d.ts ================================================ declare module "walk-sync" { interface WalkSyncOptions { directories ?: boolean } function walk_sync( target : string, opts ?: WalkSyncOptions ) : string[] export = walk_sync } ================================================ FILE: src/cli/file_collector.ts ================================================ import * as fs from "fs" import * as path from "path" import * as _ from "lodash" import walk_sync = require("walk-sync") const all_files = (target : string) : string[] => { if (fs.statSync(target).isDirectory()) { const dirs = walk_sync(target, { directories: false }) return _.map(dirs, (dir : string) => path.join(target, dir)) } else { return [ target ] } } const normalize_cli_targets = ( targets : string | string[] ) : string[] => { targets = _.concat([], targets) let files : string[] = _.uniq(_.reduce(targets, (paths : string[], target : string) => _.concat(paths, all_files(target)), [])) files = _.map(files, (file : string) => path.relative(process.cwd(), file)) return _.filter(files, (file : string) => /\.(js|ts)$/.test(file)) } export { normalize_cli_targets as files } ================================================ FILE: src/cli.ts ================================================ import { program } from "commander" import * as espree from 'espree' import * as similar from "./similar" import * as fs_collector from "./cli/file_collector" const pkg = require("./../package.json") as synt.Package const compare = ( targets : string[], opts : synt.CLIOptions ) => { const files = fs_collector.files(targets) const ecmaVersion = opts.ecmaVersion && opts.ecmaVersion != "latest" ? parseInt(opts.ecmaVersion) : opts.ecmaVersion const { js, ts } = similar.compare(files, { ...opts, ecmaVersion }) similar.print(js, opts.color) similar.print(ts, opts.color) if (opts.exitCode && (Object.keys(js).length > 0 || Object.keys(ts).length > 0)) { process.exit(1) } } const configure = () => { program .version(pkg.version) .command("analyze [paths...]") .alias("a") .option( "-s, --similarity [number]", `Lowest % similarity to look for ` + `[default=${ similar.DEFAULT_THRESHOLD }].`) .option( "-m, --min-length [number]", `Default token length a function needs to be to compare it ` + `[default=${ similar.DEFAULT_TOKEN_LENGTH }].`) .option( "-g, --ngram [number]", `Specify ngram length for comparing token sequences. ` + `[default=${ similar.DEFAULT_NGRAM_LENGTH },2,3...]`) .option("-n, --no-color", "Disable color output") .option("-e, --exit-code", "Exit with a nonzero code when issues found") .option( "-t, --source-type [value]", "Set JS source type [default=module,script,commonjs]") .option( "-a, --ecma-version [value]", `Set JS version [default=latest,${espree.supportedEcmaVersions.join(",")}]`) .action(compare) program.on("--help", () => { console.log(" Command specific help:") console.log("") console.log(" {cmd} -h, --help") console.log("") console.log(" Examples:") console.log("") console.log(" $ synt analyze lib") console.log(" $ synt analyze -s 90 foo.js bar.js baz.js") console.log("") }) } const interpret = (argv : string[]) => { configure() program.parse(argv) } export { interpret } ================================================ FILE: src/index.ts ================================================ import * as similar from "./similar" const library : synt.Lib = similar export = library ================================================ FILE: src/similar/javascript.ts ================================================ import * as escodegen from "@javascript-obfuscator/escodegen" import * as espree from "espree" import * as estraverse from "estraverse" import * as es from "estree" import * as fs from "fs" import * as _ from "lodash" const FUNCTION_OR_CLASS_NODE : string[] = [ espree.Syntax.ArrowFunctionExpression, espree.Syntax.ClassDeclaration, espree.Syntax.FunctionDeclaration, espree.Syntax.FunctionExpression // TODO: (FE/FD causes dupe of MD) // espree.Syntaxt.MethodDefinition ] const normalize = ( token_list : espree.Token[] ) : string[] => _.map(token_list, (t : espree.Token) => t.value) const tokenize = (code : string, opts : synt.CompareOptions) : string[] => { const tokenize_opts : espree.Options = { ecmaVersion: opts.ecmaVersion || "latest", sourceType: opts.sourceType || "module" } return normalize(espree.tokenize(code, tokenize_opts)) } const astify = ( code : string, opts : synt.CompareOptions ) : es.Program => { const module_type : string = opts.sourceType || "module" const ecma_version : string | number = opts.ecmaVersion || "latest" const parse_opts : espree.Options = { ecmaVersion: ecma_version, loc: true, sourceType: module_type } return espree.parse(code, parse_opts) } const ast_to_code = (node : es.Node) => { const opts = { format: { indent: { style: " " } } } return escodegen.generate(node, opts) } const is_a_method_or_class = (node : es.Node) => _.some(FUNCTION_OR_CLASS_NODE, (type) => type === node.type) const line_info = (node : es.Node) : es.SourceLocation => node.loc const parse_methods_and_classes = ( root_node : es.Node, filepath : string, opts : synt.CompareOptions ) : synt.ParseResult[] => { const entries : synt.ParseResult[] = [] // TODO: need to construct a proper parent chain estraverse.traverse(root_node, { enter(node : es.Node, _parent : es.Node) { if (!is_a_method_or_class(node)) return const method = ast_to_code(node) const tokens = tokenize(method, opts) const is_class = node.type === espree.Syntax.ClassDeclaration const result = { ast: node, code: method, is_class, path: filepath, pos: line_info(node), tokens, type: node.type } entries.push(result) } }) return entries } const find_similar_methods_and_classes = ( filepaths : string[], opts : synt.CompareOptions ) : synt.ParseResult[] => _.flatMap(filepaths, (filepath) => { const code = fs.readFileSync(filepath).toString() let node : es.Node try { node = astify(code, opts) } catch (err) { throw new Error(`in ${filepath}\n\n${_.get(err, "stack", err)}`) } return parse_methods_and_classes(node, filepath, opts) }) export { find_similar_methods_and_classes as find } ================================================ FILE: src/similar/ngram.ts ================================================ const generate = ( arr : string[], len = 1 ) : string[] => { if (len > arr.length) len = 1 if (len == 1) return arr const sets : string[] = [] arr.forEach((token : string, index : number) => { const s_len = index + len if (s_len <= arr.length) { sets.push(arr.slice(index, s_len).join("")) } }) return sets } export { generate } ================================================ FILE: src/similar/print.ts ================================================ import * as _ from "lodash" import * as cardinal from "cardinal" import * as chalk from "chalk" const print = ( group : synt.ParseResultGroup, color : boolean ) : void => { _.each(group, (results : synt.ParseResultMatchList, sim : string) => { _.each(results, (result : synt.ParseResult[]) => { const [src, cmp] = result console.log("") const match_sim = sim + "% similar" if (color) { console.log(chalk.red.bold(match_sim)) } else { console.log(match_sim) } console.log("") if (color) { console.log(chalk.gray("in: ") + chalk.green(src.path)) } else { console.log(`in: ${ src.path }`) } console.log("") if (color) { console.log(cardinal.highlight(src.code, { firstline: src.pos.start.line, linenos: true })) } else { console.log(src.code.split("\n").map((line : string, idx : number) => { return `${src.pos.start.line + idx + 1}: ${line}` }).join("\n")) } console.log("") if (src.path !== cmp.path) { if (color) { console.log(chalk.gray("in: ") + chalk.green(cmp.path)) } else { console.log(`in: ${ cmp.path }`) } console.log("") } if (color) { console.log(cardinal.highlight(cmp.code, { firstline: cmp.pos.start.line, linenos: true })) } else { console.log(cmp.code.split("\n").map((line : string, idx : number) => { return `${cmp.pos.start.line + idx + 1}: ${line}` }).join("\n")) } }) }) } export { print } ================================================ FILE: src/similar/typescript.ts ================================================ import * as _ from "lodash" import * as fs from "fs" import * as ts from "typescript" const STOP_AT_NODES : ts.SyntaxKind[] = [ ts.SyntaxKind.ArrayType, ts.SyntaxKind.PrefixUnaryExpression, ts.SyntaxKind.RegularExpressionLiteral ] const FUNCTION_OR_CLASS_NODE : ts.SyntaxKind[] = [ ts.SyntaxKind.ArrowFunction, ts.SyntaxKind.ClassDeclaration, ts.SyntaxKind.Constructor, ts.SyntaxKind.FunctionDeclaration, ts.SyntaxKind.FunctionExpression, ts.SyntaxKind.MethodDeclaration ] const PASSTHROUGH_NODES : ts.SyntaxKind[] = FUNCTION_OR_CLASS_NODE.concat([ ts.SyntaxKind.ArrowFunction, ts.SyntaxKind.ArrayLiteralExpression, ts.SyntaxKind.BinaryExpression, ts.SyntaxKind.Block, ts.SyntaxKind.CallExpression, ts.SyntaxKind.CatchClause, ts.SyntaxKind.ConditionalExpression, ts.SyntaxKind.ElementAccessExpression, ts.SyntaxKind.ExpressionStatement, ts.SyntaxKind.FirstNode, ts.SyntaxKind.FunctionExpression, ts.SyntaxKind.FunctionDeclaration, ts.SyntaxKind.FunctionType, ts.SyntaxKind.IfStatement, ts.SyntaxKind.IndexSignature, ts.SyntaxKind.NewExpression, ts.SyntaxKind.ObjectLiteralExpression, ts.SyntaxKind.Parameter, ts.SyntaxKind.ParenthesizedExpression, ts.SyntaxKind.PropertyAccessExpression, ts.SyntaxKind.PropertyAssignment, ts.SyntaxKind.PropertyDeclaration, ts.SyntaxKind.ReturnStatement, ts.SyntaxKind.TemplateExpression, ts.SyntaxKind.TemplateSpan, ts.SyntaxKind.TryStatement, ts.SyntaxKind.TypeReference, ts.SyntaxKind.TypeLiteral, ts.SyntaxKind.VariableDeclaration, ts.SyntaxKind.VariableDeclarationList, ts.SyntaxKind.VariableStatement ]) const astify = ( code : string, filepath : string, _opts : synt.CompareOptions ) : ts.SourceFile => { ts.createProgram([ filepath ], {}) return ts.createSourceFile( filepath, code, ts.ScriptTarget.Latest, true, // setParentNodes ts.ScriptKind.TS) } const is_a_base_node = (node : ts.Node) : boolean => _.some(STOP_AT_NODES, (kind) => kind === node.kind) const is_a_passthrough_node = (node : ts.Node) : boolean => _.some(PASSTHROUGH_NODES, (kind) => kind === node.kind) const is_a_method_or_class = (node : ts.Node) : boolean => _.some(FUNCTION_OR_CLASS_NODE, (kind) => kind === node.kind) const _tokenize = ( tokens : string[] ) => ( node : ts.Node ) => { if (is_a_base_node(node)) { tokens.push(node.getText()) } else if (is_a_passthrough_node(node)) { ts.forEachChild(node, _tokenize(tokens)) } else { tokens.push(node.getText()) ts.forEachChild(node, _tokenize(tokens)) } } const tokenize = (ast_node : ts.Node) : string[] => { const tokens : string[] = [] _tokenize(tokens)(ast_node) return tokens } const find_all_methods_and_classes = ( list : ts.Node[] ) => ( node : ts.Node ) => { if (is_a_method_or_class(node)) { list.push(node) } ts.forEachChild(node, find_all_methods_and_classes(list)) } const line_info = ( node : ts.Node, root_node : ts.SourceFile ) : synt.LineInfo => { const { line, character } = root_node .getLineAndCharacterOfPosition(node.getStart()) return { start: { column: character, line: line + 1 // zero-indexed } } } const parse_methods_and_classes = ( node : ts.Node, root_node : ts.SourceFile, path : string ) : synt.ParseResult[] => { const methods_and_classes : ts.Node[] = [] find_all_methods_and_classes(methods_and_classes)(node) return _.map(methods_and_classes, (method_or_class : ts.Node) => { const tokens = tokenize(method_or_class) const code = method_or_class.getText() const is_class = method_or_class.kind == ts.SyntaxKind.ClassDeclaration return { ast: method_or_class, code, is_class, path, pos: line_info(method_or_class, root_node), tokens, type: ts.SyntaxKind[method_or_class.kind] } }) } const find_similar_methods_and_classes = ( filepaths : string[], opts : synt.CompareOptions ) : synt.ParseResult[] => _.flatMap(filepaths, (filepath) => { const code = fs.readFileSync(filepath).toString() const node = astify(code, filepath, opts) const root_node = node return parse_methods_and_classes(node, root_node, filepath) }) export { find_similar_methods_and_classes as find } ================================================ FILE: src/similar.ts ================================================ import * as _ from "lodash" import * as ngram from "./similar/ngram" import * as parse_js from "./similar/javascript" import * as parse_ts from "./similar/typescript" import { print } from "./similar/print" const DEFAULT_NGRAM_LENGTH = 1 const DEFAULT_THRESHOLD = 70 const DEFAULT_TOKEN_LENGTH = 10 // http://en.wikipedia.org/wiki/Jaccard_index const similarity = ( src : synt.TokenList, cmp : synt.TokenList ) : number => { const a = _.uniq(src) const b = _.uniq(cmp) const i = _.intersection(a, b) const u = _.union(a, b) return _.toNumber( _.toNumber((i.length / u.length) * 100) .toFixed(0)) } const parse_token_length = (str : string) : number => _.isEmpty(str) ? DEFAULT_TOKEN_LENGTH : _.toNumber(str) const parse_ngram_length = (str : string) : number => _.isEmpty(str) ? DEFAULT_NGRAM_LENGTH : _.toNumber(str) const parse_threshold = (str : string) : number => { const threshold = _.toNumber(str) return threshold || DEFAULT_THRESHOLD } const is_ts_ancestor = ( src : synt.TSParseResult, cmp : synt.TSParseResult ) : boolean => { let match = false let last = src.ast while (true) { const { parent } = last if (parent === cmp.ast) match = true if (!parent || last === parent || match) break last = parent } return match } const false_positive = ( src : synt.ParseResult, cmp : synt.ParseResult, t_len : number ) => { const same_node = () => cmp.ast === src.ast const size_is_too_different = () => { const l1 = src.tokens.length const l2 = cmp.tokens.length return l1 * 2 < l2 || l2 * 2 < l1 } const one_is_too_short = () => src.tokens.length < t_len || cmp.tokens.length < t_len // HACK: TypeScript provides a parent chain // Esprima does not (out of the box), so we // also use hacky token string check to catch all const subset_of_other = () : boolean => { const is_eithers_ancestor = () : boolean => is_ts_ancestor( (src as synt.TSParseResult), (cmp as synt.TSParseResult)) || is_ts_ancestor( (cmp as synt.TSParseResult), (src as synt.TSParseResult)) const is_eithers_middle = () : boolean => { const src_j = src.tokens.join("") const cmp_j = cmp.tokens.join("") return src_j !== cmp_j && (_.includes(src_j, cmp_j) || _.includes(cmp_j, src_j)) } return is_eithers_ancestor() || is_eithers_middle() } const both_are_not_classes = () : boolean => (src.is_class && !cmp.is_class) || (!src.is_class && cmp.is_class) return same_node() || both_are_not_classes() || subset_of_other() || one_is_too_short() || size_is_too_different() } const each_pair = ( items : synt.ParseResult[], callback : ( src : synt.ParseResult, cmp : synt.ParseResult ) => void ) : void => { _.each(items, (src) => { _.each(items, (cmp) => { callback(src, cmp) }) }) } const filter_redundencies = ( group : synt.ParseResultGroup ) : synt.ParseResultGroup => { _.each(group, ( results : synt.ParseResultMatchList, sim : string ) => { group[sim] = _.reduce( results, ( new_arr : synt.ParseResultMatchList, result : synt.ParseResult[] ) => { const already_added : boolean = _.some( new_arr, (result_two : synt.ParseResult[]) => _.xor(result, result_two).length === 0) if (!already_added) { new_arr.push(result) } return new_arr }, ([] as synt.ParseResultMatchList)) }) return group } const _compare = ( files : string[], ftype : string, opts : synt.CompareOptions ) : synt.ParseResultGroup => { const is_ts = ftype === "ts" const is_file = is_ts ? /\.ts$/ : /\.js$/ files = _.filter(files, (file) => is_file.test(file)) const parse = is_ts ? parse_ts : parse_js const items : synt.ParseResult[] = parse.find(files, opts) const group : synt.ParseResultGroup = {} const t_len = parse_token_length(_.toString(opts.minLength)) const n_len = parse_ngram_length(_.toString(opts.ngram)) const sim_min = parse_threshold(_.toString(opts.similarity)) each_pair(items, (src : synt.ParseResult, cmp : synt.ParseResult) => { if (false_positive(src, cmp, t_len)) return const src_grams = ngram.generate(src.tokens, n_len) const cmp_grams = ngram.generate(cmp.tokens, n_len) const val = similarity(src_grams, cmp_grams) if (val < sim_min) return if (_.isEmpty(group[val])) group[val] = [] group[val].push([src, cmp]) }) return filter_redundencies(group) } const compare = ( files : string[], opts : synt.CompareOptions = {} ) : synt.ParseResultGroups => { files = _.concat([], files) const js_group = _compare(files, "js", opts) const ts_group = _compare(files, "ts", opts) return { js: js_group, ts: ts_group } } export { DEFAULT_NGRAM_LENGTH, DEFAULT_THRESHOLD, DEFAULT_TOKEN_LENGTH, compare, print } ================================================ FILE: test/fixtures/cli_output/test-es-fail.js.txt ================================================ ================================================ FILE: test/fixtures/cli_output/test-es.js.txt ================================================ 76% similar in: test/fixtures/system/test-es.js 10: function diag(x, y) { 11: return sqrt(square(x) + square(y)); 12: } 14: function dude(x, y) { 15: return sqrt(square(x ^ 2) + square(y)); 16: } ================================================ FILE: test/fixtures/cli_output/test.dir.color.txt ================================================ 76% similar in: test/fixtures/system/test-es.js  9: function diag(x, y) { 10: return sqrt(square(x) + square(y)); 11: } 13: function dude(x, y) { 14: return sqrt(square(x ^ 2) + square(y)); 15: } 83% similar in: test/fixtures/system/test.js 63: function (a, b) { 64: const dx = a.x - b.x; 65: const dy = a.y - b.y; 66: return Math.sqrt(dx * dx + dy * dy); 67: } 70: function (an, ab) { 71: const dx = an.x - ab.x; 72: const dy = an.y - ab.y; 73: return Math.sqrt(dx * dx + dy * dy); 74: } 89% similar in: test/fixtures/system/test.js 27: class FooBazzzz { 28: constructor() { 29: } 30: static get area() { 31: console.log('a'); 32: console.log('b'); 33: console.log('c'); 34: console.log('c'); 35: console.log('c'); 36: } 37: } 40: class FooBarrr { 41: constructor() { 42: } 43: static get area() { 44: console.log('a'); 45: console.log('b'); 46: console.log('c'); 47: console.log('c'); 48: console.log('c'); 49: } 50: } 93% similar in: test/fixtures/system/test.copy.js 1: (a, b) => { 2: console.log(); 3: if (!a) { 4: console.log(a); 5: } 6: } in: test/fixtures/system/test.js 1: (a, b) => { 2: console.log(b); 3: if (a) { 4: console.log(a); 5: } 6: } 93% similar in: test/fixtures/system/test.js 1: (a, b) => { 2: console.log(b); 3: if (a) { 4: console.log(a); 5: } 6: }  9: (a, b) => { 10: console.log(); 11: if (!a) { 12: console.log(a); 13: } 14: } 100% similar in: test/fixtures/system/test.copy.js 1: (a, b) => { 2: console.log(); 3: if (!a) { 4: console.log(a); 5: } 6: } in: test/fixtures/system/test.js  9: (a, b) => { 10: console.log(); 11: if (!a) { 12: console.log(a); 13: } 14: } 100% similar in: test/fixtures/system/test.js 31: function () { 32: console.log('a'); 33: console.log('b'); 34: console.log('c'); 35: console.log('c'); 36: console.log('c'); 37: } 44: function () { 45: console.log('a'); 46: console.log('b'); 47: console.log('c'); 48: console.log('c'); 49: console.log('c'); 50: } 87% similar in: test/fixtures/system/test.ts 20: class Greeter { 21: greeting: string; 22: constructor(message: string) { 23: this.greeting = message; 24: } 25: static greet(a, y, x) { 26: let b = y + x 27: return a + "Hello, " + this.greeting; 28: } 29: } 31: class FooGreeter { 32: constructor(message: string) { 33: this.greeting = message; 34: } 35: static greet(a, y, x) { 36: let b = x + y 37: return a + "Hello, " + this.greeting; 38: } 39: } 100% similar in: test/fixtures/system/test.ts 25: static greet(a, y, x) { 26: let b = y + x 27: return a + "Hello, " + this.greeting; 28: } 35: static greet(a, y, x) { 36: let b = x + y 37: return a + "Hello, " + this.greeting; 38: } ================================================ FILE: test/fixtures/cli_output/test.dir.txt ================================================ 76% similar in: test/fixtures/system/test-es.js 10: function diag(x, y) { 11: return sqrt(square(x) + square(y)); 12: } 14: function dude(x, y) { 15: return sqrt(square(x ^ 2) + square(y)); 16: } 83% similar in: test/fixtures/system/test.js 64: function (a, b) { 65: const dx = a.x - b.x; 66: const dy = a.y - b.y; 67: return Math.sqrt(dx * dx + dy * dy); 68: } 71: function (an, ab) { 72: const dx = an.x - ab.x; 73: const dy = an.y - ab.y; 74: return Math.sqrt(dx * dx + dy * dy); 75: } 89% similar in: test/fixtures/system/test.js 28: class FooBazzzz { 29: constructor() { 30: } 31: static get area() { 32: console.log('a'); 33: console.log('b'); 34: console.log('c'); 35: console.log('c'); 36: console.log('c'); 37: } 38: } 41: class FooBarrr { 42: constructor() { 43: } 44: static get area() { 45: console.log('a'); 46: console.log('b'); 47: console.log('c'); 48: console.log('c'); 49: console.log('c'); 50: } 51: } 93% similar in: test/fixtures/system/test.copy.js 2: (a, b) => { 3: console.log(); 4: if (!a) { 5: console.log(a); 6: } 7: } in: test/fixtures/system/test.js 2: (a, b) => { 3: console.log(b); 4: if (a) { 5: console.log(a); 6: } 7: } 93% similar in: test/fixtures/system/test.js 2: (a, b) => { 3: console.log(b); 4: if (a) { 5: console.log(a); 6: } 7: } 10: (a, b) => { 11: console.log(); 12: if (!a) { 13: console.log(a); 14: } 15: } 100% similar in: test/fixtures/system/test.copy.js 2: (a, b) => { 3: console.log(); 4: if (!a) { 5: console.log(a); 6: } 7: } in: test/fixtures/system/test.js 10: (a, b) => { 11: console.log(); 12: if (!a) { 13: console.log(a); 14: } 15: } 100% similar in: test/fixtures/system/test.js 32: function () { 33: console.log('a'); 34: console.log('b'); 35: console.log('c'); 36: console.log('c'); 37: console.log('c'); 38: } 45: function () { 46: console.log('a'); 47: console.log('b'); 48: console.log('c'); 49: console.log('c'); 50: console.log('c'); 51: } 87% similar in: test/fixtures/system/test.ts 21: class Greeter { 22: greeting: string; 23: constructor(message: string) { 24: this.greeting = message; 25: } 26: static greet(a, y, x) { 27: let b = y + x 28: return a + "Hello, " + this.greeting; 29: } 30: } 32: class FooGreeter { 33: constructor(message: string) { 34: this.greeting = message; 35: } 36: static greet(a, y, x) { 37: let b = x + y 38: return a + "Hello, " + this.greeting; 39: } 40: } 100% similar in: test/fixtures/system/test.ts 26: static greet(a, y, x) { 27: let b = y + x 28: return a + "Hello, " + this.greeting; 29: } 36: static greet(a, y, x) { 37: let b = x + y 38: return a + "Hello, " + this.greeting; 39: } ================================================ FILE: test/fixtures/cli_output/test.js.txt ================================================ 83% similar in: test/fixtures/system/test.js 64: function (a, b) { 65: const dx = a.x - b.x; 66: const dy = a.y - b.y; 67: return Math.sqrt(dx * dx + dy * dy); 68: } 71: function (an, ab) { 72: const dx = an.x - ab.x; 73: const dy = an.y - ab.y; 74: return Math.sqrt(dx * dx + dy * dy); 75: } 89% similar in: test/fixtures/system/test.js 28: class FooBazzzz { 29: constructor() { 30: } 31: static get area() { 32: console.log('a'); 33: console.log('b'); 34: console.log('c'); 35: console.log('c'); 36: console.log('c'); 37: } 38: } 41: class FooBarrr { 42: constructor() { 43: } 44: static get area() { 45: console.log('a'); 46: console.log('b'); 47: console.log('c'); 48: console.log('c'); 49: console.log('c'); 50: } 51: } 93% similar in: test/fixtures/system/test.js 2: (a, b) => { 3: console.log(b); 4: if (a) { 5: console.log(a); 6: } 7: } 10: (a, b) => { 11: console.log(); 12: if (!a) { 13: console.log(a); 14: } 15: } 100% similar in: test/fixtures/system/test.js 32: function () { 33: console.log('a'); 34: console.log('b'); 35: console.log('c'); 36: console.log('c'); 37: console.log('c'); 38: } 45: function () { 46: console.log('a'); 47: console.log('b'); 48: console.log('c'); 49: console.log('c'); 50: console.log('c'); 51: } ================================================ FILE: test/fixtures/cli_output/test.ngram.js.txt ================================================ 89% similar in: test/fixtures/system/test.js 28: class FooBazzzz { 29: constructor() { 30: } 31: static get area() { 32: console.log('a'); 33: console.log('b'); 34: console.log('c'); 35: console.log('c'); 36: console.log('c'); 37: } 38: } 41: class FooBarrr { 42: constructor() { 43: } 44: static get area() { 45: console.log('a'); 46: console.log('b'); 47: console.log('c'); 48: console.log('c'); 49: console.log('c'); 50: } 51: } 100% similar in: test/fixtures/system/test.js 32: function () { 33: console.log('a'); 34: console.log('b'); 35: console.log('c'); 36: console.log('c'); 37: console.log('c'); 38: } 45: function () { 46: console.log('a'); 47: console.log('b'); 48: console.log('c'); 49: console.log('c'); 50: console.log('c'); 51: } ================================================ FILE: test/fixtures/cli_output/test.similarity.js.txt ================================================ 100% similar in: test/fixtures/system/test.js 32: function () { 33: console.log('a'); 34: console.log('b'); 35: console.log('c'); 36: console.log('c'); 37: console.log('c'); 38: } 45: function () { 46: console.log('a'); 47: console.log('b'); 48: console.log('c'); 49: console.log('c'); 50: console.log('c'); 51: } ================================================ FILE: test/fixtures/cli_output/test.token.js.txt ================================================ 89% similar in: test/fixtures/system/test.js 28: class FooBazzzz { 29: constructor() { 30: } 31: static get area() { 32: console.log('a'); 33: console.log('b'); 34: console.log('c'); 35: console.log('c'); 36: console.log('c'); 37: } 38: } 41: class FooBarrr { 42: constructor() { 43: } 44: static get area() { 45: console.log('a'); 46: console.log('b'); 47: console.log('c'); 48: console.log('c'); 49: console.log('c'); 50: } 51: } ================================================ FILE: test/fixtures/cli_output/test.ts.txt ================================================ 87% similar in: test/fixtures/system/test.ts 21: class Greeter { 22: greeting: string; 23: constructor(message: string) { 24: this.greeting = message; 25: } 26: static greet(a, y, x) { 27: let b = y + x 28: return a + "Hello, " + this.greeting; 29: } 30: } 32: class FooGreeter { 33: constructor(message: string) { 34: this.greeting = message; 35: } 36: static greet(a, y, x) { 37: let b = x + y 38: return a + "Hello, " + this.greeting; 39: } 40: } 100% similar in: test/fixtures/system/test.ts 26: static greet(a, y, x) { 27: let b = y + x 28: return a + "Hello, " + this.greeting; 29: } 36: static greet(a, y, x) { 37: let b = x + y 38: return a + "Hello, " + this.greeting; 39: } ================================================ FILE: test/fixtures/system/test-es.js ================================================ import { readFile } from "fs" import * as path from "path"; export const sqrt = Math.sqrt; export function square(x) { return x * x; } export function diag(x, y) { return sqrt(square(x) + square(y)); } export function dude(x, y) { return sqrt(square(x^2) + square(y)); } ================================================ FILE: test/fixtures/system/test.copy.js ================================================ const foo_two = (a, b) => { console.log() if (!a) { console.log(a) } } ================================================ FILE: test/fixtures/system/test.js ================================================ const foo = (a, b) => { console.log(b) if (a) { console.log(a) } } const foo_two = (a, b) => { console.log() if (!a) { console.log(a) } } const bar = () => true && "val" function baz() {} function dude() { return function haha() { console.log("hi!") } } class FooBazzzz { constructor() { } static get area() { console.log('a') console.log('b') console.log('c') console.log('c') console.log('c') } } class FooBarrr { constructor() { } static get area() { console.log('a') console.log('b') console.log('c') console.log('c') console.log('c') } } class Rectangle { constructor(height, width) { this.height = height; this.width = width; } get area() { return this.calcArea(); } static distance(a, b) { const dx = a.x - b.x; const dy = a.y - b.y; return Math.sqrt(dx*dx + dy*dy); } static foobar(an, ab) { const dx = an.x - ab.x; const dy = an.y - ab.y; return Math.sqrt(dx*dx + dy*dy); } } ================================================ FILE: test/fixtures/system/test.ts ================================================ const foo = (a : string, b : number) : number => { console.log(b) if (!a && !b) { console.log(a) } else { return 1 } } var f = function () { } function some_method = (x) => (a) => console.log("DS") module haha { function Huz () {} } class Greeter { greeting: string; constructor(message: string) { this.greeting = message; } static greet(a, y, x) { let b = y + x return a + "Hello, " + this.greeting; } } class FooGreeter { constructor(message: string) { this.greeting = message; } static greet(a, y, x) { let b = x + y return a + "Hello, " + this.greeting; } } let greeter = new Greeter("world"); let foobar = function (x : number, s : any) : number[] { let a = [1, 2] let s = new Huz(function (t, r) {}); a.forEach((i) => { console.log(i) }) return a } ================================================ FILE: test/helpers/sinon_chai.coffee ================================================ sinon = require "sinon" chai = require "chai" sinonChai = require "sinon-chai" chai.use sinonChai .use chai.should .should() ================================================ FILE: test/helpers/system.coffee ================================================ os = require "os" path = require "path" child_process = require "child_process" chai = require "chai" on_win = os.platform() == "win32" expect = chai.expect SYNT_BIN = path.join(__dirname, "..", "..", "bin", "synt") exec = (args, cb, stdio) -> cmd = undefined cmd = "#{SYNT_BIN} #{args}" cli_args = cmd.split(" ") proc = child_process.spawn( "node", cli_args, stdio: stdio, env: process.env) out = "" err = "" error = undefined cb_called = undefined proc.stdout.on "data", (d) -> out += d proc.stderr.on "data", (d) -> err += d proc.on "error", (e) -> unless cb_called cb_called = true if on_win out = new Buffer.from(out).toString("utf-8").replace(/\r\n/g, "\n") else out = new Buffer.from(out).toString("utf-8") cb( e, out, new Buffer.from(err).toString("utf-8")) proc.on "close", (code) -> unless cb_called cb_called = true if on_win out = new Buffer.from(out).toString("utf-8").replace(/\r\n/g, "\n") else out = new Buffer.from(out).toString("utf-8") cb( { code: code }, out, new Buffer.from(err).toString("utf-8")) proc module.exports = exec: exec ================================================ FILE: test/mocha.opts ================================================ --compilers coffee:coffee-script/register -R dot -t 5000 -s 3000 ================================================ FILE: test/spec/index.coffee ================================================ chai = require "chai" library = require "./../../lib" expect = chai.expect describe "unit :: library", -> describe "when required", -> it "exports the library", -> expect(library).to.have.property("compare") expect(library).to.have.property("print") expect(library).to.have.property("DEFAULT_NGRAM_LENGTH") expect(library).to.have.property("DEFAULT_THRESHOLD") expect(library).to.have.property("DEFAULT_TOKEN_LENGTH") ================================================ FILE: test/spec/similar/ngram.coffee ================================================ sinon_chai = require "./../../helpers/sinon_chai" chai = require "chai" ngram = require "./../../../lib/similar/ngram" expect = chai.expect describe "unit :: ngram", -> describe "generating ngrams", -> list = ["1", "2", "3", "4"] it "defaults to 1 if larger than array length", -> expect(ngram.generate list, 200) .to.eql ["1", "2", "3", "4"] it "can generate a unigram by default", -> expect(ngram.generate list) .to.eql ["1", "2", "3", "4"] it "can generate a unigram", -> expect(ngram.generate list, 1) .to.eql ["1", "2", "3", "4"] it "can generate a bigram", -> expect(ngram.generate list, 2) .to.eql ["12", "23", "34"] it "can generate a trigram", -> expect(ngram.generate list, 3) .to.eql ["123", "234"] ================================================ FILE: test/spec/similar.coffee ================================================ sinon_chai = require "./../helpers/sinon_chai" chai = require "chai" similar = require "./../../lib/similar" expect = chai.expect describe "unit :: similar", -> describe "when no args/opts are provided", -> it "does not die", -> similar.compare() ================================================ FILE: test/spec/system.coffee ================================================ fs = require "fs" os = require "os" path = require "path" chai = require "chai" pkg = require "./../../package" system = require "./../helpers/system" on_win = os.platform() == "win32" expect = chai.expect FIXTURES = path.join __dirname, "..", "fixtures" SYSTEM = path.join FIXTURES, "system" FILE_JS = path.join SYSTEM, "test.js" FILE_JS_ES = path.join SYSTEM, "test-es.js" FILE_TS = path.join SYSTEM, "test.ts" CLI_OUTPUT_TEST_JS = path .join FIXTURES, "cli_output", "test.js.txt" CLI_OUTPUT_TEST_ES_MODULES = path .join FIXTURES, "cli_output", "test-es.js.txt" CLI_OUTPUT_TEST_ES_MODULES_FAIL = path .join FIXTURES, "cli_output", "test-es-fail.js.txt" CLI_OUTPUT_TEST_JS_COLOR = path .join FIXTURES, "cli_output", "test.js.color.txt" CLI_OUTPUT_TEST_JS_SIM = path .join FIXTURES, "cli_output", "test.similarity.js.txt" CLI_OUTPUT_TEST_JS_NGRAM = path .join FIXTURES, "cli_output", "test.ngram.js.txt" CLI_OUTPUT_TEST_JS_TOKEN = path .join FIXTURES, "cli_output", "test.token.js.txt" CLI_OUTPUT_TEST_TS = path .join FIXTURES, "cli_output", "test.ts.txt" CLI_OUTPUT_TEST_JS_TS_DIR = path .join FIXTURES, "cli_output", "test.dir.txt" CLI_OUTPUT_TEST_JS_TS_DIR_COLOR = path .join FIXTURES, "cli_output", "test.dir.color.txt" cli_output = (path) -> if on_win fs.readFileSync(path) .toString() .replace(/\r\n/g, "\n") .replace(/\//g, "\\") else fs.readFileSync(path) .toString() # TODO: consider testing more in depth for false positives # -> currently these tests cover a lot of it indirectly describe "system :: cli", -> describe "javascript", -> it "can compare similar functions and classes", (done) -> cmd = "analyze -n #{FILE_JS}" system.exec cmd, (error, stdout, stderr) -> expect(stdout).to.eql(cli_output CLI_OUTPUT_TEST_JS) expect(error.code).to.eql 0 done() describe "es modules", -> describe "when estype is not set (module by default)", -> it "parses the code as expected", (done) -> cmd = "analyze -n #{FILE_JS_ES}" system.exec cmd, (error, stdout, stderr) -> expect(stdout).to.eql(cli_output CLI_OUTPUT_TEST_ES_MODULES) expect(error.code).to.eql 0 done() describe "when estype is set as script", -> it "fails to parse the code", (done) -> cmd = "analyze -n -a 6 -t script #{FILE_JS_ES}" system.exec cmd, (error, stdout, stderr) -> expect(stderr).to.match /espree/i expect(stderr).to.match /'import' and 'export' may appear only with/i expect(stdout).to.eql(cli_output CLI_OUTPUT_TEST_ES_MODULES_FAIL) expect(error.code).to.eql 1 done() describe "when a file fails to parse", -> it "also shows the filename", (done) -> cmd = "analyze -n -t script #{FILE_JS_ES}" system.exec cmd, (error, stdout, stderr) -> if on_win expect(stderr) .to.match /Error: in test\\fixtures\\system\\test\-es\.js/gi else expect(stderr) .to.match new RegExp("Error: in test/fixtures/system/test-es.js") expect(stderr).to.match /'import' and 'export' may appear only with/i expect(stderr).match /espree/i expect(stdout).to.eql(cli_output CLI_OUTPUT_TEST_ES_MODULES_FAIL) expect(error.code).to.eql 1 done() describe "typescript", -> it "can compare similar functions and classes", (done) -> cmd = "analyze -n #{FILE_TS}" system.exec cmd, (error, stdout, stderr) -> expect(stdout).to.eql(cli_output CLI_OUTPUT_TEST_TS) expect(error.code).to.eql 0 done() it "can exit with a non zero code", (done) -> cmd = "analyze -e -n #{FILE_TS}" system.exec cmd, (error, stdout, stderr) -> expect(stdout).to.eql(cli_output CLI_OUTPUT_TEST_TS) expect(error.code).to.eql 1 done() describe "in general", -> it "can print help", (done) -> system.exec "--help", (error, stdout, stderr) -> expect(stdout).to.match /options:/i expect(stdout).to.match /examples:/i expect(stdout).to.match /commands:/i expect(error.code).to.eql 0 done() it "can print command specific help", (done) -> system.exec "analyze -h", (error, stdout, stderr) -> expect(stdout).to.match /analyze\|a/ expect(stdout).to.match /\-s/ expect(stdout).to.match /\-n/ expect(stdout).to.match /\-m/ expect(error.code).to.eql 0 done() it "prints the version", (done) -> cmd = "-V" system.exec cmd, (error, stdout, stderr) -> expect(stdout).to.match new RegExp pkg.version expect(error.code).to.eql 0 done() it "can set analyze sim threshold", (done) -> cmd = "analyze -s 95 -n #{FILE_JS}" system.exec cmd, (error, stdout, stderr) -> expect(stdout).to.eql(cli_output CLI_OUTPUT_TEST_JS_SIM) expect(error.code).to.eql 0 done() it "can set ngram level", (done) -> cmd = "analyze -g 10 -n #{FILE_JS}" system.exec cmd, (error, stdout, stderr) -> expect(stdout).to.eql(cli_output CLI_OUTPUT_TEST_JS_NGRAM) expect(error.code).to.eql 0 done() it "can set token level", (done) -> cmd = "analyze -m 50 -n #{FILE_JS}" system.exec cmd, (error, stdout, stderr) -> expect(stdout).to.eql(cli_output CLI_OUTPUT_TEST_JS_TOKEN) expect(error.code).to.eql 0 done() it "can set non zero exit status", (done) -> cmd = "analyze -e -m 50 -n #{FILE_JS}" system.exec cmd, (error, stdout, stderr) -> expect(stdout).to.eql(cli_output CLI_OUTPUT_TEST_JS_TOKEN) expect(error.code).to.eql 1 done() it "can output in colors", (done) -> cmd = "analyze #{SYSTEM}" system.exec cmd, (error, stdout, stderr) -> expect(stdout).to.eql(cli_output CLI_OUTPUT_TEST_JS_TS_DIR_COLOR) expect(error.code).to.eql 0 done() it "can compare via a dir", (done) -> cmd = "analyze -n #{SYSTEM}" system.exec cmd, (error, stdout, stderr) -> expect(stdout).to.eql(cli_output CLI_OUTPUT_TEST_JS_TS_DIR) expect(error.code).to.eql 0 done() ================================================ FILE: tsconfig.json ================================================ { "compilerOptions": { "module": "commonjs", "target": "es6", "moduleResolution": "node", "forceConsistentCasingInFileNames": true, "noEmitOnError": true, "noFallthroughCasesInSwitch": true, "noImplicitAny": true, "noImplicitReturns": true, "noImplicitThis": true, "noUnusedLocals": true, "removeComments": true, "outDir": "lib", "rootDir": "src", "typeRoots": [ "src/@types" ] }, "exclude": [ "node_modules", "test" ] }