eslint-codemod-utils
Advanced tools
Comparing version 0.1.0 to 0.1.1
# eslint-codemod-utils | ||
## 0.1.1 | ||
### Patch Changes | ||
- ba82178: Adds additional test cases, further AST node types (WithStatement, IfStatement, ThrowStatement). | ||
- fbd92dd: Adds CatchClause, TryStatement, DoWhileStatement, ForInStatement, ForOfStatement, ArrayPattern support. | ||
## 0.1.0 | ||
@@ -4,0 +11,0 @@ |
@@ -24,2 +24,3 @@ "use strict"; | ||
const espree = __importStar(require("espree")); | ||
const nodes_1 = require("../nodes"); | ||
const ESPREE_OPTIONS = { | ||
@@ -308,2 +309,33 @@ ecmaVersion: 2015, | ||
}); | ||
describe('doWhileStatement', () => { | ||
test('basic', () => { | ||
const testString = [ | ||
`do {`, | ||
` console.log('work')`, | ||
`} while (1 < 3)`, | ||
].join('\n'); | ||
const { body } = espree.parse(testString); | ||
expect((0, nodes_1.doWhileStatement)(body[0]).toString()).eq(testString); | ||
}); | ||
}); | ||
describe('ifStatement', () => { | ||
test('basic', () => { | ||
const testString = [ | ||
`if (1 < 3) {} else if (1 == 0) {`, | ||
` console.log('success')`, | ||
`} else {`, | ||
` console.log('error')`, | ||
`}`, | ||
].join('\n'); | ||
const { body } = espree.parse(testString); | ||
expect((0, nodes_1.ifStatement)(body[0]).toString()).eq(testString); | ||
}); | ||
}); | ||
describe('throwStatement', () => { | ||
test('basic', () => { | ||
const testString = [`throw new Error();`].join('\n'); | ||
const { body } = espree.parse(testString); | ||
expect((0, nodes_1.throwStatement)(body[0]).toString()).eq(testString); | ||
}); | ||
}); | ||
describe('jsxOpeningElement', () => { | ||
@@ -310,0 +342,0 @@ test('with comment', () => { |
@@ -14,17 +14,19 @@ "use strict"; | ||
import 'blah' | ||
const someImport = import('hello.js') | ||
const someStatement = probably.blob | ||
function App({a: a}, {b: c}) { | ||
return ( | ||
<div> | ||
<Welcome name="Sara" otherName={2} isBlanketHidden {...x()} /> | ||
<Welcome name="Cahal" /> | ||
<Welcome name="Sara" otherName={2} isBlanketHidden {...x()} /> | ||
<Welcome name="Edite" /> | ||
<Welcome name="Cahal" /> | ||
<Welcome name="Edite" /> | ||
</div> | ||
</div> | ||
); | ||
@@ -56,2 +58,7 @@ } | ||
[] | ||
if (1 < 3) {} else if (1 == 0) { | ||
console.log('success') | ||
} else { | ||
console.log('error') | ||
} | ||
const yy = { | ||
@@ -71,2 +78,7 @@ a: 1, | ||
} | ||
try { | ||
throw new Error('bad thing'); | ||
} catch {} finally { | ||
console.log('cleanup') | ||
} | ||
while (i < 0) { | ||
@@ -76,4 +88,4 @@ break | ||
const y = <Modal isOpen={true} onClick={onClick}> | ||
hello | ||
</Modal> | ||
hello | ||
</Modal> | ||
ReactDOM.render(<App />, document.getElementById('root')) | ||
@@ -80,0 +92,0 @@ export { b as y }from './other' |
export declare const DEFAULT_WHITESPACE = "\n "; | ||
export declare const typeToHelperLookup: { | ||
readonly AssignmentProperty: <T>(node: T) => T; | ||
readonly AssignmentPattern: <T>(node: T) => T; | ||
readonly AssignmentExpression: import("./types").StringableASTNode<import("estree").AssignmentExpression>; | ||
readonly AwaitExpression: import("./types").StringableASTNode<import("estree").AwaitExpression>; | ||
readonly ArrayExpression: import("./types").StringableASTNode<import("estree").ArrayExpression>; | ||
readonly ArrayPattern: import("./types").StringableASTNode<import("estree").ArrayPattern>; | ||
readonly BlockStatement: import("./types").StringableASTNode<import("estree").BlockStatement>; | ||
@@ -29,2 +31,3 @@ readonly BinaryExpression: import("./types").StringableASTNode<import("estree").BinaryExpression>; | ||
readonly Identifier: import("./types").StringableASTNode<import("estree").Identifier>; | ||
readonly IfStatement: import("./types").StringableASTNode<import("estree").IfStatement>; | ||
readonly LabeledStatement: <T>(node: T) => T; | ||
@@ -36,2 +39,4 @@ readonly Literal: import("./types").StringableASTNode<import("estree").Literal>; | ||
readonly ForStatement: import("./types").StringableASTNode<import("estree").ForStatement>; | ||
readonly ForInStatement: import("./types").StringableASTNode<import("estree").ForInStatement>; | ||
readonly ForOfStatement: import("./types").StringableASTNode<import("estree").ForOfStatement>; | ||
readonly ImportSpecifier: import("./types").StringableASTNode<import("estree").ImportSpecifier>; | ||
@@ -42,2 +47,3 @@ readonly ImportNamespaceSpecifier: import("./types").StringableASTNode<import("estree").ImportNamespaceSpecifier>; | ||
readonly ThisExpression: import("./types").StringableASTNode<import("estree").ThisExpression>; | ||
readonly ThrowStatement: import("./types").StringableASTNode<import("estree").ThrowStatement>; | ||
readonly TemplateLiteral: import("./types").StringableASTNode<import("estree").TemplateLiteral>; | ||
@@ -57,2 +63,4 @@ readonly TemplateElement: import("./types").StringableASTNode<import("estree").TemplateElement>; | ||
readonly CallExpression: import("./types").StringableASTNode<import("estree").SimpleCallExpression>; | ||
readonly SimpleCallExpression: import("./types").StringableASTNode<import("estree").SimpleCallExpression>; | ||
readonly CatchClause: import("./types").StringableASTNode<import("estree").CatchClause>; | ||
readonly ContinueStatement: import("./types").StringableASTNode<import("estree").ContinueStatement>; | ||
@@ -63,2 +71,3 @@ readonly ClassDeclaration: import("./types").StringableASTNode<import("estree").ClassDeclaration>; | ||
readonly DebuggerStatement: import("./types").StringableASTNode<import("estree").DebuggerStatement>; | ||
readonly DoWhileStatement: import("./types").StringableASTNode<import("estree").DoWhileStatement>; | ||
readonly ExportNamedDeclaration: import("./types").StringableASTNode<import("estree").ExportNamedDeclaration>; | ||
@@ -81,2 +90,3 @@ readonly ExportSpecifier: import("./types").StringableASTNode<import("estree").ExportSpecifier>; | ||
readonly StaticBlock: import("./types").StringableASTNode<import("estree").StaticBlock>; | ||
readonly TryStatement: import("./types").StringableASTNode<import("estree").TryStatement>; | ||
readonly WhileStatement: import("./types").StringableASTNode<import("estree").WhileStatement>; | ||
@@ -83,0 +93,0 @@ readonly WithStatement: import("./types").StringableASTNode<import("estree").WithStatement>; |
@@ -11,5 +11,7 @@ "use strict"; | ||
AssignmentProperty: identity_1.identity, | ||
AssignmentPattern: identity_1.identity, | ||
AssignmentExpression: nodes_1.assignmentExpression, | ||
AwaitExpression: nodes_1.awaitExpression, | ||
ArrayExpression: nodes_1.arrayExpression, | ||
ArrayPattern: nodes_1.arrayPattern, | ||
BlockStatement: nodes_1.blockStatement, | ||
@@ -37,2 +39,3 @@ BinaryExpression: nodes_1.binaryExpression, | ||
Identifier: nodes_1.identifier, | ||
IfStatement: nodes_1.ifStatement, | ||
// TODO implement | ||
@@ -45,2 +48,4 @@ LabeledStatement: identity_1.identity, | ||
ForStatement: nodes_1.forStatement, | ||
ForInStatement: nodes_1.forInStatement, | ||
ForOfStatement: nodes_1.forOfStatement, | ||
ImportSpecifier: nodes_1.importSpecifier, | ||
@@ -51,2 +56,3 @@ ImportNamespaceSpecifier: nodes_1.importNamespaceSpecifier, | ||
ThisExpression: nodes_1.thisExpression, | ||
ThrowStatement: nodes_1.throwStatement, | ||
TemplateLiteral: nodes_1.templateLiteral, | ||
@@ -68,2 +74,4 @@ TemplateElement: nodes_1.templateElement, | ||
CallExpression: nodes_1.callExpression, | ||
SimpleCallExpression: nodes_1.callExpression, | ||
CatchClause: nodes_1.catchClause, | ||
ContinueStatement: nodes_1.continueStatement, | ||
@@ -74,2 +82,3 @@ ClassDeclaration: nodes_1.classDeclaration, | ||
DebuggerStatement: nodes_1.debuggerStatement, | ||
DoWhileStatement: nodes_1.doWhileStatement, | ||
ExportNamedDeclaration: nodes_1.exportNamedDeclaration, | ||
@@ -92,2 +101,3 @@ ExportSpecifier: nodes_1.exportSpecifier, | ||
StaticBlock: nodes_1.staticBlock, | ||
TryStatement: nodes_1.tryStatement, | ||
WhileStatement: nodes_1.whileStatement, | ||
@@ -94,0 +104,0 @@ WithStatement: nodes_1.withStatement, |
@@ -5,1 +5,2 @@ /** constants has to be first so that it resolves the map */ | ||
export * from './jsx-nodes'; | ||
export * from './utils/utils'; |
@@ -17,1 +17,2 @@ "use strict"; | ||
__exportStar(require("./jsx-nodes"), exports); | ||
__exportStar(require("./utils/utils"), exports); |
@@ -69,2 +69,3 @@ import * as estree from 'estree'; | ||
export declare const returnStatement: StringableASTNode<estree.ReturnStatement>; | ||
export declare const throwStatement: StringableASTNode<estree.ThrowStatement>; | ||
/** | ||
@@ -100,2 +101,50 @@ * __UnaryExpression__ | ||
/** | ||
* __IfStatement__ | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* if (test) { | ||
* // consequant | ||
* } else { | ||
* // alternate | ||
* } | ||
* ⌃⌃⌃⌃^^^^^^^^ | ||
* ``` | ||
* | ||
* @returns {estree.IfStatement} | ||
*/ | ||
export declare const ifStatement: StringableASTNode<estree.IfStatement>; | ||
/** | ||
* __CatchClause__ | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* // always inside a try statement | ||
* catch (e) {} | ||
* ⌃⌃⌃⌃^^^^^^^^ | ||
* ``` | ||
* | ||
* @returns {estree.CatchClause} | ||
*/ | ||
export declare const catchClause: StringableASTNode<estree.CatchClause>; | ||
/** | ||
* __TryStatement__ | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* try { | ||
* // block | ||
* } catch(e) { // <--- handler | ||
* | ||
* } finally {} // <--- finalizer | ||
* ⌃⌃⌃⌃^^^^^^^^ | ||
* ``` | ||
* | ||
* @returns {estree.TryStatement} | ||
*/ | ||
export declare const tryStatement: StringableASTNode<estree.TryStatement>; | ||
/** | ||
* __WithStatement__ | ||
@@ -149,2 +198,3 @@ * | ||
export declare const arrayExpression: StringableASTNode<estree.ArrayExpression>; | ||
export declare const arrayPattern: StringableASTNode<estree.ArrayPattern>; | ||
export declare const updateExpression: StringableASTNode<estree.UpdateExpression>; | ||
@@ -197,4 +247,7 @@ export declare const expressionStatement: StringableASTNode<estree.ExpressionStatement>; | ||
export declare const importDeclaration: StringableASTNode<estree.ImportDeclaration>; | ||
export declare const bigIntLiteral: StringableASTNode<estree.BigIntLiteral>; | ||
export declare const regExpLiteral: StringableASTNode<estree.RegExpLiteral>; | ||
export declare const literal: StringableASTNode<estree.Literal>; | ||
export declare const identifier: StringableASTNode<estree.Identifier>; | ||
export declare const doWhileStatement: StringableASTNode<estree.DoWhileStatement>; | ||
export declare const whileStatement: StringableASTNode<estree.WhileStatement>; | ||
@@ -205,2 +258,4 @@ export declare const switchCase: StringableASTNode<estree.SwitchCase>; | ||
export declare const forStatement: StringableASTNode<estree.ForStatement>; | ||
export declare const forInStatement: StringableASTNode<estree.ForInStatement>; | ||
export declare const forOfStatement: StringableASTNode<estree.ForOfStatement>; | ||
export declare const continueStatement: StringableASTNode<estree.ContinueStatement>; | ||
@@ -207,0 +262,0 @@ export declare const breakStatement: StringableASTNode<estree.BreakStatement>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.staticBlock = exports.awaitExpression = exports.assignmentExpression = exports.conditionalExpression = exports.debuggerStatement = exports.breakStatement = exports.continueStatement = exports.forStatement = exports.templateLiteral = exports.switchStatement = exports.switchCase = exports.whileStatement = exports.identifier = exports.literal = exports.importDeclaration = exports.templateElement = exports.importNamespaceSpecifier = exports.variableDeclaration = exports.variableDeclarator = exports.logicalExpression = exports.memberExpression = exports.emptyStatement = exports.objectExpression = exports.spreadElement = exports.objectPattern = exports.property = exports.newExpression = exports.expressionStatement = exports.updateExpression = exports.arrayExpression = exports.yieldExpression = exports.importSpecifier = exports.exportSpecifier = exports.exportAllDeclaration = exports.exportDefaultDeclaration = exports.exportNamedDeclaration = exports.importDefaultSpecifier = exports.importExpression = exports.withStatement = exports.thisExpression = exports.unaryExpression = exports.returnStatement = exports.blockStatement = exports.functionExpression = exports.taggedTemplateExpression = exports.arrowFunctionExpression = exports.sequenceExpression = exports.binaryExpression = exports.chainExpression = exports.callExpression = void 0; | ||
exports.program = exports.classExpression = exports.classDeclaration = exports.classBody = exports.propertyDefinition = exports.methodDefinition = exports.methodOrPropertyFn = exports.functionDeclaration = void 0; | ||
exports.templateLiteral = exports.switchStatement = exports.switchCase = exports.whileStatement = exports.doWhileStatement = exports.identifier = exports.literal = exports.regExpLiteral = exports.bigIntLiteral = exports.importDeclaration = exports.templateElement = exports.importNamespaceSpecifier = exports.variableDeclaration = exports.variableDeclarator = exports.logicalExpression = exports.memberExpression = exports.emptyStatement = exports.objectExpression = exports.spreadElement = exports.objectPattern = exports.property = exports.newExpression = exports.expressionStatement = exports.updateExpression = exports.arrayPattern = exports.arrayExpression = exports.yieldExpression = exports.importSpecifier = exports.exportSpecifier = exports.exportAllDeclaration = exports.exportDefaultDeclaration = exports.exportNamedDeclaration = exports.importDefaultSpecifier = exports.importExpression = exports.withStatement = exports.tryStatement = exports.catchClause = exports.ifStatement = exports.thisExpression = exports.unaryExpression = exports.throwStatement = exports.returnStatement = exports.blockStatement = exports.functionExpression = exports.taggedTemplateExpression = exports.arrowFunctionExpression = exports.sequenceExpression = exports.binaryExpression = exports.chainExpression = exports.callExpression = void 0; | ||
exports.program = exports.classExpression = exports.classDeclaration = exports.classBody = exports.propertyDefinition = exports.methodDefinition = exports.methodOrPropertyFn = exports.functionDeclaration = exports.staticBlock = exports.awaitExpression = exports.assignmentExpression = exports.conditionalExpression = exports.debuggerStatement = exports.breakStatement = exports.continueStatement = exports.forOfStatement = exports.forInStatement = exports.forStatement = void 0; | ||
const node_1 = require("./utils/node"); | ||
@@ -27,10 +27,13 @@ const constants_1 = require("./constants"); | ||
*/ | ||
const callExpression = ({ callee, arguments: calleeArgs, optional = false, }) => ({ | ||
__pragma: 'ecu', | ||
arguments: calleeArgs, | ||
callee, | ||
optional, | ||
type: 'CallExpression', | ||
toString: () => `${callee.type === 'Super' ? 'super' : (0, node_1.node)(callee)}${optional ? '?.' : ''}(${calleeArgs.map(node_1.node).join(', ')})`, | ||
}); | ||
const callExpression = ({ arguments: calleeArgs, callee, optional, ...other }) => { | ||
return { | ||
...other, | ||
__pragma: 'ecu', | ||
arguments: calleeArgs, | ||
callee, | ||
optional, | ||
type: 'CallExpression', | ||
toString: () => `${callee.type === 'Super' ? 'super' : (0, node_1.node)(callee)}${optional ? '?.' : ''}(${calleeArgs.map(node_1.node).join(', ')})`, | ||
}; | ||
}; | ||
exports.callExpression = callExpression; | ||
@@ -177,2 +180,17 @@ const chainExpression = ({ expression, ...other }) => { | ||
exports.returnStatement = returnStatement; | ||
const throwStatement = ({ argument, ...other }) => { | ||
return { | ||
...other, | ||
argument, | ||
__pragma: 'ecu', | ||
type: 'ThrowStatement', | ||
toString: () => `throw${argument | ||
? // @ts-expect-error | ||
argument.type === 'JSXElement' || argument.type === 'JSXFragment' | ||
? ` (${constants_1.DEFAULT_WHITESPACE}${(0, node_1.node)(argument)}${constants_1.DEFAULT_WHITESPACE})` | ||
: ` ${(0, node_1.node)(argument)}` | ||
: ''};`, | ||
}; | ||
}; | ||
exports.throwStatement = throwStatement; | ||
/** | ||
@@ -223,2 +241,76 @@ * __UnaryExpression__ | ||
/** | ||
* __IfStatement__ | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* if (test) { | ||
* // consequant | ||
* } else { | ||
* // alternate | ||
* } | ||
* ⌃⌃⌃⌃^^^^^^^^ | ||
* ``` | ||
* | ||
* @returns {estree.IfStatement} | ||
*/ | ||
const ifStatement = ({ test, alternate, consequent, ...other }) => ({ | ||
...other, | ||
test, | ||
alternate, | ||
consequent, | ||
type: 'IfStatement', | ||
__pragma: 'ecu', | ||
toString: () => `if (${(0, node_1.node)(test)}) ${(0, node_1.node)(consequent)} ${alternate ? `else ${(0, node_1.node)(alternate)}` : ''}`, | ||
}); | ||
exports.ifStatement = ifStatement; | ||
/** | ||
* __CatchClause__ | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* // always inside a try statement | ||
* catch (e) {} | ||
* ⌃⌃⌃⌃^^^^^^^^ | ||
* ``` | ||
* | ||
* @returns {estree.CatchClause} | ||
*/ | ||
const catchClause = ({ body, param, ...other }) => ({ | ||
...other, | ||
body, | ||
param, | ||
type: 'CatchClause', | ||
__pragma: 'ecu', | ||
toString: () => `catch${param ? ` (${(0, node_1.node)(param)})` : ''} ${(0, node_1.node)(body)}`, | ||
}); | ||
exports.catchClause = catchClause; | ||
/** | ||
* __TryStatement__ | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* try { | ||
* // block | ||
* } catch(e) { // <--- handler | ||
* | ||
* } finally {} // <--- finalizer | ||
* ⌃⌃⌃⌃^^^^^^^^ | ||
* ``` | ||
* | ||
* @returns {estree.TryStatement} | ||
*/ | ||
const tryStatement = ({ block, finalizer, handler, ...other }) => ({ | ||
...other, | ||
block, | ||
finalizer, | ||
handler, | ||
type: 'TryStatement', | ||
__pragma: 'ecu', | ||
toString: () => `try ${(0, node_1.node)(block)} ${handler ? (0, node_1.node)(handler) : ''} ${finalizer ? `finally ${(0, node_1.node)(finalizer)}` : ''}`, | ||
}); | ||
exports.tryStatement = tryStatement; | ||
/** | ||
* __WithStatement__ | ||
@@ -362,7 +454,24 @@ * | ||
__pragma: 'ecu', | ||
// @ts-expect-error | ||
toString: () => `[${elements.map(node_1.node).map(String).join(', ')}]`, | ||
toString: () => `[${elements | ||
.filter((n) => Boolean(n)) | ||
.map(node_1.node) | ||
.map(String) | ||
.join(', ')}]`, | ||
}; | ||
}; | ||
exports.arrayExpression = arrayExpression; | ||
const arrayPattern = ({ elements, ...other }) => { | ||
return { | ||
...other, | ||
type: 'ArrayPattern', | ||
elements, | ||
__pragma: 'ecu', | ||
toString: () => `[${elements | ||
.filter((n) => Boolean(n)) | ||
.map(node_1.node) | ||
.map(String) | ||
.join(', ')}]`, | ||
}; | ||
}; | ||
exports.arrayPattern = arrayPattern; | ||
const updateExpression = ({ argument, operator, prefix, ...other }) => { | ||
@@ -578,7 +687,7 @@ return { | ||
exports.importDeclaration = importDeclaration; | ||
const literal = ({ value, raw, | ||
// @ts-expect-error TODO bigint | ||
}) => ({ | ||
const bigIntLiteral = ({ value, raw, bigint, ...other }) => ({ | ||
...other, | ||
value, | ||
raw, | ||
bigint, | ||
type: 'Literal', | ||
@@ -588,2 +697,29 @@ __pragma: 'ecu', | ||
}); | ||
exports.bigIntLiteral = bigIntLiteral; | ||
const regExpLiteral = ({ value, raw, regex, ...other }) => ({ | ||
...other, | ||
value, | ||
raw, | ||
regex, | ||
type: 'Literal', | ||
__pragma: 'ecu', | ||
toString: () => raw || String(value), | ||
}); | ||
exports.regExpLiteral = regExpLiteral; | ||
const literal = (n) => { | ||
if ('bigint' in n) { | ||
return (0, exports.bigIntLiteral)(n); | ||
} | ||
else if ('regex' in n) { | ||
return (0, exports.regExpLiteral)(n); | ||
} | ||
else { | ||
return { | ||
...n, | ||
type: 'Literal', | ||
__pragma: 'ecu', | ||
toString: () => n.raw || String(n.value), | ||
}; | ||
} | ||
}; | ||
exports.literal = literal; | ||
@@ -597,2 +733,13 @@ const identifier = ({ name }) => ({ | ||
exports.identifier = identifier; | ||
const doWhileStatement = ({ test, body, ...other }) => ({ | ||
...other, | ||
__pragma: 'ecu', | ||
test, | ||
body, | ||
type: 'DoWhileStatement', | ||
toString() { | ||
return `do ${(0, node_1.node)(body)} while (${(0, node_1.node)(test)})`; | ||
}, | ||
}); | ||
exports.doWhileStatement = doWhileStatement; | ||
const whileStatement = ({ test, body, ...other }) => ({ | ||
@@ -673,2 +820,22 @@ ...other, | ||
exports.forStatement = forStatement; | ||
const forInStatement = ({ body, left, right, ...other }) => ({ | ||
...other, | ||
__pragma: 'ecu', | ||
body, | ||
left, | ||
right, | ||
type: 'ForInStatement', | ||
toString: () => `for (${(0, node_1.node)(left)} in ${(0, node_1.node)(right)}) ${(0, node_1.node)(body)}`, | ||
}); | ||
exports.forInStatement = forInStatement; | ||
const forOfStatement = ({ body, left, right, ...other }) => ({ | ||
...other, | ||
__pragma: 'ecu', | ||
body, | ||
left, | ||
right, | ||
type: 'ForOfStatement', | ||
toString: () => `for (${(0, node_1.node)(left)} of ${(0, node_1.node)(right)}) ${(0, node_1.node)(body)}`, | ||
}); | ||
exports.forOfStatement = forOfStatement; | ||
const continueStatement = ({ label, ...other }) => ({ | ||
@@ -675,0 +842,0 @@ ...other, |
@@ -23,2 +23,3 @@ import { | ||
import * as espree from 'espree' | ||
import { doWhileStatement, ifStatement, throwStatement } from '../nodes' | ||
@@ -385,2 +386,36 @@ const ESPREE_OPTIONS = { | ||
describe('doWhileStatement', () => { | ||
test('basic', () => { | ||
const testString = [ | ||
`do {`, | ||
` console.log('work')`, | ||
`} while (1 < 3)`, | ||
].join('\n') | ||
const { body } = espree.parse(testString) | ||
expect(doWhileStatement(body[0]).toString()).eq(testString) | ||
}) | ||
}) | ||
describe('ifStatement', () => { | ||
test('basic', () => { | ||
const testString = [ | ||
`if (1 < 3) {} else if (1 == 0) {`, | ||
` console.log('success')`, | ||
`} else {`, | ||
` console.log('error')`, | ||
`}`, | ||
].join('\n') | ||
const { body } = espree.parse(testString) | ||
expect(ifStatement(body[0]).toString()).eq(testString) | ||
}) | ||
}) | ||
describe('throwStatement', () => { | ||
test('basic', () => { | ||
const testString = [`throw new Error();`].join('\n') | ||
const { body } = espree.parse(testString) | ||
expect(throwStatement(body[0]).toString()).eq(testString) | ||
}) | ||
}) | ||
describe('jsxOpeningElement', () => { | ||
@@ -387,0 +422,0 @@ test('with comment', () => { |
@@ -12,17 +12,19 @@ import { program } from '..' | ||
import 'blah' | ||
const someImport = import('hello.js') | ||
const someStatement = probably.blob | ||
function App({a: a}, {b: c}) { | ||
return ( | ||
<div> | ||
<Welcome name="Sara" otherName={2} isBlanketHidden {...x()} /> | ||
<Welcome name="Cahal" /> | ||
<Welcome name="Sara" otherName={2} isBlanketHidden {...x()} /> | ||
<Welcome name="Edite" /> | ||
<Welcome name="Cahal" /> | ||
<Welcome name="Edite" /> | ||
</div> | ||
</div> | ||
); | ||
@@ -54,2 +56,7 @@ } | ||
[] | ||
if (1 < 3) {} else if (1 == 0) { | ||
console.log('success') | ||
} else { | ||
console.log('error') | ||
} | ||
const yy = { | ||
@@ -69,2 +76,7 @@ a: 1, | ||
} | ||
try { | ||
throw new Error('bad thing'); | ||
} catch {} finally { | ||
console.log('cleanup') | ||
} | ||
while (i < 0) { | ||
@@ -74,4 +86,4 @@ break | ||
const y = <Modal isOpen={true} onClick={onClick}> | ||
hello | ||
</Modal> | ||
hello | ||
</Modal> | ||
ReactDOM.render(<App />, document.getElementById('root')) | ||
@@ -78,0 +90,0 @@ export { b as y }from './other' |
@@ -73,2 +73,10 @@ import { | ||
withStatement, | ||
ifStatement, | ||
throwStatement, | ||
catchClause, | ||
tryStatement, | ||
doWhileStatement, | ||
forInStatement, | ||
forOfStatement, | ||
arrayPattern, | ||
} from './nodes' | ||
@@ -83,5 +91,7 @@ import { identity } from './utils/identity' | ||
AssignmentProperty: identity, | ||
AssignmentPattern: identity, | ||
AssignmentExpression: assignmentExpression, | ||
AwaitExpression: awaitExpression, | ||
ArrayExpression: arrayExpression, | ||
ArrayPattern: arrayPattern, | ||
BlockStatement: blockStatement, | ||
@@ -109,2 +119,3 @@ BinaryExpression: binaryExpression, | ||
Identifier: identifier, | ||
IfStatement: ifStatement, | ||
// TODO implement | ||
@@ -117,2 +128,4 @@ LabeledStatement: identity, | ||
ForStatement: forStatement, | ||
ForInStatement: forInStatement, | ||
ForOfStatement: forOfStatement, | ||
ImportSpecifier: importSpecifier, | ||
@@ -123,2 +136,3 @@ ImportNamespaceSpecifier: importNamespaceSpecifier, | ||
ThisExpression: thisExpression, | ||
ThrowStatement: throwStatement, | ||
TemplateLiteral: templateLiteral, | ||
@@ -140,2 +154,4 @@ TemplateElement: templateElement, | ||
CallExpression: callExpression, | ||
SimpleCallExpression: callExpression, | ||
CatchClause: catchClause, | ||
ContinueStatement: continueStatement, | ||
@@ -146,2 +162,3 @@ ClassDeclaration: classDeclaration, | ||
DebuggerStatement: debuggerStatement, | ||
DoWhileStatement: doWhileStatement, | ||
ExportNamedDeclaration: exportNamedDeclaration, | ||
@@ -164,2 +181,3 @@ ExportSpecifier: exportSpecifier, | ||
StaticBlock: staticBlock, | ||
TryStatement: tryStatement, | ||
WhileStatement: whileStatement, | ||
@@ -166,0 +184,0 @@ WithStatement: withStatement, |
@@ -5,1 +5,2 @@ /** constants has to be first so that it resolves the map */ | ||
export * from './jsx-nodes' | ||
export * from './utils/utils' |
253
lib/nodes.ts
@@ -28,16 +28,20 @@ import * as estree from 'estree' | ||
export const callExpression: StringableASTNode<estree.SimpleCallExpression> = ({ | ||
callee, | ||
arguments: calleeArgs, | ||
optional = false, | ||
}) => ({ | ||
__pragma: 'ecu', | ||
arguments: calleeArgs, | ||
callee, | ||
optional, | ||
type: 'CallExpression', | ||
toString: () => | ||
`${callee.type === 'Super' ? 'super' : node(callee)}${ | ||
optional ? '?.' : '' | ||
}(${calleeArgs.map(node).join(', ')})`, | ||
}) | ||
...other | ||
}) => { | ||
return { | ||
...other, | ||
__pragma: 'ecu', | ||
arguments: calleeArgs, | ||
callee, | ||
optional, | ||
type: 'CallExpression', | ||
toString: () => | ||
`${callee.type === 'Super' ? 'super' : node(callee)}${ | ||
optional ? '?.' : '' | ||
}(${calleeArgs.map(node).join(', ')})`, | ||
} | ||
} | ||
@@ -214,2 +218,23 @@ export const chainExpression: StringableASTNode<estree.ChainExpression> = ({ | ||
export const throwStatement: StringableASTNode<estree.ThrowStatement> = ({ | ||
argument, | ||
...other | ||
}) => { | ||
return { | ||
...other, | ||
argument, | ||
__pragma: 'ecu', | ||
type: 'ThrowStatement', | ||
toString: () => | ||
`throw${ | ||
argument | ||
? // @ts-expect-error | ||
argument.type === 'JSXElement' || argument.type === 'JSXFragment' | ||
? ` (${DEFAULT_WHITESPACE}${node(argument)}${DEFAULT_WHITESPACE})` | ||
: ` ${node(argument)}` | ||
: '' | ||
};`, | ||
} | ||
} | ||
/** | ||
@@ -265,2 +290,96 @@ * __UnaryExpression__ | ||
/** | ||
* __IfStatement__ | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* if (test) { | ||
* // consequant | ||
* } else { | ||
* // alternate | ||
* } | ||
* ⌃⌃⌃⌃^^^^^^^^ | ||
* ``` | ||
* | ||
* @returns {estree.IfStatement} | ||
*/ | ||
export const ifStatement: StringableASTNode<estree.IfStatement> = ({ | ||
test, | ||
alternate, | ||
consequent, | ||
...other | ||
}) => ({ | ||
...other, | ||
test, | ||
alternate, | ||
consequent, | ||
type: 'IfStatement', | ||
__pragma: 'ecu', | ||
toString: () => | ||
`if (${node(test)}) ${node(consequent)} ${ | ||
alternate ? `else ${node(alternate)}` : '' | ||
}`, | ||
}) | ||
/** | ||
* __CatchClause__ | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* // always inside a try statement | ||
* catch (e) {} | ||
* ⌃⌃⌃⌃^^^^^^^^ | ||
* ``` | ||
* | ||
* @returns {estree.CatchClause} | ||
*/ | ||
export const catchClause: StringableASTNode<estree.CatchClause> = ({ | ||
body, | ||
param, | ||
...other | ||
}) => ({ | ||
...other, | ||
body, | ||
param, | ||
type: 'CatchClause', | ||
__pragma: 'ecu', | ||
toString: () => `catch${param ? ` (${node(param)})` : ''} ${node(body)}`, | ||
}) | ||
/** | ||
* __TryStatement__ | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* try { | ||
* // block | ||
* } catch(e) { // <--- handler | ||
* | ||
* } finally {} // <--- finalizer | ||
* ⌃⌃⌃⌃^^^^^^^^ | ||
* ``` | ||
* | ||
* @returns {estree.TryStatement} | ||
*/ | ||
export const tryStatement: StringableASTNode<estree.TryStatement> = ({ | ||
block, | ||
finalizer, | ||
handler, | ||
...other | ||
}) => ({ | ||
...other, | ||
block, | ||
finalizer, | ||
handler, | ||
type: 'TryStatement', | ||
__pragma: 'ecu', | ||
toString: () => | ||
`try ${node(block)} ${handler ? node(handler) : ''} ${ | ||
finalizer ? `finally ${node(finalizer)}` : '' | ||
}`, | ||
}) | ||
/** | ||
* __WithStatement__ | ||
@@ -441,7 +560,31 @@ * | ||
__pragma: 'ecu', | ||
// @ts-expect-error | ||
toString: () => `[${elements.map(node).map(String).join(', ')}]`, | ||
toString: () => | ||
`[${elements | ||
.filter((n): n is estree.SpreadElement | estree.Expression => | ||
Boolean(n) | ||
) | ||
.map(node) | ||
.map(String) | ||
.join(', ')}]`, | ||
} | ||
} | ||
export const arrayPattern: StringableASTNode<estree.ArrayPattern> = ({ | ||
elements, | ||
...other | ||
}) => { | ||
return { | ||
...other, | ||
type: 'ArrayPattern', | ||
elements, | ||
__pragma: 'ecu', | ||
toString: () => | ||
`[${elements | ||
.filter((n): n is estree.Pattern => Boolean(n)) | ||
.map(node) | ||
.map(String) | ||
.join(', ')}]`, | ||
} | ||
} | ||
export const updateExpression: StringableASTNode<estree.UpdateExpression> = ({ | ||
@@ -738,9 +881,12 @@ argument, | ||
export const literal: StringableASTNode<estree.Literal> = ({ | ||
export const bigIntLiteral: StringableASTNode<estree.BigIntLiteral> = ({ | ||
value, | ||
raw, | ||
// @ts-expect-error TODO bigint | ||
bigint, | ||
...other | ||
}) => ({ | ||
...other, | ||
value, | ||
raw, | ||
bigint, | ||
type: 'Literal', | ||
@@ -751,2 +897,32 @@ __pragma: 'ecu', | ||
export const regExpLiteral: StringableASTNode<estree.RegExpLiteral> = ({ | ||
value, | ||
raw, | ||
regex, | ||
...other | ||
}) => ({ | ||
...other, | ||
value, | ||
raw, | ||
regex, | ||
type: 'Literal', | ||
__pragma: 'ecu', | ||
toString: () => raw || String(value), | ||
}) | ||
export const literal: StringableASTNode<estree.Literal> = (n) => { | ||
if ('bigint' in n) { | ||
return bigIntLiteral(n as estree.BigIntLiteral) | ||
} else if ('regex' in n) { | ||
return regExpLiteral(n as estree.RegExpLiteral) | ||
} else { | ||
return { | ||
...(n as estree.SimpleLiteral), | ||
type: 'Literal', | ||
__pragma: 'ecu', | ||
toString: () => n.raw || String(n.value), | ||
} | ||
} | ||
} | ||
export const identifier: StringableASTNode<estree.Identifier> = ({ name }) => ({ | ||
@@ -759,2 +935,17 @@ type: 'Identifier', | ||
export const doWhileStatement: StringableASTNode<estree.DoWhileStatement> = ({ | ||
test, | ||
body, | ||
...other | ||
}) => ({ | ||
...other, | ||
__pragma: 'ecu', | ||
test, | ||
body, | ||
type: 'DoWhileStatement', | ||
toString() { | ||
return `do ${node(body)} while (${node(test)})` | ||
}, | ||
}) | ||
export const whileStatement: StringableASTNode<estree.WhileStatement> = ({ | ||
@@ -864,2 +1055,32 @@ test, | ||
export const forInStatement: StringableASTNode<estree.ForInStatement> = ({ | ||
body, | ||
left, | ||
right, | ||
...other | ||
}) => ({ | ||
...other, | ||
__pragma: 'ecu', | ||
body, | ||
left, | ||
right, | ||
type: 'ForInStatement', | ||
toString: () => `for (${node(left)} in ${node(right)}) ${node(body)}`, | ||
}) | ||
export const forOfStatement: StringableASTNode<estree.ForOfStatement> = ({ | ||
body, | ||
left, | ||
right, | ||
...other | ||
}) => ({ | ||
...other, | ||
__pragma: 'ecu', | ||
body, | ||
left, | ||
right, | ||
type: 'ForOfStatement', | ||
toString: () => `for (${node(left)} of ${node(right)}) ${node(body)}`, | ||
}) | ||
export const continueStatement: StringableASTNode<estree.ContinueStatement> = ({ | ||
@@ -918,3 +1139,3 @@ label, | ||
operator, | ||
toString: () => `${node(left)}${operator}${node(right)}`, | ||
toString: () => `${node(left)}${operator as string}${node(right)}`, | ||
} | ||
@@ -921,0 +1142,0 @@ } |
{ | ||
"name": "eslint-codemod-utils", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "A collection of AST helper functions for more complex ESLint rule fixes.", | ||
@@ -11,2 +11,8 @@ "source": "lib/index.ts", | ||
}, | ||
"keywords": [ | ||
"eslint", | ||
"codemods", | ||
"jscodeshift", | ||
"espree" | ||
], | ||
"author": "Alex Hinds", | ||
@@ -13,0 +19,0 @@ "license": "ISC", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
678670
42
20623