Files
react-native-devtools-frontend/scripts/eslint_rules/lib/no_only_eslint_tests.js
T
Jack Franklin a00b89c22a Lint against TemplateResult|typeof nothing
We have a common pattern in our codebase where a function might return a
lit template, or the result of `Lit.nothing` (which is equivalent to
`{}`). Therefore we have a fair amount of code that looks like:

```
foo(): LitHtml.TemplateResult|{}
// or
foo(): LitHtml.TemplateResult|typeof LitHtml.nothing
```

I'd like us to be consistent over which we prefer, but also this feels
like a little bit of an implementation detail that's leaking out - to a
person using our components system, a Lit template result or `{}` are
really equivalent - and we shouldn't have code that cares.

Therefore I'm proposing we expose (this will be done in a separate CL):

```
type LitTemplate = TemplateResult|typeof nothing
```

And then use this ESLint rule to:

1) update existing code to use the new type
2) ban future code from not using the new type

Both of those steps will also be done in a follow-up CL, this CL
introduces the basic rule.

Bug: 1320753
Change-Id: I2f3d5029a695922c9a71334e34259597eaebfcc0
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3613881
Commit-Queue: Andres Olivares <andoli@chromium.org>
Commit-Queue: Jack Franklin <jacktfranklin@chromium.org>
Reviewed-by: Andres Olivares <andoli@chromium.org>
Auto-Submit: Jack Franklin <jacktfranklin@chromium.org>
2022-04-29 16:07:23 +00:00

58 lines
1.7 KiB
JavaScript

// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'Usage of only: true in ESLint tests',
category: 'Possible Errors',
},
fixable: 'code',
messages: {noOnlyInESLintTest: 'You cannot use only: true in an ESLint test.'},
schema: [] // no options
},
create: function(context) {
function checkForOnlyInTestCases(testCaseObjects) {
for (const testCase of testCaseObjects) {
if (!testCase || !testCase.properties) {
continue;
}
const onlyKeyProp = testCase.properties.find(prop => {
return prop.key.name === 'only';
});
if (onlyKeyProp) {
context.report({
node: onlyKeyProp,
messageId: 'noOnlyInESLintTest'
});
}
}
}
return {
// match ruleTester.run('foo', rule, {...})
'CallExpression[callee.object.name=\'ruleTester\'][callee.property.name=\'run\']'(node) {
// first argument = string name
// second argument = rule itself
// third argument = the object containing the test cases - what we want!
const tests = node.arguments[2];
if (!tests || !tests.properties) {
return;
}
for (const testProperty of tests.properties) {
// Iterate over the "valid" and "invalid" rules
// Here .value = the array of test cases, and .elements gets us each
// object (individual test case) within that array.
checkForOnlyInTestCases(testProperty.value.elements);
}
}
};
}
};