Skip to content

Commit

Permalink
Merge pull request #9 from RKatarine/riazanova/change-const-to-let
Browse files Browse the repository at this point in the history
Замена const-декларации на let для пропсов
  • Loading branch information
sergeche authored May 11, 2024
2 parents 9972120 + 3966a5f commit f5cc738
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"test": "uvu ./test -i samples -i fixtures"
"test": "uvu -r tsm ./test -i samples -i fixtures"
},
"devDependencies": {
"@types/estraverse": "^5.1.3",
Expand Down
21 changes: 20 additions & 1 deletion src/compiler/ComponentDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import Context from './Context';
import Scope from './Scope';
import Patcher from './Patcher';
import logger from './logger';
import { type SymbolAnalysisResult, type TemplateSource, isFunctionDeclaration, runSymbolAnalysis } from './analyze';
import {
type SymbolAnalysisResult,
type TemplateSource,
isFunctionDeclaration,
runSymbolAnalysis,
isVariableDeclaration
} from './analyze';
import parse, { type AST } from '../parser';
import compileTemplate from './template';
import compileEventHandler from './template/event';
Expand Down Expand Up @@ -277,6 +283,7 @@ export default class ComponentDeclaration {
break;
case 'prop':
chunk = `${symbol} = ${nextPropsArg}.${propName}`;
this.patchVariableDeclaration(patcher, this.scope.declarations.get(symbol))
break;
case 'rest':
// TODO поддержать rect-аргумент
Expand Down Expand Up @@ -382,6 +389,18 @@ export default class ComponentDeclaration {
}
});
}

private patchVariableDeclaration(patcher: Patcher, node?: ESTree.Node) {
if (!node || !isVariableDeclaration(node)) {
return
}
if (node.kind === 'const') {
patcher.replace({
start: node.start,
end: node.start + 5
}, `let`);
}
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ function handleIdentifier(scope: Scope, node: ESTree.Identifier, parents: ESTree
}
}

export function isVariableDeclaration(node: ESTree.Node): node is ESTree.VariableDeclaration {
return node.type === 'VariableDeclaration';
}

function declareOrAssign(node: ESTree.Node): node is ESTree.VariableDeclaration | ESTree.AssignmentExpression | ESTree.UpdateExpression {
return node.type === 'VariableDeclaration'
|| node.type === 'AssignmentExpression'
Expand Down
10 changes: 10 additions & 0 deletions test/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ test('if1.js', async () => {
same(ctx.render(), await read('./fixtures/compile/if1.js'));
});

test('const-to-let.js', async () => {
const file = await read('./samples/const-to-let.js');
const ctx = new Context(file);
const component = ctx.getComponents()[0];
const decl = new ComponentDeclaration(ctx, component);
decl.compile(ctx.patcher);

same(ctx.render(), await read('./fixtures/compile/const-to-let.js'));
});

test.run();
34 changes: 34 additions & 0 deletions test/fixtures/compile/const-to-let.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { element, attach, text, createContext, setupContext, finalizeContext } from 'endorphin/internal';
import { html } from './endorphin';

function ConstToLetComponent(props) {
createContext();
let { a } = props;
let b = props.b;

setupContext([a, b], ConstToLetComponent_template, 3 /* a | b */);
return finalizeContext((nextProps) => { props = nextProps;invalidate(0, a = nextProps.a);invalidate(1, b = nextProps.b) });
}


function ConstToLetComponent_template(ctx, stage, refs) {
const { scope, dirty } = ctx;
if (stage === 1) {
refs.length = 3;
refs[0] = element("div");
attach(refs[0]);
refs[0].appendChild(text("\n "));
refs[1] = text(scope[0]);
refs[0].appendChild(refs[1]);
refs[0].appendChild(text("\n "));
refs[2] = element("span");
refs[0].appendChild(refs[2]);
refs[2].innerText = scope[1];
refs[0].appendChild(text("\n "));
} else if (stage === 2) {
(dirty & 1 /* a */) && (refs[1].nodeValue = scope[0]);
(dirty & 2 /* b */) && (refs[2].innerText = scope[1]);
} else if (stage === 3) {
refs[0].remove();
}
}
11 changes: 11 additions & 0 deletions test/samples/const-to-let.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { html } from './endorphin';

function ConstToLetComponent(props) {
const { a } = props;
const b = props.b;

return html`<div>
${a}
<span> ${b}</span>
</div>`;
}

0 comments on commit f5cc738

Please sign in to comment.