备份: 完整开发状态(含反混淆脚本和临时文件)
This commit is contained in:
202
node_modules/@codemod/matchers/LICENSE.txt
generated
vendored
Normal file
202
node_modules/@codemod/matchers/LICENSE.txt
generated
vendored
Normal file
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, March 2017
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright 2017 Brian Donovan
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
461
node_modules/@codemod/matchers/README.md
generated
vendored
Normal file
461
node_modules/@codemod/matchers/README.md
generated
vendored
Normal file
@@ -0,0 +1,461 @@
|
||||
# @codemod/matchers
|
||||
|
||||
Matchers for JavaScript & TypeScript codemods.
|
||||
|
||||
## Install
|
||||
|
||||
Install from [npm](https://npmjs.com/):
|
||||
|
||||
```sh
|
||||
$ npm install @codemod/matchers
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
> This package is primarily intended to be used by codemods with `@codemod/cli`, but can be used in any Babel plugin or JavaScript/TypeScript AST processor. Note that the examples below are all written in [TypeScript](https://typescriptlang.org/), but in most cases are identical to their JavaScript counterpart.
|
||||
|
||||
The easiest way to use this package is to use `@codemod/cli` like so:
|
||||
|
||||
```ts
|
||||
// `t` is `@babel/types`
|
||||
import { defineCodemod, t } from '@codemod/cli'
|
||||
|
||||
// `m` is the `@codemod/matchers` module
|
||||
export default defineCodemod(({ m }) => ({
|
||||
visitors: {
|
||||
// …
|
||||
},
|
||||
}))
|
||||
```
|
||||
|
||||
The rest of the README shows how to use `@codemod/matchers` without
|
||||
`@codemod/cli`, but the same patterns apply.
|
||||
|
||||
### Simple Matching
|
||||
|
||||
Just as you can build AST nodes with `@babel/types`, you can build AST node
|
||||
matchers to match an exact node with `@codemod/matchers`:
|
||||
|
||||
```ts
|
||||
import * as m from '@codemod/matchers'
|
||||
import * as t from '@babel/types'
|
||||
|
||||
// `matcher` only matches Identifier nodes named 'test'
|
||||
const matcher = m.identifier('test')
|
||||
|
||||
matcher.match(t.identifier('test')) // true
|
||||
matcher.match(t.identifier('test2')) // false
|
||||
```
|
||||
|
||||
### Fuzzy Matching
|
||||
|
||||
Matching exact nodes is not usually what you want, however. `@codemod/matchers` can build matchers where only part of the data is specified:
|
||||
|
||||
```ts
|
||||
import * as m from '@codemod/matchers'
|
||||
import * as t from '@babel/types'
|
||||
|
||||
// `matcher` matches any Identifier, regardless of name
|
||||
const matcher = m.identifier()
|
||||
|
||||
matcher.match(t.identifier('test')) // true
|
||||
matcher.match(t.identifier('test2')) // true
|
||||
matcher.match(t.emptyStatement()) // false
|
||||
```
|
||||
|
||||
Here's a more complex example that matches any `console.log` calls. Assume that `expr` parses the given JS as an expression:
|
||||
|
||||
```ts
|
||||
import * as m from '@codemod/matchers'
|
||||
|
||||
// `matcher` matches any `console.log(…)` call
|
||||
const matcher = m.callExpression(
|
||||
m.memberExpression(m.identifier('console'), m.identifier('log'), false)
|
||||
// `arguments` is omitted to match anything, or we could pass `m.anything()`
|
||||
)
|
||||
|
||||
matcher.match(expr('console.log()')) // true
|
||||
matcher.match(expr('console.log(1, 2)')) // true
|
||||
matcher.match(expr('console.log')) // false
|
||||
```
|
||||
|
||||
There are a variety of fuzzy matchers that come with `@codemod/matchers`:
|
||||
|
||||
```ts
|
||||
import * as m from '@codemod/matchers'
|
||||
|
||||
m.anyString().match('a string') // true
|
||||
m.anyString().match(1) // false
|
||||
|
||||
m.anyNumber().match(1) // true
|
||||
m.anyNumber().match('a string') // false
|
||||
|
||||
m.anything().match(1) // true
|
||||
m.anything().match('a string') // true
|
||||
m.anything().match(expr('foo')) // true
|
||||
m.anything().match(null) // true
|
||||
|
||||
m.anyNode().match(expr('a + b')) // true
|
||||
m.anyNode().match(expr('!a')) // true
|
||||
m.anyNode().match(1) // false
|
||||
m.anyNode().match('a string') // false
|
||||
```
|
||||
|
||||
### Capturing Matches
|
||||
|
||||
Often you'll want to capture part of the node that you've matched so that you can extract information from it or edit it.
|
||||
|
||||
```ts
|
||||
import * as m from '@codemod/matchers'
|
||||
|
||||
// matches `console.<consoleMethod>(…)` calls
|
||||
const consoleMethod = m.capture(m.identifier())
|
||||
const matcher = m.callExpression(
|
||||
m.memberExpression(m.identifier('console'), consoleMethod, false)
|
||||
// `arguments` is omitted to match anything, or we could pass `m.anything()`
|
||||
)
|
||||
|
||||
if (matcher.match(expr('console.log()'))) {
|
||||
console.log(`found console call: ${consoleMethod.current.name}`)
|
||||
}
|
||||
|
||||
if (matcher.match(expr('console.group("hi!")'))) {
|
||||
console.log(`found console call: ${consoleMethod.current.name}`)
|
||||
}
|
||||
|
||||
if (matcher.match(expr('notAConsoleCall()'))) {
|
||||
console.log(`found console call: ${consoleMethod.current.name}`)
|
||||
}
|
||||
|
||||
// logs:
|
||||
// found console call: log
|
||||
// found console call: group
|
||||
```
|
||||
|
||||
### Back-referencing Captures
|
||||
|
||||
Sometimes you'll want to refer to an earlier captured value in a later part of the matcher. For example, let's say you want to match a function expression which returns its argument:
|
||||
|
||||
```ts
|
||||
import * as m from '@codemod/matchers'
|
||||
|
||||
const argumentMatcher = m.capture(m.identifier())
|
||||
const matcher = m.functionExpression(
|
||||
m.anything(),
|
||||
[argumentMatcher],
|
||||
m.blockStatement([m.returnStatement(m.fromCapture(argumentMatcher))])
|
||||
)
|
||||
|
||||
matcher.match(expr('function(a) { return a; })')) // true
|
||||
matcher.match(expr('function id(a) { return a; })')) // true
|
||||
matcher.match(expr('function(a) { return b; })')) // false
|
||||
matcher.match(expr('function(a) { return 1; })')) // false
|
||||
matcher.match(expr('function(a) { return a + a; })')) // false
|
||||
```
|
||||
|
||||
### Use in a Codemod
|
||||
|
||||
All the previous examples have matchers testing a specific AST node. This is useful for illustration, but is not typically how you'd use them. Codemods written for `@codemod/cli` are [Babel plugins](https://github.com/jamiebuilds/babel-handbook/blob/master/translations/en/plugin-handbook.md) and therefore use the visitor pattern to process ASTs. Here's the above example that identifies functions that do nothing but return their argument again, this time as a Babel plugin that replaces such functions with a global `IDENTITY` reference:
|
||||
|
||||
```ts
|
||||
/**
|
||||
* Replaces identity functions with `IDENTITY`:
|
||||
*
|
||||
* list.filter(function(a) { return a; });
|
||||
*
|
||||
* becomes:
|
||||
*
|
||||
* list.filter(IDENTITY);
|
||||
*/
|
||||
import * as m from '@codemod/matchers'
|
||||
import * as t from '@babel/types'
|
||||
import { NodePath } from '@babel/traverse'
|
||||
|
||||
export default function () {
|
||||
return {
|
||||
visitor: {
|
||||
FunctionExpression(path: NodePath<t.FunctionExpression>): void {
|
||||
const argumentMatcher = m.capture(m.identifier())
|
||||
const matcher = m.functionExpression(
|
||||
m.anything(),
|
||||
[argumentMatcher],
|
||||
m.blockStatement([m.returnStatement(m.fromCapture(argumentMatcher))])
|
||||
)
|
||||
|
||||
if (matcher.match(path.node)) {
|
||||
path.replaceWith(t.identifier('IDENTITY'))
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Here is the same plugin again without using `@codemod/matchers`:
|
||||
|
||||
```ts
|
||||
/**
|
||||
* Replaces identity functions with `IDENTITY`:
|
||||
*
|
||||
* list.filter(function(a) { return a; });
|
||||
*
|
||||
* becomes:
|
||||
*
|
||||
* list.filter(IDENTITY);
|
||||
*/
|
||||
import * as t from '@babel/types'
|
||||
import { NodePath } from '@babel/traverse'
|
||||
|
||||
export default function () {
|
||||
return {
|
||||
visitor: {
|
||||
/**
|
||||
* This version of the codemod, which does not use `@codemod/matchers`,
|
||||
* is more verbose and more likely to have subtle bugs. For example,
|
||||
* it's easy to forget to check that the `return` statement actually has a
|
||||
* return value before checking that it is an identifier, which would
|
||||
* result in a crash.
|
||||
*/
|
||||
FunctionExpression(path: NodePath<t.FunctionExpression>): void {
|
||||
// ensure function has exactly one parameter
|
||||
if (path.node.params.length !== 1) {
|
||||
return
|
||||
}
|
||||
|
||||
// ensure parameter is an identifier
|
||||
const param = path.node.params[0]
|
||||
if (!t.isIdentifier(param)) {
|
||||
return
|
||||
}
|
||||
|
||||
// ensure function body has exactly one statement
|
||||
if (path.node.body.body.length !== 1) {
|
||||
return
|
||||
}
|
||||
|
||||
// ensure that statement is a return statement
|
||||
const statement = path.node.body.body[0]
|
||||
if (!t.isReturnStatement(statement)) {
|
||||
return
|
||||
}
|
||||
|
||||
// ensure the return actually returns something, an identifier
|
||||
if (!statement.argument || !t.isIdentifier(statement.argument)) {
|
||||
return
|
||||
}
|
||||
|
||||
// ensure returned identifier has same name as the param
|
||||
if (statement.argument.name !== param.name) {
|
||||
return
|
||||
}
|
||||
|
||||
// replace!
|
||||
path.replaceWith(t.identifier('IDENTITY'))
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Deep Matches
|
||||
|
||||
Sometimes you know you want to match a node but don't know its depth in the tree, and thus can't hardcode a whole matching tree. To deal with this situation you can use the `containerOf` matcher. For example, this matcher will find the first `done` call inside a mocha test, accounting for whatever name might have been used for the parameter:
|
||||
|
||||
```ts
|
||||
import * as m from '@codemod/matchers'
|
||||
|
||||
const doneParam = m.capture(m.identifier())
|
||||
const matcher = m.callExpression(m.identifier('test'), [
|
||||
m.anyString(),
|
||||
m.function(
|
||||
[doneParam],
|
||||
m.containerOf(m.callExpression(m.fromCapture(doneParam)))
|
||||
),
|
||||
])
|
||||
|
||||
// matches because there's a `done()` call
|
||||
matcher.match(
|
||||
expr(`
|
||||
test('setTimeout calls back around N ms later', function(done) {
|
||||
const now = Date.now();
|
||||
const duration = 5;
|
||||
|
||||
setTimeout(function() {
|
||||
assert.ok(Date.now() - now < 10);
|
||||
done();
|
||||
}, duration);
|
||||
});
|
||||
`)
|
||||
)
|
||||
|
||||
// does not match because there's no `done()` call
|
||||
matcher.match(
|
||||
expr(`
|
||||
test('adds things', function() {
|
||||
assert.strictEqual(3 + 4, 7);
|
||||
});
|
||||
`)
|
||||
)
|
||||
```
|
||||
|
||||
### Custom Matchers
|
||||
|
||||
The easiest way to build custom matchers is simply by composing existing ones:
|
||||
|
||||
```ts
|
||||
import * as m from '@codemod/matchers'
|
||||
import * as t from '@babel/types'
|
||||
|
||||
function plusEqualOne() {
|
||||
return m.assignmentExpression(
|
||||
'+=',
|
||||
m.anything(), // or just `undefined` for the same effect
|
||||
m.numericLiteral(1)
|
||||
)
|
||||
}
|
||||
|
||||
const matcher = plusEqualOne()
|
||||
|
||||
matcher.match(expr('a += 1')) // true
|
||||
matcher.match(expr('a.b += 1')) // true
|
||||
matcher.match(expr('a -= 1')) // false
|
||||
matcher.match(expr('a += 2')) // false
|
||||
```
|
||||
|
||||
You can build simple custom matchers easily using a predicate:
|
||||
|
||||
```ts
|
||||
import * as m from '@codemod/matchers'
|
||||
|
||||
const oddNumberMatcher = m.matcher(
|
||||
(value) => typeof value === 'number' && Math.abs(number % 2) === 1
|
||||
)
|
||||
|
||||
oddNumberMatcher.match(expr('-1')) // true
|
||||
oddNumberMatcher.match(expr('0')) // false
|
||||
oddNumberMatcher.match(expr('1')) // true
|
||||
oddNumberMatcher.match(expr('2')) // true
|
||||
oddNumberMatcher.match(expr('3')) // true
|
||||
oddNumberMatcher.match(expr('Infinity')) // false
|
||||
oddNumberMatcher.match(expr('NaN')) // false
|
||||
```
|
||||
|
||||
Such matchers are easily parameterized by wrapping it in a function:
|
||||
|
||||
```ts
|
||||
import * as m from '@codemod/matchers';
|
||||
|
||||
function stringMatching(pattern: RegExp) {
|
||||
return m.matcher(
|
||||
value => typeof value === 'string' && pattern.test(value)
|
||||
);
|
||||
)
|
||||
|
||||
const startsWithRun = stringMatching(/^run/);
|
||||
|
||||
startsWithRun.match('run'); // true
|
||||
startsWithRun.match('runner'); // true
|
||||
startsWithRun.match('running'); // true
|
||||
startsWithRun.match('ruining'); // false
|
||||
startsWithRun.match(' run'); // false
|
||||
startsWithRun.match(''); // false
|
||||
startsWithRun.match(1); // false
|
||||
```
|
||||
|
||||
A common case where you think you'd need a custom matcher is when you want one of a few possible values. In such cases you can use the `or` matcher:
|
||||
|
||||
```ts
|
||||
import * as m from '@codemod/matchers'
|
||||
|
||||
const matcher = m.or(m.anyString(), m.anyNumber())
|
||||
|
||||
matcher.match(1) // true
|
||||
matcher.match('string') // true
|
||||
matcher.match({}) // false
|
||||
matcher.match(expr('1')) // false
|
||||
```
|
||||
|
||||
Matching one of a few values is common when dealing with things such as functions, which could be arrow functions, function expressions, or function declarations. Here's a more general version of the `IDENTITY` codemod which uses the `or` matcher to also replace arrow functions:
|
||||
|
||||
```ts
|
||||
/**
|
||||
* Replaces identity functions with `IDENTITY`:
|
||||
*
|
||||
* list.filter(function(a) { return a; });
|
||||
* list2.filter(a => a);
|
||||
*
|
||||
* becomes:
|
||||
*
|
||||
* list.filter(IDENTITY);
|
||||
* list2.filter(IDENTITY);
|
||||
*/
|
||||
import * as m from '@codemod/matchers'
|
||||
import * as t from '@babel/types'
|
||||
import { NodePath } from '@babel/traverse'
|
||||
|
||||
export default function () {
|
||||
return {
|
||||
visitor: {
|
||||
FunctionExpression(path: NodePath<t.FunctionExpression>): void {
|
||||
const paramId = m.capture(m.identifier())
|
||||
const matcher = m.function(
|
||||
[paramId],
|
||||
m.or(
|
||||
m.blockStatement([m.returnStatement(m.fromCapture(paramId))]),
|
||||
m.fromCapture(paramId)
|
||||
)
|
||||
)
|
||||
|
||||
if (matcher.match(path.node)) {
|
||||
path.replaceWith(t.identifier('IDENTITY'))
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
You probably won't need it, but you can build your own by subclassing `Matcher`. Here's the same `stringMatching` but as a subclass of `Matcher`:
|
||||
|
||||
```ts
|
||||
import * as m from '@codemod/matchers'
|
||||
import * as t from '@babel/types'
|
||||
|
||||
// This is more ceremony than the simple predicate-based one above.
|
||||
class StringMatching extends m.Matcher<string> {
|
||||
constructor(private readonly pattern: RegExp) {
|
||||
super()
|
||||
}
|
||||
|
||||
matchValue(
|
||||
value: unknown,
|
||||
keys: ReadonlyArray<PropertyKey>
|
||||
): value is string {
|
||||
return typeof value === 'string' && this.pattern.test(value)
|
||||
}
|
||||
}
|
||||
|
||||
const startsWithRun = new StringMatching(/^run/)
|
||||
|
||||
startsWithRun.match('run') // true
|
||||
startsWithRun.match('runner') // true
|
||||
startsWithRun.match('running') // true
|
||||
startsWithRun.match('ruining') // false
|
||||
startsWithRun.match(' run') // false
|
||||
startsWithRun.match('') // false
|
||||
startsWithRun.match(1) // false
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
See [CONTRIBUTING.md](../../CONTRIBUTING.md) for information on setting up the project for development and on contributing to the project.
|
||||
|
||||
## License
|
||||
|
||||
Copyright 2019 Brian Donovan
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
|
||||
50
node_modules/@codemod/matchers/package.json
generated
vendored
Normal file
50
node_modules/@codemod/matchers/package.json
generated
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
{
|
||||
"name": "@codemod/matchers",
|
||||
"version": "1.7.1",
|
||||
"description": "Matchers for JavaScript & TypeScript codemods.",
|
||||
"repository": "https://github.com/codemod-js/codemod",
|
||||
"license": "Apache-2.0",
|
||||
"author": "Brian Donovan",
|
||||
"main": "build/index.js",
|
||||
"types": "build/index.d.ts",
|
||||
"files": [
|
||||
"build"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "tsc --build tsconfig.build.json",
|
||||
"clean": "rm -rf build tsconfig.build.tsbuildinfo",
|
||||
"lint": "eslint .",
|
||||
"lint:fix": "eslint . --fix",
|
||||
"prepublishOnly": "pnpm clean && pnpm build",
|
||||
"test": "is-ci test:coverage test:watch",
|
||||
"test:coverage": "jest --coverage",
|
||||
"test:watch": "jest --watch"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/types": "^7.20.7",
|
||||
"@codemod/utils": "^1.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.20.12",
|
||||
"@babel/generator": "^7.20.14",
|
||||
"@babel/traverse": "^7.20.13",
|
||||
"@codemod/core": "^2.2.0",
|
||||
"@codemod/parser": "^1.4.0",
|
||||
"@types/babel__core": "^7.20.0",
|
||||
"@types/babel__generator": "^7.6.4",
|
||||
"@types/babel__template": "^7.4.1",
|
||||
"@types/babel__traverse": "^7.18.3",
|
||||
"@types/dedent": "^0.7.0",
|
||||
"@types/jest": "^25.1.0",
|
||||
"@types/node": "^18.14.0",
|
||||
"@types/prettier": "^2.0.0",
|
||||
"dedent": "^0.7.0",
|
||||
"is-ci-cli": "^2.2.0",
|
||||
"jest": "^27.3.1",
|
||||
"typescript": "^4.9.5"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"gitHead": "21ef8147856dbd3525d1506eb94cd918662847a3"
|
||||
}
|
||||
202
node_modules/@codemod/parser/LICENSE.txt
generated
vendored
Normal file
202
node_modules/@codemod/parser/LICENSE.txt
generated
vendored
Normal file
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, March 2017
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright 2017 Brian Donovan
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
33
node_modules/@codemod/parser/README.md
generated
vendored
Normal file
33
node_modules/@codemod/parser/README.md
generated
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
# @codemod/parser
|
||||
|
||||
Wrapper around `@babel/parser` that allows parsing everything.
|
||||
|
||||
## Install
|
||||
|
||||
Install from [npm](https://npmjs.com/):
|
||||
|
||||
```sh
|
||||
$ npm install @codemod/parser
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```ts
|
||||
import { parse } from '@codemod/parser'
|
||||
|
||||
console.log(parse('a ?? b').program.body[0].expression.operator) // '??'
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
See [CONTRIBUTING.md](../../CONTRIBUTING.md) for information on setting up the project for development and on contributing to the project.
|
||||
|
||||
## License
|
||||
|
||||
Copyright 2019 Brian Donovan
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
|
||||
38
node_modules/@codemod/parser/package.json
generated
vendored
Normal file
38
node_modules/@codemod/parser/package.json
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"name": "@codemod/parser",
|
||||
"version": "1.4.1",
|
||||
"description": "Wrapper around @babel/parser that allows parsing everything.",
|
||||
"repository": "https://github.com/codemod-js/codemod",
|
||||
"license": "Apache-2.0",
|
||||
"author": "Brian Donovan",
|
||||
"main": "build/index.js",
|
||||
"types": "build/index.d.ts",
|
||||
"files": [
|
||||
"build"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "tsc --build tsconfig.build.json",
|
||||
"clean": "rm -rf build tsconfig.build.tsbuildinfo",
|
||||
"lint": "eslint .",
|
||||
"lint:fix": "eslint . --fix",
|
||||
"prepublishOnly": "pnpm clean && pnpm build",
|
||||
"test": "is-ci test:coverage test:watch",
|
||||
"test:coverage": "jest --coverage",
|
||||
"test:watch": "jest --watch"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/parser": "^7.20.15"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/types": "^7.20.7",
|
||||
"@types/jest": "^25.1.0",
|
||||
"@types/node": "^18.14.0",
|
||||
"is-ci-cli": "^2.2.0",
|
||||
"jest": "^27.3.1",
|
||||
"typescript": "^4.9.5"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"gitHead": "7d754b551d0c70c508892dccc10382bab91356f2"
|
||||
}
|
||||
40
node_modules/@codemod/utils/README.md
generated
vendored
Normal file
40
node_modules/@codemod/utils/README.md
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
# @codemod/utils
|
||||
|
||||
This package contains utilities for writing codemods. Mostly it exists to enable
|
||||
codemods to avoid depending on `@babel/types`, `@babel/core`, and others as well
|
||||
as `@codemod/cli`. It's easy to depend on incompatible versions of these
|
||||
packages, so this package wraps them and exports a single version of each.
|
||||
|
||||
## Install
|
||||
|
||||
Install from [npm](https://npmjs.com/):
|
||||
|
||||
```sh
|
||||
$ npm install @codemod/core
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
You can use this library directly, but typically it's provided by `@codemod/cli`
|
||||
when defining a codemod with `defineCodemod`. Here's an example of using it
|
||||
directly:
|
||||
|
||||
```ts
|
||||
// `m` is `@codemod/matchers`, a library of useful matchers
|
||||
// `t` is `@babel/types`, babel AST type predicates and builders
|
||||
import { t, m } from '@codemod/utils'
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
See [CONTRIBUTING.md](../../CONTRIBUTING.md) for information on setting up the project for development and on contributing to the project.
|
||||
|
||||
## License
|
||||
|
||||
Copyright 2023 Brian Donovan
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
|
||||
41
node_modules/@codemod/utils/package.json
generated
vendored
Normal file
41
node_modules/@codemod/utils/package.json
generated
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"name": "@codemod/utils",
|
||||
"version": "1.1.0",
|
||||
"description": "codemod rewrites JavaScript and TypeScript",
|
||||
"repository": "https://github.com/codemod-js/codemod.git",
|
||||
"license": "Apache-2.0",
|
||||
"author": "Brian Donovan",
|
||||
"main": "build/index.js",
|
||||
"types": "build/index.d.ts",
|
||||
"files": [
|
||||
"build"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "tsc --build tsconfig.build.json",
|
||||
"clean": "rm -rf build tsconfig.build.tsbuildinfo",
|
||||
"lint": "eslint .",
|
||||
"lint:fix": "eslint . --fix",
|
||||
"prepublishOnly": "yarn clean && yarn build",
|
||||
"test": "is-ci test:coverage test:watch",
|
||||
"test:coverage": "jest --coverage",
|
||||
"test:watch": "jest --watch"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/core": "^7.20.12",
|
||||
"@babel/types": "^7.20.7",
|
||||
"@codemod/parser": "^1.4.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/babel__core": "^7.20.0",
|
||||
"@types/babel__generator": "^7.6.4",
|
||||
"@types/babel__traverse": "^7.18.3",
|
||||
"@types/jest": "^29.4.0",
|
||||
"@types/node": "^18.14.0",
|
||||
"is-ci-cli": "^2.2.0",
|
||||
"jest": "^27.3.1"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"gitHead": "c7ba547a429235fb2682c6679877a38e53ebb832"
|
||||
}
|
||||
Reference in New Issue
Block a user