Skip to content
This repository was archived by the owner on Sep 21, 2019. It is now read-only.

Develop #55

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ dist
.DS_Store
coverage/
*.log

34 changes: 34 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "ts",
"program": "${workspaceFolder}/src/cli.ts",
"args": ["${workspaceFolder}/../ssc-cdt4/**/*.js", "--keep-original-files"],
"preLaunchTask": "build",
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
},
{
"type": "node",
"request": "launch",
"name": "ts-cdt",
"program": "${workspaceFolder}/src/cli.ts",
"args": ["${workspaceFolder}/../euf/ssc-cdt4/src/**/*.js", "--keep-original-files"],
"preLaunchTask": "build",
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
},
{
"type": "node",
"request": "launch",
"name": "ts-one",
"program": "${workspaceFolder}/src/cli.ts",
"args": ["${workspaceFolder}/../p.js", "--keep-original-files", "--print"],
"preLaunchTask": "build",
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
},
{
"type": "node",
"request": "launch",
"name": "node",
"program": "${workspaceFolder}/d/cli",
"args": ["./d/P.js"]
},
{
"args": [],
"cwd": "${workspaceRoot}",
Expand Down
21 changes: 15 additions & 6 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "tsc",
"isShellCommand": true,
"args": ["-p", "."],
"showOutput": "silent",
"problemMatcher": "$tsc"
"version": "2.0.0",
"tasks": [
{
"label": "clean",
"type": "shell",
"command": "rm",
"args": ["-rf", "dist"]
},
{
"label": "build",
"command": "tsc",
"args": ["--pretty"],
"dependsOn": ["clean"]
}
]
}
7,060 changes: 7,060 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"prelint": "npm run clean",
"lint": "tslint --project tsconfig.json --format codeFrame --exclude test/**/*.tsx",
"prepublish": "npm run build",
"clean": "rm -rf dist",
"clean": "rm -rf d",
"prebuild": "npm run clean",
"build": "tsc --pretty",
"precommit": "lint-staged",
Expand All @@ -22,10 +22,16 @@
".ts": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/tests/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": ["ts", "js"]
"moduleFileExtensions": [
"ts",
"js"
]
},
"lint-staged": {
"*.{js,json,css,md,ts,tsx}": ["node_modules/.bin/prettier --write", "git add"]
"*.{js,json,css,md,ts,tsx}": [
"node_modules/.bin/prettier --write",
"git add"
]
},
"bin": "dist/cli.js",
"author": "Mohsen Azimi <me@azimi.me>",
Expand All @@ -35,17 +41,17 @@
"commander": "^2.15.1",
"detect-indent": "^5.0.0",
"glob": "^7.1.2",
"lodash": "^4.17.10",
"lodash": "^4.17.11",
"prettier": "^1.12.1",
"typescript": "2.8.3"
"typescript": "^2.8.3"
},
"devDependencies": {
"@types/chalk": "^2.2.0",
"@types/commander": "^2.9.1",
"@types/detect-indent": "^5.0.0",
"@types/glob": "^5.0.35",
"@types/jest": "^22.2.3",
"@types/lodash": "^4.14.109",
"@types/lodash": "^4.14.135",
"@types/node": "^10.1.2",
"@types/prettier": "^1.12.2",
"@types/react": "^16.3.14",
Expand Down
5 changes: 4 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node

import * as program from 'commander';
import * as glob from 'glob';
import * as fs from 'fs';
Expand Down Expand Up @@ -45,6 +44,7 @@ program
.option('--ignore-prettier-errors', 'Ignore (but warn about) errors in Prettier', false)
.option('--keep-original-files', 'Keep original files', false)
.option('--keep-temporary-files', 'Keep temporary files', false)
.option('--print', 'print output to console', false)
.usage('[options] <filename or glob>')
.command('* [glob/filename...]')
.action((globPatterns: string[]) => {
Expand Down Expand Up @@ -75,6 +75,9 @@ program
try {
fs.copyFileSync(filePath, temporaryPath);
const result = run(temporaryPath, prettierOptions, compilationOptions);
if (program.print) {
console.log('result:\n', result);
}
fs.writeFileSync(newPath, result);
if (!program.keepOriginalFiles) {
fs.unlinkSync(filePath);
Expand Down
1 change: 1 addition & 0 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export function compile(
} catch (prettierError) {
if (compilationOptions.ignorePrettierErrors) {
console.warn(`Prettier failed for ${filePath} (ignorePrettierErrors is on):`);
console.warn(printed);
console.warn(prettierError);
return printed;
}
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/build-prop-type-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function buildInterfaceFromPropTypeObjectLiteral(objectLiteral: ts.Object
*
* @param node React propTypes value
*/
function getTypeFromReactPropTypeExpression(node: ts.Expression): ts.TypeNode {
export function getTypeFromReactPropTypeExpression(node: ts.Expression): ts.TypeNode {
let result = null;
if (ts.isPropertyAccessExpression(node)) {
/**
Expand Down Expand Up @@ -85,7 +85,7 @@ function getTypeFromReactPropTypeExpression(node: ts.Expression): ts.TypeNode {
} else if (/number/.test(text)) {
result = ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword);
} else if (/object/.test(text)) {
result = ts.createKeywordTypeNode(ts.SyntaxKind.ObjectKeyword);
result = ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword);
} else if (/node/.test(text)) {
result = ts.createTypeReferenceNode('React.ReactNode', []);
} else if (/element/.test(text)) {
Expand Down
78 changes: 78 additions & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export function isPropTypesMember(classMember: ts.ClassElement, sourceFile: ts.S
* @param sourceFile
*/
export function getComponentName(propTypeAssignment: ts.Statement, sourceFile: ts.SourceFile) {
if (!sourceFile) {
// debug
console.log(`undefiend`);
}
const text = propTypeAssignment.getText(sourceFile);
return text.substr(0, text.indexOf('.'));
}
Expand Down Expand Up @@ -205,3 +209,77 @@ export function removeItem<T>(collection: ArrayLike<T>, item: T) {
const index = _.indexOf(collection, item);
return _.slice(collection, 0, index).concat(_.slice(collection, index + 1));
}

/**
*
* @param statements todo
* @param callback
*/
export function filter<T>(statements: ts.Node[] | ts.NodeArray<ts.Node> = [], callback: (node: ts.Node) => boolean) {
const result: T[] = [];
const queue: Array<ts.Node | undefined> = _.toArray(statements);
let node: ts.Node | undefined;

while ((node = queue.pop())) {
if (callback(node)) {
result.push(node as any);
}

if (ts.isJsxElement(node)) {
node.openingElement.attributes.forEachChild(child => {
queue.push(child);
});
queue.push(...node.children);
continue;
}

if (ts.isJsxSelfClosingElement(node)) {
node.attributes.forEachChild(child => {
queue.push(child);
});
continue;
}

if (ts.isBlock(node)) {
queue.push(...node.statements);
continue;
}

if ((node as any).thenStatement) {
queue.push((node as any).thenStatement);
}

if ((node as any).elseStatement) {
queue.push((node as any).elseStatement);
}

if ((node as any).initializer) {
queue.push((node as any).initializer);
}

if ((node as any).expression) {
queue.push((node as any).expression);
}

if ((node as any).declarationList) {
queue.push(...(node as any).declarationList.declarations);
}

if (ts.isCallExpression(node)) {
queue.push(...node.arguments);
}

if ((node as any).body) {
queue.push((node as any).body);
}

if ((node as any).whenTrue) {
queue.push((node as any).whenTrue);
}

if ((node as any).whenFlase) {
queue.push((node as any).whenFlase);
}
}
return result;
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { collapseIntersectionInterfacesTransformFactoryFactory } from './transfo
import { reactRemoveStaticPropTypesMemberTransformFactoryFactory } from './transforms/react-remove-static-prop-types-member-transform';
import { reactStatelessFunctionMakePropsTransformFactoryFactory } from './transforms/react-stateless-function-make-props-transform';
import { reactRemovePropTypesImportTransformFactoryFactory } from './transforms/react-remove-prop-types-import';
import { reactRefPropsTransformFactoryFactory } from './transforms/react-ref-props-transform';

export {
reactMovePropTypesToClassTransformFactoryFactory,
Expand All @@ -18,6 +19,7 @@ export {
reactRemovePropTypesAssignmentTransformFactoryFactory,
reactRemoveStaticPropTypesMemberTransformFactoryFactory,
reactRemovePropTypesImportTransformFactoryFactory,
reactRefPropsTransformFactoryFactory,
compile,
};

Expand All @@ -29,6 +31,7 @@ export const allTransforms = [
reactRemovePropTypesAssignmentTransformFactoryFactory,
reactRemoveStaticPropTypesMemberTransformFactoryFactory,
reactRemovePropTypesImportTransformFactoryFactory,
// reactRefPropsTransformFactoryFactory, // to remove
];

export type TransformFactoryFactory = (typeChecker: ts.TypeChecker) => ts.TransformerFactory<ts.SourceFile>;
Expand Down
Loading