-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: stop oclif/command or flags import
- Loading branch information
Showing
3 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,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), | ||
}); | ||
} | ||
} | ||
}, | ||
} | ||
: {}; | ||
}, | ||
}); |
This file contains 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,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: '', | ||
}, | ||
], | ||
}); |