Skip to content

Commit

Permalink
Merge branch 'use-custom-parser' of github.com:patricklx/eslint-plugi…
Browse files Browse the repository at this point in the history
…n-ember into use-custom-parser

# Conflicts:
#	lib/parsers/gjs-parser.js
#	tests/lib/rules-preprocessor/gjs-gts-processor-test.js
  • Loading branch information
patricklx committed Aug 2, 2023
2 parents 0ce038c + b00595d commit 808c7bf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
53 changes: 38 additions & 15 deletions lib/parsers/gjs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ function preprocessGlimmerTemplates(info, code) {
const ast = glimmer.preprocess(template, { mode: 'codemod' });
const allNodes = [];
glimmer.traverse(ast, {
All(node) {
All(node, path) {
const n = node;
n.parent = path.parentNode;
allNodes.push(node);
if (node.type === 'CommentStatement' || node.type === 'MustacheCommentStatement') {
comments.push(node);
Expand Down Expand Up @@ -159,6 +161,34 @@ function preprocessGlimmerTemplates(info, code) {
n.loc.start = codeLines.offsetToPosition(tpl.templateRange[0]);
n.loc.end = codeLines.offsetToPosition(tpl.templateRange[1]);
}
if ('blockParams' in n) {
n.params = [];
}
if ('blockParams' in n && n.parent) {
let part = code.slice(...n.parent.range);
let start = n.parent.range[0];
let idx = part.indexOf('|') + 1;
start += idx;
part = part.slice(idx, -1);
idx = part.indexOf('|');
part = part.slice(0, idx);
for (const param of n.blockParams) {
const regex = new RegExp(`\\b${param}\\b`);
const match = part.match(regex);
const range = [start + match.index, 0];
range[1] = range[0] + param.length;
n.params.push({
type: 'BlockParam',
name: param,
range,
parent: n,
loc: {
start: codeLines.offsetToPosition(range[0]),
end: codeLines.offsetToPosition(range[1]),
},
});
}
}
n.type = `Glimmer__${n.type}`;
allNodeTypes.add(n.type);
}
Expand All @@ -168,7 +198,6 @@ function preprocessGlimmerTemplates(info, code) {
for (const [k, v] of Object.entries(glimmerVisitorKeys)) {
templateVisitorKeys[`Glimmer__${k}`] = [...v];
}
templateVisitorKeys['Glimmer__PathExpression']?.push('identifier', 'member');
return {
templateVisitorKeys,
templateInfos,
Expand Down Expand Up @@ -209,18 +238,12 @@ function convertAst(result, preprocessedResult, visitorKeys) {
const scope = result.isTypescript
? new TypescriptScope.BlockScope(result.scopeManager, upperScope, node)
: new Scope(result.scopeManager, 'block', upperScope, node);
for (const [i, b] of node.blockParams.entries()) {
const v = new Variable(b, scope);
const nameNode = {
type: 'BlockParam',
loc: node.loc,
parent: node,
name: b,
};
v.identifiers.push(nameNode);
v.defs.push(new Definition('Parameter', nameNode, node, node, i, 'Block Param'));
for (const [i, b] of node.params.entries()) {
const v = new Variable(b.name, scope);
v.identifiers.push(b);
v.defs.push(new Definition('Parameter', b, node, node, i, 'Block Param'));
scope.variables.push(v);
scope.set.set(b, v);
scope.set.set(b.name, v);
}
}

Expand All @@ -235,10 +258,10 @@ function convertAst(result, preprocessedResult, visitorKeys) {
registerNodeInScope(node.head, scope, variable);
}
}
if (node.type === 'Glimmer__ElementNode' && isUpperCase(node.tag[0])) {
if (node.type === 'Glimmer__ElementNode') {
node.name = node.tag;
const { scope, variable } = findVarInParentScopes(result.scopeManager, path, node.tag) || {};
if (scope) {
if (scope && (variable || isUpperCase(node.tag[0]))) {
registerNodeInScope(node, scope, variable);
}
}
Expand Down
9 changes: 5 additions & 4 deletions tests/lib/rules-preprocessor/gjs-gts-processor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,17 @@ const invalid = [
filename: 'my-component.gjs',
code: `
<template>
{{#let 'x' as |noop notUsed|}}
{{#let 'x' as |noop notUsed usedEl|}}
{{noop}}
<usedEl />
{{/let}}
</template>
`,
errors: [
{
column: 37,
endColumn: 7,
endLine: 5,
column: 27,
endColumn: 34,
endLine: 3,
line: 3,
message: "'notUsed' is defined but never used.",
messageId: 'unusedVar',
Expand Down

0 comments on commit 808c7bf

Please sign in to comment.