Skip to content

Commit

Permalink
feat: stop oclif/command or flags import
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Dec 7, 2022
1 parent 1a134ab commit 8f87695
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { noDeprecatedProperties } from './rules/migration/noDeprecatedProperties
import { shouldParseFlags } from './rules/migration/shouldParseFlags';
import { noThisFlags } from './rules/migration/noThisFlags';
import { getConnectionWithVersion } from './rules/getConnectionsWithVersion';
import { noOclifFlagsCommandImport } from './rules/noOclifFlagsCommandImport';

const recommended = {
plugins: ['sf-plugin'],
rules: {
Expand All @@ -41,6 +43,7 @@ const recommended = {
'sf-plugin/flag-min-max-default': 'warn',
'sf-plugin/run-matches-class-type': 'error',
'sf-plugin/get-connection-with-version': 'warn',
'sf-plugin/no-oclif-flags-command-import': 'error',
},
};
export = {
Expand Down Expand Up @@ -83,5 +86,6 @@ export = {
'no-this-org': noThisOrg,
'no-this-flags': noThisFlags,
'get-connection-with-version': getConnectionWithVersion,
'no-oclif-flags-command-import': noOclifFlagsCommandImport,
},
};
69 changes: 69 additions & 0 deletions src/rules/noOclifFlagsCommandImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { ESLintUtils } from '@typescript-eslint/utils';
import { isInCommandDirectory } from '../shared/commands';
export const noOclifFlagsCommandImport = ESLintUtils.RuleCreator.withoutDocs({
meta: {
docs: {
description: 'Change import of flags and Command from oclif to use sf-plugins-core',
recommended: 'error',
},
messages: {
flags: 'Use Flags from sf-plugins-core',
command: 'Use SfCommand from sf-plugins-core',
empty: 'no empty imports',
},
type: 'problem',
schema: [],
fixable: 'code',
},
defaultOptions: [],
create(context) {
return isInCommandDirectory(context)
? {
ImportDeclaration(node): void {
// verify it extends SfCommand
if (node.source.value === '@oclif/core') {
node.specifiers.forEach((specifier) => {
if (specifier.local.name === 'Flags') {
context.report({
node: specifier,
messageId: 'flags',
fix: (fixer) => {
const comma = context.getSourceCode().getTokenAfter(specifier);
return comma.value === ','
? fixer.removeRange([specifier.range[0], specifier.range[1] + 1])
: fixer.remove(specifier);
},
});
}
if (specifier.local.name === 'Command') {
context.report({
node: specifier,
messageId: 'command',
fix: (fixer) => {
const comma = context.getSourceCode().getTokenAfter(specifier);
return comma.value === ','
? fixer.removeRange([specifier.range[0], specifier.range[1] + 1])
: fixer.remove(specifier);
},
});
}
});
if (node.specifiers.length === 0) {
context.report({
node,
messageId: 'empty',
fix: (fixer) => fixer.remove(node),
});
}
}
},
}
: {};
},
});
81 changes: 81 additions & 0 deletions test/rules/noOclifFlagsCommandImport.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import path from 'path';
import { ESLintUtils } from '@typescript-eslint/utils';
import { noOclifFlagsCommandImport } from '../../src/rules/noOclifFlagsCommandImport';

const ruleTester = new ESLintUtils.RuleTester({
parser: '@typescript-eslint/parser',
});

ruleTester.run('noOclifFlagsCommandImport', noOclifFlagsCommandImport, {
valid: [
{
name: 'sf command import',
filename: path.normalize('src/commands/foo.ts'),
code: "import {Flags, SfCommand} from '@salesforce/sf-plugins-core'",
},
{
name: 'other oclif imports',
filename: path.normalize('src/commands/foo.ts'),
code: "import {Interfaces} from '@oclif/core'",
},
],
invalid: [
{
name: 'import Flags and Command from @oclif/core',
filename: path.normalize('src/commands/foo.ts'),
errors: [{ messageId: 'flags' }, { messageId: 'command' }],
// it only removes one per pass. The next round would take out the `Commands` import, too
code: `
import {Flags,Command} from '@oclif/core';
import {Foo,Bar} from '@something';
import {Fooz,Barz} from '@something2';
`,
output: `
import {Command} from '@oclif/core';
import {Foo,Bar} from '@something';
import {Fooz,Barz} from '@something2';
`,
},
{
name: 'import Flags from @oclif/core',
filename: path.normalize('src/commands/foo.ts'),
errors: [{ messageId: 'flags' }],
code: "import {Flags,Interfaces} from '@oclif/core'",
output: "import {Interfaces} from '@oclif/core'",
},
{
name: 'import Flags from @oclif/core',
filename: path.normalize('src/commands/foo.ts'),
errors: [{ messageId: 'flags' }],
code: "import {Flags} from '@oclif/core'",
output: "import {} from '@oclif/core'",
},
{
name: 'import Command from @oclif/core',
filename: path.normalize('src/commands/foo.ts'),
errors: [{ messageId: 'command' }],
code: "import {Command} from '@oclif/core'",
output: "import {} from '@oclif/core'",
},
{
name: 'import Command from @oclif/core',
filename: path.normalize('src/commands/foo.ts'),
errors: [{ messageId: 'command' }],
code: "import {Command,Interfaces} from '@oclif/core'",
output: "import {Interfaces} from '@oclif/core'",
},
{
name: 'empty import should just be removed',
filename: path.normalize('src/commands/foo.ts'),
errors: [{ messageId: 'empty' }],
code: "import {} from '@oclif/core'",
output: '',
},
],
});

0 comments on commit 8f87695

Please sign in to comment.