Skip to content

Commit

Permalink
Allow mapping of expected extensions in import/extension rule
Browse files Browse the repository at this point in the history
  • Loading branch information
pwhissell committed Apr 25, 2021
1 parent e871a9a commit e4bff8e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/rules/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function buildProperties(context) {
defaultConfig: 'never',
pattern: {},
ignorePackages: false,
extensionMap: [],
};

context.options.forEach(obj => {
Expand All @@ -34,6 +35,11 @@ function buildProperties(context) {
return;
}

if (obj.extensionMap !== undefined) {
result.extensionMap = obj.extensionMap;
return;
}

// If this is not the new structure, transfer all props to result.pattern
if (obj.pattern === undefined && obj.ignorePackages === undefined) {
Object.assign(result.pattern, obj);
Expand All @@ -49,6 +55,7 @@ function buildProperties(context) {
if (obj.ignorePackages !== undefined) {
result.ignorePackages = obj.ignorePackages;
}

});

if (result.defaultConfig === 'ignorePackages') {
Expand Down Expand Up @@ -135,6 +142,10 @@ module.exports = {
return false;
}

function mapExtension(extension, extensionMap) {
return extensionMap && extensionMap[extension] ? extensionMap[extension] : extension;
}

function checkFileExtension(source) {
// bail if the declaration doesn't have a source, e.g. "export { foo };"
if (!source) return;
Expand All @@ -154,7 +165,9 @@ module.exports = {

// get extension from resolved path, if possible.
// for unresolved, use source value.
const extension = path.extname(resolvedPath || importPath).substring(1);
let extension = path.extname(resolvedPath || importPath).substring(1);

extension = mapExtension(extension, props.extensionMap);

// determine if this is a module
const isPackage = isExternalModule(
Expand Down
1 change: 1 addition & 0 deletions tests/files/typescript-import-js-extension/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const helloWorld = 'helloWorld';
5 changes: 5 additions & 0 deletions tests/files/typescript-import-js-extension/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"baseUrl": "."
}
}
67 changes: 66 additions & 1 deletion tests/src/rules/extensions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RuleTester } from 'eslint';
import path from 'path';
import rule from 'rules/extensions';
import { test, testFilePath } from '../utils';
import { getTSParsers, test, testFilePath } from '../utils';

const ruleTester = new RuleTester();

Expand Down Expand Up @@ -587,3 +588,67 @@ ruleTester.run('extensions', rule, {
}),
],
});


context('TypeScript', function () {
getTSParsers().forEach((parser) => {
ruleTester.run(`extensions`, rule, {
valid: [
test({
code: 'import { helloWorld } from "./typescript-import-js-extension/file.js"',
parser: parser,
options: ['always', { 'extensionMap': { 'ts': 'js' } }],
settings: {
'import/parsers': { [parser]: ['.ts'] },
'typescript': { 'extensions': ['.ts'], 'directory': path.resolve(__dirname, '../../files/typescript-import-js-extension/') },
},
}),
test({
code: 'import { helloWorld } from "./typescript-import-js-extension/file.ts"',
options: ['always', { 'extensionMap': { 'json': 'jsonx' } }],
settings: {
'import/resolver': {
'typescript': { 'extensions': ['.ts'], 'directory': path.resolve(__dirname, '../../files/typescript-import-js-extension/') },
},
},
}),
],
invalid: [
test({
code: 'import { helloWorld } from "./typescript-import-js-extension/file.ts"',
options: ['always', { 'extensionMap': { 'ts': 'js' } }],
settings: {
'import/resolver': {
'typescript': { 'extensions': ['.ts'], 'directory': path.resolve(__dirname, '../../files/typescript-import-js-extension/') },
},
},
errors: [
{
message: 'Missing file extension "js" for "./typescript-import-js-extension/file.ts"',
line: 1,
column: 28,
},
],
}),

test({
code: 'import { helloWorld } from "./typescript-import-js-extension/file"',
filename: testFilePath('./any/typescript/file.ts'),
options: ['always', { 'extensionMap': { 'ts': 'js' } }],
settings: {
'import/resolver': {
'typescript': { 'extensions': ['.ts'], 'directory': path.resolve(__dirname, '../../files/typescript-import-js-extension/') },
},
},
errors: [
{
message: 'Missing file extension for "./typescript-import-js-extension/file"',
line: 1,
column: 28,
},
],
}),
],
});
});
});

0 comments on commit e4bff8e

Please sign in to comment.