Skip to content

Commit c67dfff

Browse files
committed
Extract switchNewExpressionToCallExpression
1 parent bd98e5d commit c67dfff

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

rules/new-for-builtins.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const getDocumentationUrl = require('./utils/get-documentation-url');
33
const builtins = require('./utils/builtins');
44
const isShadowed = require('./utils/is-shadowed');
5-
const isNewExpressionWithParentheses = require('./utils/is-new-expression-with-parentheses');
5+
const switchNewExpressionToCallExpression = require('./utils/switch-new-expression-to-call-expression');
66

77
const messages = {
88
enforce: 'Use `new {{name}}()` instead of `{{name}}()`.',
@@ -40,7 +40,7 @@ const create = context => {
4040
}
4141
},
4242
NewExpression: node => {
43-
const {callee, range} = node;
43+
const {callee} = node;
4444
const {name} = callee;
4545

4646
if (disallowNew.has(name) && !isShadowed(context.getScope(), callee)) {
@@ -52,17 +52,7 @@ const create = context => {
5252

5353
if (name !== 'String' && name !== 'Boolean' && name !== 'Number') {
5454
problem.fix = function * (fixer) {
55-
const [start] = range;
56-
let end = start + 3; // `3` = length of `new`
57-
const textAfter = sourceCode.text.slice(end);
58-
const [leadingSpaces] = textAfter.match(/^\s*/);
59-
end += leadingSpaces.length;
60-
61-
yield fixer.removeRange([start, end]);
62-
63-
if (!isNewExpressionWithParentheses(node, sourceCode)) {
64-
yield fixer.insertTextAfter(node, '()');
65-
}
55+
yield * switchNewExpressionToCallExpression(node, sourceCode, fixer);
6656
};
6757
}
6858

rules/no-new-buffer.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
const {getStaticValue} = require('eslint-utils');
33
const getDocumentationUrl = require('./utils/get-documentation-url');
4-
const isNewExpressionWithParentheses = require('./utils/is-new-expression-with-parentheses');
4+
const switchNewExpressionToCallExpression = require('./utils/switch-new-expression-to-call-expression');
55

66
const ERROR = 'error';
77
const ERROR_UNKNOWN = 'error-unknown';
@@ -44,18 +44,8 @@ const inferMethod = (bufferArguments, scope) => {
4444

4545
function fix(node, sourceCode, method) {
4646
return function * (fixer) {
47-
const [start] = node.range;
48-
let end = start + 3; // `3` = length of `new`
49-
const textAfter = sourceCode.text.slice(end);
50-
const [leadingSpaces] = textAfter.match(/^\s*/);
51-
end += leadingSpaces.length;
52-
yield fixer.removeRange([start, end]);
53-
47+
yield * switchNewExpressionToCallExpression(node, sourceCode, fixer);
5448
yield fixer.insertTextAfter(node.callee, `.${method}`);
55-
56-
if (!isNewExpressionWithParentheses(node, sourceCode)) {
57-
yield fixer.insertTextAfter(node, '()');
58-
}
5949
};
6050
}
6151

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
const isNewExpressionWithParentheses = require('./is-new-expression-with-parentheses');
3+
4+
function * switchNewExpressionToCallExpression(node, sourceCode, fixer) {
5+
const [start] = node.range;
6+
let end = start + 3; // `3` = length of `new`
7+
const textAfter = sourceCode.text.slice(end);
8+
const [leadingSpaces] = textAfter.match(/^\s*/);
9+
end += leadingSpaces.length;
10+
yield fixer.removeRange([start, end]);
11+
12+
if (!isNewExpressionWithParentheses(node, sourceCode)) {
13+
yield fixer.insertTextAfter(node, '()');
14+
}
15+
}
16+
17+
module.exports = switchNewExpressionToCallExpression

0 commit comments

Comments
 (0)