-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from bttmly/better_filters
Better filters
- Loading branch information
Showing
13 changed files
with
277 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import * as R from "ramda"; | ||
import * as escodegen from "escodegen"; | ||
import S from "../mutators/_syntax"; | ||
import { LocationFilter } from "../types"; | ||
|
||
export const isStringRequire = R.allPass([ | ||
R.propEq("type", S.CallExpression), | ||
R.pathEq(["callee", "name"], "require"), | ||
R.pathEq(["arguments", "length"], 1), | ||
R.pathEq(["arguments", "0", "type"], S.Literal), | ||
n => typeof R.path(["arguments", "0", "value"], n) === "string", | ||
]); | ||
|
||
export const isUseStrict = R.allPass([ | ||
R.propEq("type", S.ExpressionStatement), | ||
R.pathEq(["expression", "value"], "use strict"), | ||
]); | ||
|
||
export const isCallOfName = (name: string): LocationFilter => { | ||
return ({ node }) => { | ||
if (!R.propEq("type", S.CallExpression, node)) return false; | ||
if (!R.pathEq(["callee", "type"], S.Identifier, node)) return false; | ||
if (!R.pathEq(["callee", "name"], name, node)) return false; | ||
return true; | ||
}; | ||
}; | ||
|
||
export function nodeSourceIncludes(text: string): LocationFilter { | ||
return ({ node }) => { | ||
return escodegen.generate(node).includes(text); | ||
}; | ||
} | ||
|
||
export function sourceNodeIncludesAny(texts: string[]): LocationFilter { | ||
return ({ node }) => { | ||
const code = escodegen.generate(node); | ||
return texts.some(t => code.includes(t)); | ||
}; | ||
} | ||
|
||
export function nodeSourceMatches(re: RegExp): LocationFilter { | ||
return ({ node }) => { | ||
return re.test(escodegen.generate(node)); | ||
}; | ||
} | ||
|
||
export function sourceNodeMatchesAny(res: RegExp[]): LocationFilter { | ||
return ({ node }) => { | ||
const code = escodegen.generate(node); | ||
return res.some(re => re.test(code)); | ||
}; | ||
} | ||
|
||
export function sourceNodeIs(text: string): LocationFilter { | ||
return ({ node }) => { | ||
return escodegen.generate(node).trim() === text; | ||
}; | ||
} | ||
|
||
export const isESModuleInterop = nodeSourceIncludes( | ||
"Object.defineProperty(exports, '__esModule', { value: true });", | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,49 @@ | ||
export function hasProp(prop: string) { | ||
return (node: any) => node.hasOwnProperty(prop) && node[prop] !== null; | ||
import * as R from "ramda"; | ||
import { NodeFilter } from "../types"; | ||
import * as escodegen from "escodegen"; | ||
import S from "../mutators/_syntax"; | ||
|
||
export function hasProp(prop: string): NodeFilter { | ||
return node => { | ||
return R.prop(prop, node as any) != null; | ||
}; | ||
} | ||
|
||
export const isCallOfName = (name: string): NodeFilter => { | ||
return node => { | ||
if (!R.propEq("type", S.CallExpression, node)) return false; | ||
if (!R.pathEq(["callee", "type"], S.Identifier, node)) return false; | ||
if (!R.pathEq(["callee", "name"], name, node)) return false; | ||
return true; | ||
}; | ||
}; | ||
|
||
export function nodeSourceIncludes(text: string): NodeFilter { | ||
return node => escodegen.generate(node).includes(text); | ||
} | ||
|
||
export function sourceNodeIncludesAny(texts: string[]): NodeFilter { | ||
return node => { | ||
const code = escodegen.generate(node); | ||
return texts.some(t => code.includes(t)); | ||
}; | ||
} | ||
|
||
export function nodeSourceMatches(re: RegExp): NodeFilter { | ||
return node => re.test(escodegen.generate(node)); | ||
} | ||
|
||
export function sourceNodeMatchesAny(res: RegExp[]): NodeFilter { | ||
return node => { | ||
const code = escodegen.generate(node); | ||
return res.some(re => re.test(code)); | ||
}; | ||
} | ||
|
||
export function sourceNodeIs(text: string): NodeFilter { | ||
return node => escodegen.generate(node).trim() === text; | ||
} | ||
|
||
export const isESModuleInterop = nodeSourceIncludes( | ||
"Object.defineProperty(exports, '__esModule', { value: true });", | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as R from "ramda"; | ||
import S from "./_syntax"; | ||
import { VOID_NODE } from "./_constant-nodes"; | ||
import { MutatorPlugin } from "../types"; | ||
|
||
// a default parameter | ||
// input: `function fn (x = 1) {}` | ||
// output: `function fn (x) {}` | ||
|
||
const plugin: MutatorPlugin = { | ||
type: "mutator", | ||
name: "drop-return", | ||
nodeTypes: S.FUNC_NODES, | ||
mutator: R.ifElse( | ||
node => node.argument == null, | ||
R.always(VOID_NODE), | ||
node => ({ | ||
type: S.ExpressionStatement, | ||
expression: node.argument, | ||
}), | ||
), | ||
}; | ||
|
||
export default plugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.