0

WebUI: Disallow extension-less JS imports via ESLint, part 2 (last).

In this part adding a new no-restricted-syntax ESLint check that
catches such violations.

Per the JS styleguide at [1] all JS import statements should explitcly
specify the file extension. The TS styleguide does not seem to have
that requirement and shows examples where the extension is omitted [2].

Given the prevalence of non-extensionless imports in Chromium's
codebase it seems better to follow the JS styleguide for both JS and
TS to be consistent with the vast majority of existing code.

[1] https://google.github.io/styleguide/jsguide.html#es-module-imports
[2] https://google.github.io/styleguide/tsguide.html#imports

Bug: 40519637
Change-Id: Ic941f5b4b648d8a8f1780fdeef3e348b6e44fedf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5375665
Commit-Queue: Demetrios Papadopoulos <dpapad@chromium.org>
Reviewed-by: Rebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1274565}
This commit is contained in:
dpapad
2024-03-18 23:51:34 +00:00
committed by Chromium LUCI CQ
parent d9e30b3ab8
commit a21b3e4f06

@ -81,6 +81,18 @@ module.exports = {
// Prevent a common misuse of "!" operator.
"selector": "TSNonNullExpression > CallExpression > MemberExpression[property.name=/^querySelectorAll$/]",
"message": "Remove unnecessary \"!\" non-null operator after querySelectorAll(). It always returns a non-null result",
},
{
// https://google.github.io/styleguide/jsguide.html#es-module-imports
// 1) Matching only import URLs that have at least one '/' slash, to
// avoid false positives for NodeJS imports like
// `import fs from 'fs';`.
// Using '\u002F' instead of '/' as the suggested workaround for
// https://github.com/eslint/eslint/issues/16555
// 2) Allowing extensions that have a length between 2-4 characters
// (for example js, css, json)
"selector": "ImportDeclaration[source.value=/^.*\\u002F.*(?<!\\.[a-z]{2}|\\.[a-z]{3}|\\.[a-z]{4})$/]",
"message": "Disallowed extensionless import. Explicitly specify the extension suffix."
}],
'no-throw-literal': 'error',
'no-trailing-spaces': 'error',