Skip to content

Commit

Permalink
fix(import-tkn)!: named imports to preserve all (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
idoros authored Sep 22, 2022
1 parent b415098 commit 6088f59
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 42 deletions.
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 @@ -187,7 +189,7 @@ function findImports(
star,
defaultName,
named,
tagged: tagged,
tagged,
from,
errors,
start: s.tokens[startTokenIndex].start,
Expand All @@ -199,8 +201,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 @@ -239,10 +241,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 Down Expand Up @@ -306,7 +328,7 @@ describe(`demos/import-tokenizer`, () => {
expectedAst: [
createImportValue({
star: false,
named: { b: 'b' },
named: [['b', 'b']],
tagged: {},
from: 'x',
defaultName: undefined,
Expand All @@ -324,7 +346,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: false,
named: undefined,
tagged: undefined,
tagged: {},
from: 'x',
defaultName: undefined,
errors: ['unclosed block'],
Expand All @@ -340,7 +362,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: false,
named: undefined,
tagged: undefined,
tagged: {},
from: undefined,
defaultName: undefined,
errors: [
Expand All @@ -354,7 +376,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: false,
named: undefined,
tagged: undefined,
tagged: {},
from: 'y',
defaultName: undefined,
errors: [],
Expand All @@ -370,7 +392,7 @@ describe(`demos/import-tokenizer`, () => {
createImportValue({
star: false,
named: undefined,
tagged: undefined,
tagged: {},
from: 'x',
defaultName: undefined,
errors: ['missing name'],
Expand All @@ -385,7 +407,7 @@ describe(`demos/import-tokenizer`, () => {
expectedAst: [
createImportValue({
star: true,
named: { a: 'a' },
named: [['a', 'a']],
tagged: {},
from: 'x',
defaultName: 'x',
Expand All @@ -403,8 +425,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 @@ -419,8 +446,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 @@ -435,8 +467,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 @@ -451,10 +488,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

0 comments on commit 6088f59

Please sign in to comment.