Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/test-all-packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,9 @@ jobs:
- name: yarn test (eslint-config)
if: (success() || failure())
run: cd packages/eslint-config && yarn ${{ steps.vars.outputs.test }} | $TEST_COLLECT
- name: yarn test (eslint-plugin)
if: (success() || failure())
run: cd packages/eslint-plugin && yarn ${{ steps.vars.outputs.test }} | $TEST_COLLECT
- name: yarn test (vat-data)
if: (success() || failure())
run: cd packages/vat-data && yarn ${{ steps.vars.outputs.test }} | $TEST_COLLECT
Expand Down
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export default [
...fixupConfigRules(
compat.extends(
'@agoric',
'plugin:@agoric/recommended',
'plugin:ava/recommended',
'plugin:require-extensions/recommended',
),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"ava": "^5.3.0",
"c8": "^10.1.3",
"conventional-changelog-conventionalcommits": "^9.1.0",
"eslint": "^9.32.0",
"eslint": "^9.39.1",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-jessie": "^0.0.6",
"eslint-config-prettier": "^10.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/SwingSet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@
"access": "public"
},
"typeCoverage": {
"atLeast": 76.59
"atLeast": 76.71
}
}
1 change: 1 addition & 0 deletions packages/SwingSet/src/types-external.js
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 */
Copy link
Member

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

 * @typedef { import('@agoric/swingset-liveslots').VatDeliveryObject } VatDeliveryObject
 * @typedef { import('@agoric/swingset-liveslots').VatDeliveryResult } VatDeliveryResult
 * @typedef { import('@agoric/swingset-liveslots').VatSyscallObject } VatSyscallObject
 * @typedef { import('@agoric/swingset-liveslots').VatSyscallResult } VatSyscallResult

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #11949 (comment) below

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"it's the only way to re-export with JSDoc"

Could you explain this? Why do these lines uniquely suffer from this problem?

These lines are here because other modules import the above types from this module.

In .ts files we'd just use export but there's nothing like that for JSDoc because this issue is open,

In practice I think it's fine to have this rule and expect that type rollups in .js files will reduce over time.

Copy link
Member

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.

export {};

/**
Expand Down
12 changes: 6 additions & 6 deletions packages/SwingSet/src/types-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ export {};
* @typedef { string } VatID
* @typedef { string } MeterID
* @typedef { { meterID?: MeterID } } OptMeterID
* @typedef { import('./types-external.js').BaseVatOptions } BaseVatOptions
* @typedef { import('@agoric/swingset-liveslots').VatDeliveryObject } VatDeliveryObject
* @typedef { import('@agoric/swingset-liveslots').VatDeliveryResult } VatDeliveryResult
* @typedef { import('@agoric/swingset-liveslots').VatSyscallObject } VatSyscallObject
* @typedef { import('@agoric/swingset-liveslots').VatSyscallResult } VatSyscallResult
* @typedef { import('@agoric/swingset-liveslots').VatSyscallHandler } VatSyscallHandler
* @import {BaseVatOptions} from './types-external.js';
* @import {VatDeliveryObject} from '@agoric/swingset-liveslots';
* @import {VatDeliveryResult} from '@agoric/swingset-liveslots';
* @import {VatSyscallObject} from '@agoric/swingset-liveslots';
* @import {VatSyscallResult} from '@agoric/swingset-liveslots';
* @import {VatSyscallHandler} from '@agoric/swingset-liveslots';
*
* // used by vatKeeper.setSourceAndOptions(source, RecordedVatOptions)
*
Expand Down
2 changes: 1 addition & 1 deletion packages/agoric-cli/src/commands/inter.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ $ inter bid list --from my-acct
? state.offerStatuses.entries()
: current.liveOffers;
for (const [id, spec] of entries) {
const offerStatus = state.offerStatuses.get(id) || spec;
const offerStatus = state.offerStatuses.get(String(id)) || spec;
harden(offerStatus); // coalesceWalletState should do this
// console.debug(offerStatus.invitationSpec);
if (!matches(offerStatus.invitationSpec, bidInvitationShape))
Expand Down
1 change: 1 addition & 0 deletions packages/agoric-cli/src/sdk-package-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default [
"@agoric/deploy-script-support",
"@agoric/ertp",
"@agoric/eslint-config",
"@agoric/eslint-plugin",
"@agoric/fast-usdc",
"@agoric/governance",
"@agoric/import-manager",
Expand Down
2 changes: 1 addition & 1 deletion packages/client-utils/src/wallet-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { makeSmartWalletKit } from './smart-wallet-kit.js';

/** @typedef {import('./smart-wallet-kit.js').SmartWalletKit} WalletUtils */
/** @import {SmartWalletKit} from './smart-wallet-kit.js'; */

/** @deprecated use `makeSmartWalletKit` */
export const makeWalletUtils = makeSmartWalletKit;
2 changes: 1 addition & 1 deletion packages/cosmic-swingset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@
"timeout": "20m"
},
"typeCoverage": {
"atLeast": 86.54
"atLeast": 86.47
}
}
1 change: 1 addition & 0 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"eslint-config.*"
],
"peerDependencies": {
"@agoric/eslint-plugin": "^0.1.0",
"@endo/eslint-plugin": "^2.4.0",
"@jessie.js/eslint-plugin": "^0.4.2",
"eslint": "^9.0.0",
Expand Down
10 changes: 10 additions & 0 deletions packages/eslint-plugin/eslint.config.js
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'],
},
];
33 changes: 33 additions & 0 deletions packages/eslint-plugin/package.json
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"
}
}
31 changes: 31 additions & 0 deletions packages/eslint-plugin/src/index.js
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',
},
},
},
};
147 changes: 147 additions & 0 deletions packages/eslint-plugin/src/rules/no-typedef-import.js
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 packages/eslint-plugin/tests/rules/no-typedef-import.test.js
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' }],
},
],
});
8 changes: 8 additions & 0 deletions packages/eslint-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {},
"include": [
"src",
"test",
],
}
2 changes: 1 addition & 1 deletion packages/governance/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@
"access": "public"
},
"typeCoverage": {
"atLeast": 89.47
"atLeast": 89.48
}
}
Loading
Loading