🌐 AI搜索 & 代理 主页
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
37 changes: 32 additions & 5 deletions packages/eslint-plugin/src/rules/no-deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,17 @@ export default createRule<Options, MessageIds>({
}
}

function isInsideExportOrImport(node: TSESTree.Node): boolean {
function isInsideImport(node: TSESTree.Node): boolean {
let current = node;

while (true) {
switch (current.type) {
case AST_NODE_TYPES.ExportAllDeclaration:
case AST_NODE_TYPES.ExportNamedDeclaration:
case AST_NODE_TYPES.ImportDeclaration:
return true;

case AST_NODE_TYPES.ArrowFunctionExpression:
case AST_NODE_TYPES.ExportAllDeclaration:
case AST_NODE_TYPES.ExportNamedDeclaration:
case AST_NODE_TYPES.BlockStatement:
case AST_NODE_TYPES.ClassDeclaration:
case AST_NODE_TYPES.TSInterfaceDeclaration:
Expand Down Expand Up @@ -366,11 +366,12 @@ export default createRule<Options, MessageIds>({
}

function checkIdentifier(node: IdentifierLike): void {
if (isDeclaration(node) || isInsideExportOrImport(node)) {
if (isDeclaration(node) || isInsideImport(node)) {
return;
}

const reason = getDeprecationReason(node);

if (reason == null) {
return;
}
Expand Down Expand Up @@ -440,7 +441,33 @@ export default createRule<Options, MessageIds>({
}

return {
Identifier: checkIdentifier,
Identifier(node): void {
const { parent } = node;

if (
parent.type === AST_NODE_TYPES.ExportNamedDeclaration ||
parent.type === AST_NODE_TYPES.ExportAllDeclaration
) {
return;
}

if (parent.type === AST_NODE_TYPES.ExportSpecifier) {
// only deal with the alias (exported) side, not the local binding
if (parent.exported !== node) {
return;
}

const symbol = services.getSymbolAtLocation(node);
const aliasDeprecation = getJsDocDeprecation(symbol);

if (aliasDeprecation != null) {
return;
}
}

// whether it's a plain identifier or the exported alias
checkIdentifier(node);
},
JSXIdentifier(node): void {
if (node.parent.type !== AST_NODE_TYPES.JSXClosingElement) {
checkIdentifier(node);
Expand Down
5 changes: 5 additions & 0 deletions packages/eslint-plugin/tests/fixtures/deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export {
ClassWithDeprecatedConstructor as ReexportedClassWithDeprecatedConstructor,
};

/** @deprecated Reason */
export type T = { a: string };

export type U = { b: string };

/** @deprecated */
export default {
foo: 1,
Expand Down
84 changes: 84 additions & 0 deletions packages/eslint-plugin/tests/rules/no-deprecated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ ruleTester.run('no-deprecated', rule, {
default as ts,
} from 'typescript';
`,
`
export { deprecatedFunction as 'bur' } from './deprecated';
`,
`
export { 'deprecatedFunction' } from './deprecated';
`,
`
namespace A {
/** @deprecated */
Expand Down Expand Up @@ -329,6 +335,12 @@ ruleTester.run('no-deprecated', rule, {
}
<foo bar={1} />;
`,
`
export {
/** @deprecated */
foo,
};
`,
{
code: `
/** @deprecated */
Expand Down Expand Up @@ -3274,5 +3286,77 @@ exists('/foo');
},
],
},
{
code: `
import { deprecatedFunction } from './deprecated';

export { deprecatedFunction };
`,
errors: [
{
column: 18,
endColumn: 36,
endLine: 4,
line: 4,
messageId: 'deprecated',
},
],
},
{
code: `
export { deprecatedFunction } from './deprecated';
`,
errors: [
{
column: 18,
endColumn: 36,
endLine: 2,
line: 2,
messageId: 'deprecated',
},
],
},
{
code: `
export type { T, U } from './deprecated';
`,
errors: [
{
column: 23,
endColumn: 24,
endLine: 2,
line: 2,
messageId: 'deprecatedWithReason',
},
],
},
{
code: `
export { default as foo } from './deprecated';
`,
errors: [
{
column: 29,
endColumn: 32,
endLine: 2,
line: 2,
messageId: 'deprecated',
},
],
},
{
code: `
export { deprecatedFunction as bar } from './deprecated';
`,
errors: [
{
column: 40,
endColumn: 43,
endLine: 2,
line: 2,
messageId: 'deprecated',
},
],
},
],
});
1 change: 1 addition & 0 deletions packages/typescript-eslint/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ export default {
};

export {
// eslint-disable-next-line @typescript-eslint/no-deprecated
config,
type ConfigWithExtends,
type InfiniteDepthConfigWithExtends,
Expand Down
Loading