diff --git a/.gitignore b/.gitignore index c44e8809..07802af0 100644 --- a/.gitignore +++ b/.gitignore @@ -5,8 +5,10 @@ # Ignore TypeScript-emitted files *.js +*.js.map *.swp !.*rc.js *.d.ts +dist/ # Except... !/types/**/*.d.ts diff --git a/package.json b/package.json index 055d6420..c17e6b8a 100644 --- a/package.json +++ b/package.json @@ -19,18 +19,38 @@ "url": "git://github.com/coderaiser/recast.git" }, "license": "MIT", - "main": "main.js", + "main": "./dist/src/main.js", "types": "main.d.ts", "scripts": { - "mocha": "test/run.sh", - "debug": "test/run.sh --inspect-brk", - "test": "npm run lint && npm run build && npm run mocha", - "build": "npm run clean && tsc", + "test": "jest", + "watch": "jest --watch", + "snapupdate": "jest --updateSnapshot", + "build": "npm run clean && npm run compile", "lint": "eslint --ext .ts .", "format": "prettier --write '{lib,test}/**.ts' '*rc.js'", - "clean": "ts-emit-clean", - "prepack": "npm run build", - "postpack": "npm run clean" + "clean": "rimraf ./dist", + "compile": "tsc -p .", + "prepack": "npm run build" + }, + "jest": { + "testEnvironment": "node", + "roots": [ + "./test" + ], + "testMatch": [ + "**/test/**/*\\.[jt]s?(x)", + "**/run\\.[jt]s" + ], + "transform": { + "^.+\\.ts$": "ts-jest" + }, + "testPathIgnorePatterns": ["/test/data/"], + "moduleFileExtensions": [ + "ts", + "js", + "json", + "node" + ] }, "lint-staged": { "*.ts": [ @@ -53,18 +73,22 @@ "@babel/preset-env": "7.11.5", "@types/esprima": "4.0.2", "@types/glob": "7.1.3", - "@types/mocha": "8.0.3", + "@types/jest": "^27.4.1", "@types/node": "^16.0.0", "@typescript-eslint/parser": "^5.8.0", "eslint": "^8.5.0", "esprima-fb": "15001.1001.0-dev-harmony-fb", + "expect": "^27.5.1", + "jest": "^27.5.1", + "jest-mock": "^27.5.1", "flow-parser": "^0.159.0", "glob": "^7.1.7", "lint-staged": "^10.2.6", - "mocha": "^9.0.2", "prettier": "^2.0.5", + "rimraf": "^3.0.2", "reify": "0.20.12", - "ts-emit-clean": "1.0.0", + "ts-jest": "^27.1.4", + "ts-node": "^10.7.0", "typescript": "^4.3.5" }, "engines": { diff --git a/test/lines.ts b/test/lines.ts index 61446a04..95c6a5e1 100644 --- a/test/lines.ts +++ b/test/lines.ts @@ -491,7 +491,7 @@ describe("lines", function () { fs.readFile(__filename, "utf-8", function (err, source) { assert.equal(err, null); - assert.strictEqual(fromString(source).guessTabWidth(), 4); + assert.strictEqual(fromString(source).guessTabWidth(), 2); fs.readFile( path.join(__dirname, "..", "package.json"), diff --git a/test/perf.ts b/test/perf.ts index 566c26cb..49660ccc 100644 --- a/test/perf.ts +++ b/test/perf.ts @@ -2,31 +2,35 @@ import path from "path"; import fs from "fs"; import * as recast from "../main"; -const source = fs.readFileSync( - path.join(__dirname, "data", "backbone.js"), - "utf8", -); - -const start = +new Date(); -const ast = recast.parse(source); -const types = Object.create(null); - -const parseTime = +new Date() - start; -console.log("parse", parseTime, "ms"); - -recast.visit(ast, { - visitNode: function (path) { - types[path.value.type] = true; - this.traverse(path); - }, -}); - -const visitTime = +new Date() - start - parseTime; -console.log("visit", visitTime, "ms"); - -recast.prettyPrint(ast).code; - -const printTime = +new Date() - start - visitTime - parseTime; -console.log("print", printTime, "ms"); - -console.log("total", +new Date() - start, "ms"); +describe('performance metrics', () => { + it('should print out', () => { + const source = fs.readFileSync( + path.join(__dirname, "data", "backbone.js"), + "utf8", + ); + + const start = +new Date(); + const ast = recast.parse(source); + const types = Object.create(null); + + const parseTime = +new Date() - start; + console.log("parse", parseTime, "ms"); + + recast.visit(ast, { + visitNode: function (path) { + types[path.value.type] = true; + this.traverse(path); + }, + }); + + const visitTime = +new Date() - start - parseTime; + console.log("visit", visitTime, "ms"); + + recast.prettyPrint(ast).code; + + const printTime = +new Date() - start - visitTime - parseTime; + console.log("print", printTime, "ms"); + + console.log("total", +new Date() - start, "ms"); + }); +}) diff --git a/test/typescript.ts b/test/typescript.ts index 53514acc..54a5a76b 100644 --- a/test/typescript.ts +++ b/test/typescript.ts @@ -449,7 +449,7 @@ function testReprinting(pattern: any, description: any) { return; } - this.timeout(20000); + jest.setTimeout(20000); assert.strictEqual(recast.print(ast).code, source); const reprintedCode = recast.prettyPrint(ast).code; diff --git a/tsconfig.json b/tsconfig.json index b11175ee..cc2b16dc 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,9 @@ "lib": ["es2015"], "module": "commonjs", "declaration": true, + "declarationMap": true, "rootDir": ".", + "outDir": "dist", "strict": true, "noUnusedLocals": true, "noUnusedParameters": true, @@ -12,6 +14,7 @@ "moduleResolution": "node", "esModuleInterop": true, "importHelpers": true, + "sourceMap": true, "stripInternal": true }, "exclude": [