Skip to content

Commit

Permalink
Ignore unexpected case in catch-error-name and prefer-set-has (#1077
Browse files Browse the repository at this point in the history
)
  • Loading branch information
fisker committed Jan 27, 2021
1 parent 43b1e3c commit 79a64b1
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 22 deletions.
7 changes: 7 additions & 0 deletions rules/catch-error-name.js
Expand Up @@ -69,6 +69,13 @@ const create = context => {
const scope = context.getScope();
const variable = findVariable(scope, node);

// This was reported https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1075#issuecomment-768072967
// But can't reproduce, just ignore this case
/* istanbul ignore next */
if (!variable) {
return;
}

if (originalName === '_' && variable.references.length === 0) {
return;
}
Expand Down
8 changes: 8 additions & 0 deletions rules/prefer-set-has.js
Expand Up @@ -145,6 +145,14 @@ const create = context => {
return {
[selector]: node => {
const variable = findVariable(context.getScope(), node);

// This was reported https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1075#issuecomment-768073342
// But can't reproduce, just ignore this case
/* istanbul ignore next */
if (!variable) {
return;
}

const identifiers = getVariableIdentifiers(variable).filter(identifier => identifier !== node);

if (
Expand Down
71 changes: 70 additions & 1 deletion test/catch-error-name.js
Expand Up @@ -849,6 +849,75 @@ test.typescript({
})
`,
errors: [generateError('err', 'error')]
})
}),
// https://github.com/untitled-labs/metabase-custom/blob/0fbb8b3d6f183bff6ad786d5158ddabf745f1f5c/frontend/src/metabase/containers/dnd/ItemDragSource.jsx#L51
{
code: outdent`
@DragSource({
async endDrag(props, monitor, component) {
try {
} catch (e) {
alert("There was a problem moving these items: " + e);
}
}
})
export default class A {}
`,
output: outdent`
@DragSource({
async endDrag(props, monitor, component) {
try {
} catch (error) {
alert("There was a problem moving these items: " + error);
}
}
})
export default class A {}
`,
errors: 1
}
]
});

test.babel({
testerOptions: {
parserOptions: {
babelOptions: {
parserOpts: {
plugins: [
['decorators', {decoratorsBeforeExport: true}]
]
}
}
}
},
valid: [],
invalid: [
// https://github.com/untitled-labs/metabase-custom/blob/0fbb8b3d6f183bff6ad786d5158ddabf745f1f5c/frontend/src/metabase/containers/dnd/ItemDragSource.jsx#L51
{
code: outdent`
@DragSource({
async endDrag(props, monitor, component) {
try {
} catch (e) {
alert("1There was a problem moving these items: " + e);
}
}
})
export default class A {}
`,
output: outdent`
@DragSource({
async endDrag(props, monitor, component) {
try {
} catch (error) {
alert("1There was a problem moving these items: " + error);
}
}
})
export default class A {}
`,
errors: 1
}
]
});
3 changes: 3 additions & 0 deletions test/integration/projects.js
Expand Up @@ -143,6 +143,9 @@ module.exports = [
'https://github.com/astrofox-io/astrofox',
// #1075
'https://github.com/jaredLunde/masonic'
// These two project use `decorator`, try to enable when we use `@babel/eslint-parser`
// 'https://github.com/untitled-labs/metabase-custom',
// 'https://github.com/TheThingsNetwork/lorawan-stack',
].map(project => {
if (typeof project === 'string') {
project = {repository: project};
Expand Down
56 changes: 56 additions & 0 deletions test/prefer-set-has.js
Expand Up @@ -902,3 +902,59 @@ test({
}
]
});

test.babel({
testerOptions: {
parserOptions: {
babelOptions: {
parserOpts: {
plugins: [
['decorators', {decoratorsBeforeExport: true}]
]
}
}
}
},
valid: [
// https://github.com/TheThingsNetwork/lorawan-stack/blob/1dab30227e632ceade425e0c67d5f84316e830da/pkg/webui/console/containers/device-importer/index.js#L74
outdent`
@connect(
state => {
const availableComponents = ['is']
if (nsConfig.enabled) availableComponents.push('ns')
if (jsConfig.enabled) availableComponents.push('js')
if (asConfig.enabled) availableComponents.push('as')
return {
availableComponents,
}
},
)
export default class A {}
`
],
invalid: [
]
});

test.typescript({
valid: [
// https://github.com/TheThingsNetwork/lorawan-stack/blob/1dab30227e632ceade425e0c67d5f84316e830da/pkg/webui/console/containers/device-importer/index.js#L74
outdent`
@connect(
state => {
const availableComponents = ['is']
if (nsConfig.enabled) availableComponents.push('ns')
if (jsConfig.enabled) availableComponents.push('js')
return {
availableComponents,
}
},
)
export default class A {}
`
],
invalid: [
]
});
66 changes: 45 additions & 21 deletions test/utils/test.js
Expand Up @@ -25,32 +25,56 @@ function createTester(options) {
return tester;
}

runTest.typescript = tests => runTest({
...tests,
testerOptions: {parser: require.resolve('@typescript-eslint/parser')}
});
runTest.typescript = tests => {
const {testerOptions = {}} = tests;
testerOptions.parserOptions = testerOptions.parserOptions || {};

runTest.babel = tests => runTest({
...tests,
testerOptions: {
parser: require.resolve('@babel/eslint-parser'),
parserOptions: {
...defaultParserOptions,
return runTest({
...tests,
testerOptions: {
...testerOptions,
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {
...defaultParserOptions,
...testerOptions.parserOptions
}
}
});
};

runTest.babel = tests => {
const {testerOptions = {}} = tests;
testerOptions.parserOptions = testerOptions.parserOptions || {};
testerOptions.parserOptions.babelOptions = testerOptions.parserOptions.babelOptions || {};
testerOptions.parserOptions.babelOptions.parserOpts = testerOptions.parserOptions.babelOptions.parserOpts || {};
testerOptions.parserOptions.babelOptions.parserOpts.plugins = testerOptions.parserOptions.babelOptions.parserOpts.plugins || [];

requireConfigFile: false,
sourceType: 'module',
allowImportExportEverywhere: true,
babelOptions: {
parserOpts: {
plugins: [
'jsx',
'classProperties'
]
return runTest({
...tests,
testerOptions: {
...testerOptions,
parser: require.resolve('@babel/eslint-parser'),
parserOptions: {
...defaultParserOptions,
requireConfigFile: false,
sourceType: 'module',
allowImportExportEverywhere: true,
...testerOptions.parserOptions,
babelOptions: {
...testerOptions.parserOptions.babelOptions,
parserOpts: {
...testerOptions.parserOptions.babelOptions.parserOpts,
plugins: [
'jsx',
'classProperties',
...testerOptions.parserOptions.babelOptions.parserOpts.plugins
]
}
}
}
}
}
});
});
};

runTest.babelLegacy = tests => runTest({
...tests,
Expand Down

0 comments on commit 79a64b1

Please sign in to comment.