Skip to content

Commit

Permalink
Merge 1250471 into 74ddbf9
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 17, 2020
2 parents 74ddbf9 + 1250471 commit 2ade08b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
10 changes: 6 additions & 4 deletions rules/catch-error-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const astUtils = require('eslint-ast-utils');
const avoidCapture = require('./utils/avoid-capture');
const getDocumentationUrl = require('./utils/get-documentation-url');
const renameIdentifier = require('./utils/rename-identifier');

// Matches `someObj.then([FunctionExpression | ArrowFunctionExpression])`
function isLintablePromiseCatch(node) {
Expand Down Expand Up @@ -38,7 +39,8 @@ const create = context => {
...context.options[0]
};

const {scopeManager} = context.getSourceCode();
const sourceCode = context.getSourceCode();
const {scopeManager} = sourceCode;

const {name} = options;
const caughtErrorsIgnorePattern = new RegExp(options.caughtErrorsIgnorePattern);
Expand All @@ -64,7 +66,7 @@ const create = context => {

if (node.type === 'Identifier') {
problem.fix = fixer => {
const fixings = [fixer.replaceText(node, expectedName)];
const nodes = [node];

const variables = scopeManager.getDeclaredVariables(scopeNode);
for (const variable of variables) {
Expand All @@ -73,11 +75,11 @@ const create = context => {
}

for (const reference of variable.references) {
fixings.push(fixer.replaceText(reference.identifier, expectedName));
nodes.push(reference.identifier);
}
}

return fixings;
return nodes.map(node => renameIdentifier(node, expectedName, fixer, sourceCode));
};
}

Expand Down
1 change: 0 additions & 1 deletion rules/no-keyword-prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ module.exports = {
docs: {
url: getDocumentationUrl(__filename)
},
fixable: 'code',
schema,
messages: {
[MESSAGE_ID]: 'Do not prefix identifiers with keyword `{{keyword}}`.'
Expand Down
14 changes: 14 additions & 0 deletions test/catch-error-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const ruleTester = avaRuleTester(test, {
}
});

const typescriptRuleTester = avaRuleTester(test, {
parser: require.resolve('@typescript-eslint/parser')
});

function invalidTestCase(options) {
if (typeof options === 'string') {
options = {code: options};
Expand Down Expand Up @@ -416,3 +420,13 @@ ruleTester.run('catch-error-name', rule, {
}
]
});

typescriptRuleTester.run('catch-error-name', rule, {
valid: [],
invalid: [
invalidTestCase({
code: 'promise.catch(function (err: Error) { console.log(err) })',
output: 'promise.catch(function (error: Error) { console.log(error) })'
})
]
});

0 comments on commit 2ade08b

Please sign in to comment.