diff --git a/.github/main.workflow b/.github/main.workflow deleted file mode 100644 index 49cb97b..0000000 --- a/.github/main.workflow +++ /dev/null @@ -1,46 +0,0 @@ -workflow "Main" { - on = "push" - resolves = ["Publish"] -} - -action "Install" { - uses = "docker://node" - runs = "npm" - args = "install" -} - -action "Test" { - needs = "Install" - uses = "docker://node" - runs = "npm" - args = "test" -} - -action "Pre Build" { - needs = "Install" - uses = "docker://stedolan/jq" - runs = "sh" - args = "Taskfile prebuild" -} - -action "Build" { - needs = "Pre Build" - uses = "docker://node" - runs = "npm" - args = "run build" -} - -# Filter for master branch -action "Master" { - needs = ["Test", "Build"] - uses = "actions/bin/filter@master" - args = "branch master" -} - -action "Publish" { - needs = "Master" - uses = "docker://node" - runs = "npx" - args = "semantic-release" - secrets = ["NPM_TOKEN", "GITHUB_TOKEN"] -} diff --git a/CHANGELOG.md b/CHANGELOG.md index eeef4a8..e69b71e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,14 @@ +## [1.0.1](https://github.com/unlight/typescript-transform-unspec/compare/v1.0.0...v1.0.1) (2019-06-14) + + +### Bug Fixes + +* Fixed build ([bc09372](https://github.com/unlight/typescript-transform-unspec/commit/bc09372)) +* Removed debug code ([e7afd37](https://github.com/unlight/typescript-transform-unspec/commit/e7afd37)) + # 1.0.0 (2019-06-14) ### Features -* First release ([1fc88b8](https://github.com/unlight/typescript-transform-unspec/commit/1fc88b8)) -* First release ([352d144](https://github.com/unlight/typescript-transform-unspec/commit/352d144)) +* First release diff --git a/Taskfile b/Taskfile index 2cd43d8..51ae57f 100644 --- a/Taskfile +++ b/Taskfile @@ -1,19 +1,18 @@ #!/bin/bash PATH="$PWD/node_modules/.bin":$PATH -prebuild() { - rm -rf dist - cp -rf src dist && /usr/bin/find dist -name '*.spec.ts' | xargs rm -f - cat tsconfig.json | jq 'del(.include, .compilerOptions.outDir)' > dist/tsconfig.json - cp README.md LICENSE package.json dist -} - build() { + set -x + rm -rfv dist + cp -rfv src dist && /usr/bin/find dist -name '*.spec.ts' | xargs rm -rvf + cat tsconfig.json | jq 'del(.include, .compilerOptions.outDir)' | jq -r '.compilerOptions.plugins[0].transform = "./index.ts"' > dist/tsconfig.json + cp -v README.md LICENSE package.json dist cd dist - rm example.ts + rm -rvf example.ts ttsc -p . - rm tsconfig.json + rm -rvf tsconfig.json cd .. + set +x } "$@" diff --git a/package.json b/package.json index 9a40706..9e3e88b 100644 --- a/package.json +++ b/package.json @@ -2,16 +2,22 @@ "name": "typescript-transform-unspec", "version": "0.0.0-dev", "license": "MIT", - "description": "Typescript transform plugin", + "description": "Typescript transform plugin removes spec definition from source file", "main": "index.js", "typings": "index.d.ts", "author": "2019", - "keywords": [], + "keywords": [ + "typescript-transformer", + "typescript-plugin", + "typescript-transform-plugin", + "typescript-compiler" + ], "engines": { "node": ">=8" }, "repository": { - "type": "git" + "type": "git", + "url": "https://github.com/unlight/typescript-transform-unspec.git" }, "scripts": { "test": "npm run eslint && npm run tscheck && npm run test:cov && npm run typecov", @@ -39,26 +45,27 @@ "setupwebpack": "npm i -D webpack webpack-cli webpack-dev-server html-webpack-plugin source-map-loader ts-loader html-loader swc-loader @swc/core", "commit": "git-cz" }, + "dependencies": {}, "peerDependencies": { "typescript": ">=2.9" }, "devDependencies": { - "@semantic-release/changelog": "^3.0.2", + "@semantic-release/changelog": "^3.0.4", "@semantic-release/commit-analyzer": "^7.0.0-beta.2", "@semantic-release/git": "^7.1.0-beta.3", - "@semantic-release/github": "^5.4.0-beta.1", + "@semantic-release/github": "^5.4.0", "@semantic-release/npm": "^5.2.0-beta.6", - "@semantic-release/release-notes-generator": "^7.1.7", - "@types/jest": "^24.0.13", - "@types/node": "^12.0.2", - "@typescript-eslint/eslint-plugin-tslint": "^1.9.0", - "@typescript-eslint/parser": "^1.9.0", + "@semantic-release/release-notes-generator": "^7.2.0", + "@types/jest": "^24.0.14", + "@types/node": "^12.0.8", + "@typescript-eslint/eslint-plugin-tslint": "^1.10.2", + "@typescript-eslint/parser": "^1.10.2", "eslint": "^5.16.0", "eslint-import-resolver-node": "^0.3.2", "eslint-plugin-import": "^2.17.3", "eslint-plugin-jest": "^22.6.4", "eslint-plugin-only-warn": "^1.0.1", - "eslint-plugin-unicorn": "^8.0.2", + "eslint-plugin-unicorn": "^9.1.0", "eslint-plugin-wix-editor": "^2.0.0", "jest": "^24.8.0", "npm-run-all": "^4.1.5", @@ -67,20 +74,20 @@ "remark-license": "^5.0.1", "remark-toc": "^5.1.1", "semantic-release": "^16.0.0-beta.19", - "simplytyped": "^3.1.0", + "simplytyped": "^3.2.0", "source-map-loader": "^0.2.4", "ts-jest": "^24.0.2", "ts-loader": "^6.0.2", "ts-node": "^8.2.0", - "tslint": "^5.16.0", + "tslint": "^5.17.0", "tslint-clean-code": "^0.2.9", "tslint-microsoft-contrib": "^6.2.0", "tslint-sonarts": "^1.9.0", "ttypescript": "^1.5.7", "type-coverage": "^2.0.2", - "typescript": "~3.4.5", + "typescript": "~3.5.2", "watchexec-bin": "^1.0.0", - "webpack": "^4.33.0", + "webpack": "^4.34.0", "webpack-cli": "^3.3.4" }, "config": { diff --git a/src/example.ts b/src/example.ts index 17dc6d9..ed72b81 100644 --- a/src/example.ts +++ b/src/example.ts @@ -7,5 +7,7 @@ it('hello world test', () => { }); describe('hello world test', () => { - jest.mock('fs'); + beforeAll(() => { + jest.mock('inspector'); + }); }); diff --git a/src/index.spec.ts b/src/index.spec.ts index 2bc775e..9d58bc4 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -1,15 +1,13 @@ /* eslint-disable @typescript-eslint/tslint/config */ -import * as lib from './index'; +import transformPlugin from './index'; import * as ts from 'typescript'; -let transformPlugin: typeof lib.typescriptTransformUnspec = lib as any; - function transform(sourceText: string) { const program = ts.createProgram({ rootNames: [], options: {}, }); - const transformer = transformPlugin(program, {}); + const transformer = transformPlugin(program); const sourceFile = ts.createSourceFile('filename.tsx', sourceText, ts.ScriptTarget.ES5, true, ts.ScriptKind.TSX); let result: string | undefined; const writeCallback = (fileName: string, data: string) => { @@ -20,11 +18,30 @@ function transform(sourceText: string) { } it('smoke test', () => { - expect(lib).toBeTruthy(); - expect(transform(';')).toEqual(';'); + expect(transformPlugin).toBeTruthy(); + expect(transform(';')).toBe(';'); }); it('should be removed it call expression', () => { - debugger; - expect(transform('it()')).toEqual(''); + expect(transform('it()')).toBe(''); + expect(transform('xit()')).toBe(''); + expect(transform('fit()')).toBe(''); +}); + +it('should be removed describe call expression', () => { + expect(transform('describe()')).toBe(''); + expect(transform('xdescribe()')).toBe(''); + expect(transform('fdescribe()')).toBe(''); +}); + +it('functions with modifiers', () => { + expect(transform('describe.only()')).toBe(''); + expect(transform('describe.skip()')).toBe(''); + expect(transform('describe.any()')).toBe(''); + expect(transform('test.only()')).toBe(''); + expect(transform('test.skip()')).toBe(''); + expect(transform('test.any()')).toBe(''); + expect(transform('it.only()')).toBe(''); + expect(transform('it.skip()')).toBe(''); + expect(transform('it.any()')).toBe(''); }); diff --git a/src/index.ts b/src/index.ts index 11e6c99..c781c41 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,24 +1,60 @@ import * as ts from 'typescript'; -export function typescriptTransformUnspec(program: ts.Program, options?): ts.TransformerFactory { +export default function typescriptTransformUnspec(program: ts.Program): ts.TransformerFactory { // tslint:disable-line:no-default-export return (context) => (file) => visitNodeAndChildren(file, program, context); } function visitNodeAndChildren(node: ts.Node, program: ts.Program, context: ts.TransformationContext): ts.SourceFile { - return ts.visitEachChild(visitor(node), (childNode) => visitNodeAndChildren(childNode, program, context), context) as ts.SourceFile; + const visitor = (childNode) => visitNodeAndChildren(childNode, program, context); + return ts.visitEachChild(filterNode(node), visitor, context); } -function visitor(node: ts.Node): ts.Node | undefined { +function filterNode(node: T): T | undefined { if (isSpecExpressionStatement(node)) { return undefined; } return node; } +// todo: delete only global +const testMethodList = [ + 'jasmine', + 'jest', + 'before', + 'after', + 'beforeAll', + 'beforeEach', + 'afterAll', + 'afterEach', + 'describe', + 'fdescribe', + 'xdescribe', + 'it', + 'fit', + 'xit', + 'test', + 'xtest', + 'spyOn', +]; + +function isTestExpressionText(text: string | ts.__String) { + return testMethodList.includes(String(text)); +} + function isSpecExpressionStatement(node: ts.Node) { - return (ts.isExpressionStatement(node) && ts.isCallExpression(node.expression) - && ts.isIdentifier(node.expression.expression) - && node.expression.expression.escapedText === 'it'); + if (ts.isExpressionStatement(node) && ts.isCallExpression(node.expression)) { + const callExpr = node.expression.expression; + if (ts.isIdentifier(callExpr) && isTestExpressionText(callExpr.escapedText)) { + return true; + } + if (ts.isPropertyAccessExpression(callExpr)) { + const propertyAccessExpr = callExpr.expression; + if (ts.isIdentifier(propertyAccessExpr) && isTestExpressionText(propertyAccessExpr.escapedText)) { + return true; + } + } + } + return false; } module.exports = typescriptTransformUnspec; diff --git a/tslint.json b/tslint.json index 360ae7f..2680ac4 100644 --- a/tslint.json +++ b/tslint.json @@ -47,6 +47,7 @@ "no-reserved-keywords": false, "no-increment-decrement": false, "no-unnecessary-bind": false, + "newline-per-chained-call": false, ".": false } }