Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(import-tkn)!: preserve named imports #258

Merged
merged 1 commit into from
Sep 22, 2022
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
20 changes: 11 additions & 9 deletions packages/imports-parser/src/imports-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ const shouldAddToken = (type: CodeToken['type']) =>
export interface ImportValue {
star: boolean;
defaultName: string | undefined;
named: Record<string, string> | undefined;
tagged: Record<string, Record<string, string>> | undefined;
named: NamedMapping[] | undefined;
tagged: Record<string, NamedMapping[] | undefined>;
from: string | undefined;
errors: string[];
start: number;
end: number;
}

type NamedMapping = [from: string, to: string];

const isImportBlockEndError = (token: CodeToken) => token.value === 'from' || token.type === ';';

function findImports(
Expand All @@ -89,8 +91,8 @@ function findImports(
const errors = [];
let defaultName;
let star = false;
let named = undefined;
let tagged = undefined;
let named: ImportValue['named'] = undefined;
let tagged: ImportValue['tagged'] = {};
let from;
t = s.next();
if (t.type === 'string') {
Expand Down Expand Up @@ -186,7 +188,7 @@ function findImports(
star,
defaultName,
named,
tagged: tagged,
tagged,
from,
errors,
start: s.tokens[startTokenIndex].start,
Expand All @@ -198,8 +200,8 @@ function findImports(
}

function processNamedBlock(block: CodeToken[], errors: string[], taggedImportSupport: boolean) {
const named: Record<string, string> = {};
const tagged: Record<string, Record<string, string>> = {};
const named: [from: string, to: string][] = [];
const tagged: Record<string, [from: string, to: string][]> = {};
const tokens: CodeToken[] = [];

for (let i = 0; i < block.length; i++) {
Expand Down Expand Up @@ -238,10 +240,10 @@ function processNamedBlock(block: CodeToken[], errors: string[], taggedImportSup
function pushToken() {
if (tokens.length === 1) {
const name = tokens[0].value;
named[name] = name;
named.push([name, name]);
} else if (tokens.length === 3) {
if (tokens[1].value === 'as') {
named[tokens[0].value] = tokens[2].value;
named.push([tokens[0].value, tokens[2].value]);
}
}
tokens.length = 0;
Expand Down
101 changes: 68 additions & 33 deletions packages/imports-parser/test/import-tokenizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: false,
named: undefined,
tagged: undefined,
tagged: {},
from: 'x',
defaultName: undefined,
errors: [],
Expand All @@ -39,7 +39,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: false,
named: undefined,
tagged: undefined,
tagged: {},
from: 'x',
defaultName: undefined,
errors: [],
Expand All @@ -55,7 +55,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: false,
named: undefined,
tagged: undefined,
tagged: {},
from: 'x',
defaultName: undefined,
errors: ['missing semicolon'],
Expand All @@ -65,7 +65,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: false,
named: undefined,
tagged: undefined,
tagged: {},
from: 'y',
defaultName: undefined,
errors: [],
Expand All @@ -81,7 +81,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: false,
named: undefined,
tagged: undefined,
tagged: {},
from: 'x',
defaultName: undefined,
errors: [],
Expand All @@ -97,7 +97,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: false,
named: undefined,
tagged: undefined,
tagged: {},
from: 'x',
defaultName: undefined,
errors: [],
Expand All @@ -113,7 +113,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: false,
named: undefined,
tagged: undefined,
tagged: {},
from: 'x',
defaultName: undefined,
errors: [],
Expand All @@ -129,7 +129,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: false,
named: undefined,
tagged: undefined,
tagged: {},
from: 'x',
defaultName: undefined,
errors: [],
Expand All @@ -145,7 +145,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: false,
named: undefined,
tagged: undefined,
tagged: {},
from: 'x',
defaultName: 'name',
errors: [],
Expand All @@ -160,7 +160,7 @@ describe(`demos/import-tokenizer`, () => {
expectedAst: [
createImportValue({
star: false,
named: { named: 'named' },
named: [['named', 'named']],
tagged: {},
from: 'x',
defaultName: undefined,
Expand All @@ -176,7 +176,7 @@ describe(`demos/import-tokenizer`, () => {
expectedAst: [
createImportValue({
star: false,
named: { named: 'renamed' },
named: [['named', 'renamed']],
tagged: {},
from: 'x',
defaultName: undefined,
Expand All @@ -187,12 +187,31 @@ describe(`demos/import-tokenizer`, () => {
],
});
});
it(`import {x, x as local-x} from "x"`, () => {
test(`import {x, x as local-x} from "x"`, {
expectedAst: [
createImportValue({
star: false,
named: [
['x', 'x'],
['x', 'local-x'],
],
tagged: {},
from: 'x',
defaultName: undefined,
errors: [],
start: 0,
end: 33,
}),
],
});
});
it(`import name, {named} from "x"`, () => {
test(`import name, {named} from "x"`, {
expectedAst: [
createImportValue({
star: false,
named: { named: 'named' },
named: [['named', 'named']],
tagged: {},
from: 'x',
defaultName: 'name',
Expand All @@ -209,7 +228,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: true,
named: undefined,
tagged: undefined,
tagged: {},
from: 'x',
defaultName: 'name',
errors: [],
Expand All @@ -224,7 +243,10 @@ describe(`demos/import-tokenizer`, () => {
expectedAst: [
createImportValue({
star: false,
named: { named1: 'named1', named2: 'named2' },
named: [
['named1', 'named1'],
['named2', 'named2'],
],
tagged: {},
from: 'x',
defaultName: undefined,
Expand All @@ -240,7 +262,7 @@ describe(`demos/import-tokenizer`, () => {
expectedAst: [
createImportValue({
star: false,
named: { named: 'named' },
named: [['named', 'named']],
tagged: {},
from: 'x',
defaultName: undefined,
Expand All @@ -258,7 +280,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: true,
named: undefined,
tagged: undefined,
tagged: {},
from: 'x',
defaultName: undefined,
errors: ['expected as after *'],
Expand All @@ -274,7 +296,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: true,
named: undefined,
tagged: undefined,
tagged: {},
from: 'x',
defaultName: undefined,
errors: ['expected as after *', 'invalid missing from'],
Expand All @@ -290,7 +312,7 @@ describe(`demos/import-tokenizer`, () => {
expectedAst: [
createImportValue({
star: false,
named: { b: 'b' },
named: [['b', 'b']],
tagged: {},
from: 'x',
defaultName: undefined,
Expand All @@ -308,7 +330,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: false,
named: undefined,
tagged: undefined,
tagged: {},
from: 'x',
defaultName: undefined,
errors: ['unclosed block'],
Expand All @@ -324,7 +346,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: false,
named: undefined,
tagged: undefined,
tagged: {},
from: undefined,
defaultName: undefined,
errors: [
Expand All @@ -338,7 +360,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: false,
named: undefined,
tagged: undefined,
tagged: {},
from: 'y',
defaultName: undefined,
errors: [],
Expand All @@ -354,7 +376,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: false,
named: undefined,
tagged: undefined,
tagged: {},
from: 'x',
defaultName: undefined,
errors: ['missing name'],
Expand All @@ -369,7 +391,7 @@ describe(`demos/import-tokenizer`, () => {
expectedAst: [
createImportValue({
star: true,
named: { a: 'a' },
named: [['a', 'a']],
tagged: {},
from: 'x',
defaultName: 'x',
Expand All @@ -387,8 +409,13 @@ describe(`demos/import-tokenizer`, () => {
expectedAst: [
createImportValue({
star: false,
named: {},
tagged: { named: { a: 'b', c: 'c' } },
named: [],
tagged: {
named: [
['a', 'b'],
['c', 'c'],
],
},
from: 'x',
defaultName: undefined,
errors: [],
Expand All @@ -403,8 +430,13 @@ describe(`demos/import-tokenizer`, () => {
expectedAst: [
createImportValue({
star: false,
named: {},
tagged: { ',': { a: 'b', c: 'c' } },
named: [],
tagged: {
',': [
['a', 'b'],
['c', 'c'],
],
},
from: 'x',
defaultName: undefined,
errors: ['invalid tag name: ,'],
Expand All @@ -419,8 +451,13 @@ describe(`demos/import-tokenizer`, () => {
expectedAst: [
createImportValue({
star: false,
named: {},
tagged: { named: { a: 'b', c: 'c' } },
named: [],
tagged: {
named: [
['a', 'b'],
['c', 'c'],
],
},
from: 'x',
defaultName: undefined,
errors: ['unclosed tagged import "named"'],
Expand All @@ -435,10 +472,8 @@ describe(`demos/import-tokenizer`, () => {
expectedAst: [
createImportValue({
star: false,
named: {
c: 'c',
},
tagged: { tag1: { a: 'a' }, tag2: { b: 'b' } },
named: [['c', 'c']],
tagged: { tag1: [['a', 'a']], tag2: [['b', 'b']] },
from: 'x',
defaultName: undefined,
errors: [],
Expand Down