Skip to content

Commit 9b0fa1e

Browse files
committed
feature: @putout/engine-runner: replace: initialize match once
1 parent d6fcb2c commit 9b0fa1e

File tree

1 file changed

+13
-15
lines changed
  • packages/engine-runner/lib/replace

1 file changed

+13
-15
lines changed

packages/engine-runner/lib/replace/index.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,31 @@ const log = (from, path) => {
3939
};
4040

4141
const {keys, entries} = Object;
42-
4342
const {stringify} = JSON;
4443

4544
const stub = () => [];
4645
const stubMatch = () => ({});
4746
const packKeys = (a) => () => keys(a);
48-
const isObj = (a) => typeof a === 'object';
47+
const isObject = (a) => typeof a === 'object';
4948

5049
module.exports = ({rule, plugin, msg, options}) => {
50+
const maybeMatch = plugin.match || stubMatch;
51+
const match = maybeMatch({
52+
options,
53+
});
54+
5155
const {
5256
report,
5357
exclude = stub,
5458
replace,
55-
filter = getFilter(plugin.match, options),
59+
filter = getFilter(match),
5660
} = plugin;
5761

5862
const replaceItems = replace({
5963
options,
6064
});
6165

62-
const fix = getFix(replaceItems, plugin.match, options);
66+
const fix = getFix(replaceItems, match);
6367
const include = packKeys(replaceItems);
6468

6569
return {
@@ -135,27 +139,21 @@ const fix = (from, to, path) => {
135139
log(from, newPath);
136140
};
137141

138-
const getFix = (items, match = stubMatch, options) => (path) => {
139-
const initedMatch = match({
140-
options,
141-
});
142-
142+
const getFix = (items, match) => (path) => {
143143
for (const [from, to] of entries(items)) {
144144
const nodeFrom = template.ast(from);
145145

146146
if (compare(path, nodeFrom, {findUp: false})) {
147-
const matchFn = initedMatch[from];
147+
const matchFn = match[from];
148148

149149
if (!matchFn || runMatch(path, nodeFrom, matchFn))
150150
fix(from, to, path);
151151
}
152152
}
153153
};
154154

155-
const getFilter = (match = stubMatch, options) => (path) => {
156-
const all = entries(match({
157-
options,
158-
}));
155+
const getFilter = (match) => (path) => {
156+
const all = entries(match);
159157

160158
for (const [from, matchProperty] of all) {
161159
const nodeFrom = template.ast(from);
@@ -189,7 +187,7 @@ function parseTo(to, values, path) {
189187
if (!toStr)
190188
return null;
191189

192-
if (isObj(toStr) && toStr.type) {
190+
if (isObject(toStr) && toStr.type) {
193191
toStr.__putout_replace_cooked = true;
194192
return toStr;
195193
}

0 commit comments

Comments
 (0)