Skip to content

Commit

Permalink
fix: fixes after upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
dword-design committed Jul 4, 2020
1 parent 75c637f commit 8fe0efb
Show file tree
Hide file tree
Showing 4 changed files with 1,250 additions and 994 deletions.
14 changes: 7 additions & 7 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ const runTest = config => () => {
...config.files,
})
const lintingConfig = {
useEslintrc: false,
overrideConfig: {
extends: ['plugin:@dword-design/import-alias/recommended'],
parserOptions: {
sourceType: 'module',
ecmaVersion: 2015,
sourceType: 'module',
},
extends: ['plugin:@dword-design/import-alias/recommended'],
},
useEslintrc: false,
}
const eslintToLint = new ESLint(lintingConfig)
const eslintToFix = new ESLint({ ...lintingConfig, fix: true })
Expand All @@ -56,6 +56,10 @@ const runTest = config => () => {

export default {
'prefer-alias': {
code: endent`
import foo from '../foo/bar'
`,
filename: P.join('sub', 'index.js'),
files: {
'.babelrc.json': JSON.stringify({
plugins: [
Expand All @@ -66,10 +70,6 @@ export default {
],
}),
},
code: endent`
import foo from '../foo/bar'
`,
filename: P.join('sub', 'index.js'),
messages: [
{
message:
Expand Down
20 changes: 10 additions & 10 deletions src/rules/prefer-alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ const findMatchingAlias = (sourcePath, currentFile, options) => {
}

export default {
meta: {
type: 'suggestion',
fixable: true,
},
create: context => {
const currentFile = context.getFilename()
const folder = P.dirname(currentFile)
Expand All @@ -32,8 +28,8 @@ export default {
const manager = new OptionManager()
const babelConfig = manager.init({
babelrc: true,
root: folder,
filename: currentFile,
root: folder,
})
const plugin = babelConfig.plugins |> find({ key: 'module-resolver' })
const options = plugin.options
Expand All @@ -54,8 +50,8 @@ export default {
)
if (!matchingAlias) {
return context.report({
node,
message: `Unexpected parent import '${sourcePath}'. No matching alias found to fix the issue`,
node,
})
}
const absoluteImportPath = P.resolve(folder, sourcePath)
Expand All @@ -64,29 +60,33 @@ export default {
|> replace(/\\/g, '/')
}`
return context.report({
node,
message: `Unexpected parent import '${sourcePath}'. Use '${rewrittenImport}' instead`,
fix: fixer =>
fixer.replaceTextRange(
[node.source.range[0] + 1, node.source.range[1] - 1],
rewrittenImport
),
message: `Unexpected parent import '${sourcePath}'. Use '${rewrittenImport}' instead`,
node,
})
}
const importWithoutAlias = resolvePath(sourcePath, currentFile, options)
if (!(importWithoutAlias |> isParentImport) && hasAlias) {
return context.report({
node,
message: `Unexpected subpath import via alias '${sourcePath}'. Use '${importWithoutAlias}' instead`,
fix: fixer =>
fixer.replaceTextRange(
[node.source.range[0] + 1, node.source.range[1] - 1],
importWithoutAlias
),
message: `Unexpected subpath import via alias '${sourcePath}'. Use '${importWithoutAlias}' instead`,
node,
})
}
return undefined
},
}
},
meta: {
fixable: true,
type: 'suggestion',
},
}
Loading

0 comments on commit 8fe0efb

Please sign in to comment.