-
Notifications
You must be signed in to change notification settings - Fork 254
custom eslint plugin that fixes typedef imports #11949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1f042d2
chore: refresh type coverage
turadg 2c8be70
ci: allow lint commands to run independently
turadg 5b24518
chore(deps): bump eslint
turadg ed409e5
feat: scaffold @agoric/eslint-plugin
turadg 82fdd9f
chore: remove dollar-sign scaffold
turadg 922ec6c
refactor: Convert ESM modules to CJS in eslint-plugin rules
turadg 7ac8fa5
feat: rule to prevent typedef for type imports
turadg 1061bd3
lint: adopt @agoric/eslint-plugin/recommended
turadg 3b968fb
lint: disable no-typedef-import where legitimate
turadg 768d44f
ci: test eslint-plugin
turadg 61b4489
lint: fix @agoric/no-typedef-import
turadg 94efa18
chore(types): fix newly detected errors
turadg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,6 +102,6 @@ | |
| "access": "public" | ||
| }, | ||
| "typeCoverage": { | ||
| "atLeast": 76.59 | ||
| "atLeast": 76.71 | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| /* eslint-disable @agoric/no-typedef-import -- it's the only way to re-export with JSDoc */ | ||
| export {}; | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,6 +82,6 @@ | |
| "timeout": "20m" | ||
| }, | ||
| "typeCoverage": { | ||
| "atLeast": 86.54 | ||
| "atLeast": 86.47 | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import eslintPluginEslintPlugin from 'eslint-plugin-eslint-plugin'; | ||
|
|
||
| export default [ | ||
| { | ||
| plugins: { | ||
| 'eslint-plugin': eslintPluginEslintPlugin, | ||
| }, | ||
| extends: ['plugin:eslint-plugin/recommended'], | ||
| }, | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| { | ||
| "name": "@agoric/eslint-plugin", | ||
| "version": "0.1.0", | ||
| "description": "ESLint plugin for Agoric best practices", | ||
| "main": "src/index.js", | ||
| "type": "commonjs", | ||
| "files": [ | ||
| "src" | ||
| ], | ||
| "keywords": [ | ||
| "eslint", | ||
| "eslintplugin", | ||
| "eslint-plugin" | ||
| ], | ||
| "scripts": { | ||
| "build": "exit 0", | ||
| "lint": "yarn run -T run-s --continue-on-error 'lint:*'", | ||
| "lint:types": "yarn run -T tsc", | ||
| "test": "node --test **/*.test.*", | ||
| "test:xs": "exit 0" | ||
| }, | ||
| "peerDependencies": { | ||
| "eslint": "^9.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "eslint": "^9.0.0", | ||
| "eslint-plugin-eslint-plugin": "^7.2.0" | ||
| }, | ||
| "license": "Apache-2.0", | ||
| "engines": { | ||
| "node": "^20.9 || ^22.11" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| const fs = require('node:fs'); | ||
| const path = require('path'); | ||
|
|
||
| const pkg = JSON.parse( | ||
| fs.readFileSync(path.join(__dirname, '../package.json'), 'utf8'), | ||
| ); | ||
|
|
||
| // Import rules | ||
| const noTypedefImport = require('./rules/no-typedef-import.js'); | ||
|
|
||
| module.exports = { | ||
| meta: { | ||
| name: pkg.name, | ||
| version: pkg.version, | ||
| }, | ||
|
|
||
| // Rule definitions | ||
| rules: { | ||
| 'no-typedef-import': noTypedefImport, | ||
| }, | ||
|
|
||
| // Recommended config | ||
| configs: { | ||
| recommended: { | ||
| plugins: ['@agoric'], | ||
| rules: { | ||
| '@agoric/no-typedef-import': 'error', | ||
| }, | ||
| }, | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /** | ||
| * ESLint rule: no-typedef-import | ||
| * | ||
| * Finds inline `import()` expressions inside `@typedef` declarations and replaces | ||
| * them with a corresponding `@import` directive. This prevents generating new type aliases | ||
| * and unwittingly exporting them from the module. | ||
| * | ||
| * If the module should be exporting the types, `@typedef` is still the only way in JSDoc | ||
| * so disable this rule in those files. | ||
| * | ||
| * @see https://github.com/microsoft/typescript/issues/60831 | ||
| */ | ||
|
|
||
| const LINE_REGEX = | ||
| /^(\s*\*?\s*)@typedef\s+\{\s*import\((['"])([^'"]+)\2\)\.([A-Za-z0-9_$]+)([^}]*)\}\s+([A-Za-z0-9_$]+)(.*)$/; | ||
|
|
||
| const getConversionPlan = value => { | ||
| const lines = value.split('\n'); | ||
| const convertible = []; | ||
|
|
||
| for (let index = 0; index < lines.length; index += 1) { | ||
| const line = lines[index]; | ||
| const match = line.match(LINE_REGEX); | ||
| if (!match) { | ||
| continue; | ||
| } | ||
|
|
||
| const [ | ||
| , | ||
| indent, | ||
| quote, | ||
| importPath, | ||
| importedType, | ||
| rawSuffix = '', | ||
| , | ||
| // typedefName (unused), | ||
| trailing = '', | ||
| ] = match; | ||
|
|
||
| if (rawSuffix && rawSuffix.trim() !== '') { | ||
| continue; // skip typedefs that add generics or other suffixes | ||
| } | ||
|
|
||
| convertible.push({ | ||
| index, | ||
| indent, | ||
| quote, | ||
| importPath, | ||
| importedType, | ||
| trailing, | ||
| }); | ||
| } | ||
|
|
||
| return { lines, convertible }; | ||
| }; | ||
|
|
||
| const transformCommentValue = value => { | ||
| const { lines, convertible } = getConversionPlan(value); | ||
| if (!convertible.length) { | ||
| return null; | ||
| } | ||
|
|
||
| for (const { | ||
| index, | ||
| indent, | ||
| quote, | ||
| importPath, | ||
| importedType, | ||
| trailing, | ||
| } of convertible) { | ||
| const trailingText = trailing || ''; | ||
| lines[index] = | ||
| `${indent}@import {${importedType}} from ${quote}${importPath}${quote};${trailingText}`; | ||
| } | ||
|
|
||
| return lines.join('\n'); | ||
| }; | ||
|
|
||
| /** @type {import('eslint').Rule.RuleModule} */ | ||
| module.exports = { | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: | ||
| 'Disallow inline import() expressions inside @typedef; prefer @import directives.', | ||
| recommended: false, | ||
| }, | ||
| fixable: 'code', | ||
| schema: [], | ||
| messages: { | ||
| noTypedefImport: | ||
| 'Use an @import directive instead of an inline import() inside @typedef.', | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| const sourceCode = context.getSourceCode(); | ||
|
|
||
| return { | ||
| Program() { | ||
| for (const comment of sourceCode.getAllComments()) { | ||
| if (comment.type !== 'Block') { | ||
| continue; | ||
| } | ||
| if (!comment.value.includes('@typedef')) { | ||
| continue; | ||
| } | ||
|
|
||
| const { convertible } = getConversionPlan(comment.value); | ||
| if (!convertible.length) { | ||
| continue; | ||
| } | ||
|
|
||
| const { range, loc } = comment; | ||
| if (!range) { | ||
| continue; | ||
| } | ||
|
|
||
| const originalText = sourceCode.text.slice(range[0], range[1]); | ||
| const reportLoc = loc ?? { | ||
| start: sourceCode.getLocFromIndex(range[0]), | ||
| end: sourceCode.getLocFromIndex(range[1]), | ||
| }; | ||
|
|
||
| const descriptor = { | ||
| loc: reportLoc, | ||
| messageId: 'noTypedefImport', | ||
| fix(fixer) { | ||
| const newValue = transformCommentValue(comment.value); | ||
| if (!newValue) { | ||
| return null; | ||
| } | ||
|
|
||
| const newCommentText = originalText.replace( | ||
| comment.value, | ||
| newValue, | ||
| ); | ||
| return fixer.replaceTextRange(range, newCommentText); | ||
| }, | ||
| }; | ||
|
|
||
| context.report(descriptor); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; |
52 changes: 52 additions & 0 deletions
52
packages/eslint-plugin/tests/rules/no-typedef-import.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| const { RuleTester } = require('eslint'); | ||
| const rule = require('../../src/rules/no-typedef-import.js'); | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| languageOptions: { ecmaVersion: 2022, sourceType: 'module' }, | ||
| }); | ||
|
|
||
| ruleTester.run('no-typedef-import', rule, { | ||
| valid: [ | ||
| { | ||
| code: `/** | ||
| * @import {BundleCap} from './types.js'; | ||
| */`, | ||
| }, | ||
| { | ||
| code: `/** Just a doc block without typedef imports */`, | ||
| }, | ||
| { | ||
| // Not just an import; it's qualifying the type. | ||
| code: `/** | ||
| * @typedef { import('@endo/marshal').CapData<string> } SwingSetCapData | ||
| */`, | ||
| }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: `/** @typedef { import('./types.js').BundleCap } BundleCap */`, | ||
| output: `/** @import {BundleCap} from './types.js'; */`, | ||
| errors: [{ messageId: 'noTypedefImport' }], | ||
| }, | ||
| { | ||
| code: `/** | ||
| * @typedef { import('./kvStore.js').KVStore } KVStore | ||
| * @typedef { import('./kvStore.js').SnapshotResult } SnapshotResult | ||
| */`, | ||
| output: `/** | ||
| * @import {KVStore} from './kvStore.js'; | ||
| * @import {SnapshotResult} from './kvStore.js'; | ||
| */`, | ||
| errors: [{ messageId: 'noTypedefImport' }], | ||
| }, | ||
| { | ||
| code: `/** | ||
| * @typedef { import('./smart-wallet-kit.js').SmartWalletKit } WalletUtils | ||
| */`, | ||
| output: `/** | ||
| * @import {SmartWalletKit} from './smart-wallet-kit.js'; | ||
| */`, | ||
| errors: [{ messageId: 'noTypedefImport' }], | ||
| }, | ||
| ], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "extends": "../../tsconfig.json", | ||
| "compilerOptions": {}, | ||
| "include": [ | ||
| "src", | ||
| "test", | ||
| ], | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,6 +76,6 @@ | |
| "access": "public" | ||
| }, | ||
| "typeCoverage": { | ||
| "atLeast": 89.47 | ||
| "atLeast": 89.48 | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Skimming, mostly I see imports without typedefs and typedefs without imports. I do some combinations like
But why do we want to preserve that form rather than applying your fix? AFAICT, your fix would work, be meaning preserving, and be as much of an improvement here as it is where you do apply it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #11949 (comment) below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These lines are here because other modules import the above types from this module.
In
.tsfiles we'd just useexportbut there's nothing like that for JSDoc because this issue is open,@exporttag microsoft/TypeScript#22126In practice I think it's fine to have this rule and expect that type rollups in
.jsfiles will reduce over time.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation. No objections.