Skip to content
Closed
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
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v16.3.0
v20.19.0
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"type": "module",
"engines": {
"node": ">=14.5.1"
"node": ">=20.19.0"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -33,5 +33,6 @@
"workspaces": [
"packages/*",
"plugins/*"
]
],
"customElements": "custom-elements.json"
}
12 changes: 4 additions & 8 deletions packages/analyzer/cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

import ts from "typescript";
import { createSourceFile } from "./src/utils/oxc-adapter.js";
import oxcAdapter from "./src/utils/oxc-adapter.js";
import path from "path";
import globby from "globby";
import fs from "fs";
Expand Down Expand Up @@ -54,16 +55,11 @@ export async function cli({

const globs = await globby(merged, { cwd });
const modules = userConfig?.overrideModuleCreation
? userConfig.overrideModuleCreation({ ts, globs })
? userConfig.overrideModuleCreation({ ts: oxcAdapter, globs })
: globs.map((glob) => {
const fullPath = path.resolve(cwd, glob);
const source = fs.readFileSync(fullPath).toString();
return ts.createSourceFile(
glob,
source,
ts.ScriptTarget.ES2015,
true
);
return createSourceFile(glob, source);
});

let thirdPartyCEMs = [];
Expand Down
6 changes: 4 additions & 2 deletions packages/analyzer/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { create } from './src/create.js';
// Export ts to avoid version mismatch when using the create() method programmatically
export { default as ts } from 'typescript';
// Export ts (oxc-based adapter) to avoid version mismatch when using the create() method programmatically
export { default as ts } from './src/utils/oxc-adapter.js';
// Export legacy() helper for wrapping plugins written against the old TypeScript-compiler-API interface
export { legacy } from './src/utils/legacy.js';
6 changes: 5 additions & 1 deletion packages/analyzer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"custom-elements-manifest": "1.0.0",
"debounce": "1.2.1",
"globby": "11.0.4",
"oxc-parser": "^0.121.0",
"typescript": "~5.4.2"
},
"devDependencies": {},
Expand All @@ -55,5 +56,8 @@
"Benny Powers <web@bennypowers.com>",
"Matias Huhta <huhta.matias@gmail.com>"
],
"customElements": "custom-elements.json"
"customElements": "custom-elements.json",
"engines": {
"node": ">=20.19.0"
}
}
2 changes: 1 addition & 1 deletion packages/analyzer/src/browser-entrypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Do not use directly, but import from ./browser/index.js
*/

import ts from 'typescript';
import ts from './utils/oxc-adapter.js';

import { create } from './create.js';
import { catalystPlugin } from './features/framework-plugins/catalyst/catalyst.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/analyzer/src/create.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ts from "typescript";
import ts from "./utils/oxc-adapter.js";
import { FEATURES } from "./features/index.js";
import { withErrorHandling } from "./utils/index.js";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { createArrowFunction } from './creators/createArrowFunction.js';
export function arrowFunctionPlugin() {
return {
name: 'CORE - ARROW-FUNCTION',
analyzePhase({ts, node, moduleDoc}){
analyzePhase({node, moduleDoc}){
switch(node.kind) {
case ts.SyntaxKind.VariableStatement:
case 'VariableStatement':
if(!isMixin(node) && hasInitializer(node)) {
const functionLike = createArrowFunction(node);
moduleDoc.declarations.push(functionLike);
Expand Down
4 changes: 2 additions & 2 deletions packages/analyzer/src/features/analyse-phase/class-jsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { has, safe } from '../../utils/index.js';
export function classJsDocPlugin() {
return {
name: 'CORE - CLASS-JSDOC',
analyzePhase({ts, node, moduleDoc}){
analyzePhase({node, moduleDoc}){
switch (node.kind) {
case ts.SyntaxKind.ClassDeclaration:
case 'ClassDeclaration':
const className = node?.name?.getText();
const classDoc = moduleDoc?.declarations?.find(declaration => declaration.name === className);

Expand Down
4 changes: 2 additions & 2 deletions packages/analyzer/src/features/analyse-phase/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { createClass } from './creators/createClass.js';
export function classPlugin() {
return {
name: 'CORE - CLASSES',
analyzePhase({ts, node, moduleDoc, context}){
analyzePhase({node, moduleDoc, context}){
switch(node.kind) {
case ts.SyntaxKind.ClassDeclaration:
case 'ClassDeclaration':
const klass = createClass(node, moduleDoc, context);
moduleDoc.declarations.push(klass);
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import ts from 'typescript';
import { handleParametersAndReturnType } from './createFunctionLike.js';
import { handleJsDoc } from './handlers.js';

export function createArrowFunction(node) {
const arrowFunction = node?.declarationList?.declarations?.find(declaration => ts.SyntaxKind.ArrowFunction === declaration?.initializer?.kind);
const arrowFunction = node?.declarationList?.declarations?.find(declaration => 'ArrowFunctionExpression' === declaration?.initializer?.kind);

let functionLikeTemplate = {
kind: 'function',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ts from 'typescript';
import { forEachChild } from '../../../utils/oxc-adapter.js';
import { createFunctionLike } from './createFunctionLike.js';
import { createAttribute, createAttributeFromField } from './createAttribute.js';
import { createField } from './createClassField.js';
Expand Down Expand Up @@ -37,9 +37,9 @@ export function createClass(node, moduleDoc, context) {
/**
* @example static observedAttributes
*/
if (ts.isPropertyDeclaration(member)) {
if (member?.kind === 'PropertyDeclaration') {
member?.initializer?.elements?.forEach((element) => {
if (ts.isStringLiteral(element)) {
if (element?.kind === 'StringLiteral') {
const attribute = createAttribute(element);
classTemplate.attributes.push(attribute);
}
Expand All @@ -49,11 +49,11 @@ export function createClass(node, moduleDoc, context) {
/**
* @example static get observedAttributes() {}
*/
if (ts.isGetAccessor(member)) {
if (member?.kind === 'GetAccessor') {
const returnStatement = member?.body?.statements?.find(isReturnStatement);

returnStatement?.expression?.elements?.forEach((element) => {
if (ts.isStringLiteral(element)) {
if (element?.kind === 'StringLiteral') {
const attribute = createAttribute(element);
classTemplate.attributes.push(attribute);
}
Expand All @@ -72,7 +72,7 @@ export function createClass(node, moduleDoc, context) {
/**
* Handle class methods
*/
if (ts.isMethodDeclaration(member) && !hasIgnoreJSDoc(member)) {
if (member?.kind === 'MethodDeclaration' && !hasIgnoreJSDoc(member)) {
const method = createFunctionLike(member);
classTemplate.members.push(method);
}
Expand All @@ -84,15 +84,15 @@ export function createClass(node, moduleDoc, context) {
const field = createField(member);

/** If a member has only a getAccessor, it means it's readonly */
if(ts.isGetAccessor(member)) {
const hasSetter = node.members.some(m => ts.isSetAccessor(m) && m.name.getText() === field.name);
if(member?.kind === 'GetAccessor') {
const hasSetter = node.members.some(m => m?.kind === 'SetAccessor' && m.name.getText() === field.name);
if(!hasSetter) {
field.readonly = true;
}
}

/** Flag class fields that get assigned a variable, so we can resolve it later (in the RESOLVE-INITIALIZERS plugin) */
if (member?.initializer?.kind === ts.SyntaxKind.Identifier) {
if (member?.initializer?.kind === 'Identifier') {
field.resolveInitializer = {
...resolveModuleOrPackageSpecifier(moduleDoc, context, member?.initializer?.getText()),
}
Expand Down Expand Up @@ -148,7 +148,7 @@ export function createClass(node, moduleDoc, context) {
*
* In order to find `this.dispatchEvent` calls, we have to traverse a method's AST
*/
if (ts.isMethodDeclaration(member)) {
if (member?.kind === 'MethodDeclaration') {
eventsVisitor(member, classTemplate);
}
});
Expand All @@ -170,12 +170,12 @@ function eventsVisitor(source, classTemplate) {

function visitNode(node) {
switch (node.kind) {
case ts.SyntaxKind.CallExpression:
case 'CallExpression':

/** If callexpression is `this.dispatchEvent` */
if (isDispatchEvent(node) && !hasIgnoreJSDoc(node.parent)) {
node?.arguments?.forEach((arg) => {
if (arg.kind === ts.SyntaxKind.NewExpression) {
if (arg.kind === 'NewExpression') {
/** e.g. `selected-changed` */
const eventName = arg?.arguments?.[0]?.text;
/**
Expand All @@ -201,7 +201,7 @@ function eventsVisitor(source, classTemplate) {
}
}

ts.forEachChild(node, visitNode);
forEachChild(node, visitNode);
}
}

Expand All @@ -210,31 +210,31 @@ export function getDefaultValuesFromConstructorVisitor(source, classTemplate, co

function visitNode(node) {
switch (node.kind) {
case ts.SyntaxKind.Constructor:
case 'Constructor':
/**
* For every member that was added in the classDoc, we want to add a default value if we can
* To do this, we visit a class's constructor, and loop through the statements
*/
node.body?.statements?.filter((statement) => statement.kind === ts.SyntaxKind.ExpressionStatement)
.filter((statement) => statement.expression.kind === ts.SyntaxKind.BinaryExpression)
node.body?.statements?.filter((statement) => statement.kind === 'ExpressionStatement')
.filter((statement) => statement.expression.kind === 'BinaryExpression')
.forEach((statement) => mapClassMember(source, classTemplate, context, node, statement, statement.expression));

break;
}

ts.forEachChild(node, visitNode);
forEachChild(node, visitNode);
}
}

function mapClassMember(source, classTemplate, context, node, statement, expression) {
let existingMember = classTemplate?.members?.find(member => expression?.left?.name?.getText() === member.name && member.kind === 'field');

// If the source is minified, or otherwise has a comma separated prop initialization
if (expression?.operatorToken?.kind === ts.SyntaxKind.CommaToken) {
if (expression.left.kind === ts.SyntaxKind.BinaryExpression) {
if (expression?.operatorToken?.kind === ',') {
if (expression.left.kind === 'BinaryExpression') {
mapClassMember(source, classTemplate, context, node, statement, expression.left);
}
if (expression.right.kind === ts.SyntaxKind.BinaryExpression) {
if (expression.right.kind === 'BinaryExpression') {
mapClassMember(source, classTemplate, context, node, statement, expression.right);
}
return;
Expand Down Expand Up @@ -270,7 +270,7 @@ function mapClassMember(source, classTemplate, context, node, statement, express
existingMember = handleDefaultValue(existingMember, statement, expression);

/** Flag class fields that get assigned a variable, so we can resolve it later (in the RESOLVE-INITIALIZERS plugin) */
if (expression?.right?.kind === ts.SyntaxKind.Identifier) {
if (expression?.right?.kind === 'Identifier') {
existingMember.resolveInitializer = {
...resolveModuleOrPackageSpecifier({ path: source.getSourceFile().fileName }, context, expression?.right?.getText()),
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ts from 'typescript';
import { has } from '../../../utils/index.js';
import { handleModifiers, handleJsDoc } from './handlers.js';

Expand All @@ -24,10 +23,10 @@ export function createFunctionLike(node) {
*/
export function handleKind(functionLike, node) {
switch(node.kind) {
case ts.SyntaxKind.FunctionDeclaration:
case 'FunctionDeclaration':
functionLike.kind = 'function';
break;
case ts.SyntaxKind.MethodDeclaration:
case 'MethodDeclaration':
functionLike.kind = 'method';
break;
}
Expand All @@ -38,28 +37,33 @@ export function handleKind(functionLike, node) {
* Handle a functionLikes return type and parameters/parameter types
*/
export function handleParametersAndReturnType(functionLike, node) {
if(node?.type) {
// In ESTree, node.type is the node discriminant string; returnTypeNode is the actual return type
const returnTypeNode = node?.returnTypeNode;
if(returnTypeNode) {
functionLike.return = {
type: { text: node.type.getText() }
type: { text: returnTypeNode.getText?.() || '' }
}
}

const parameters = [];
node?.parameters?.forEach((param) => {
// In ESTree, param.name is already a synthetic object {name, text, getText}
// (set by augmentParam). param.typeNode is the TypeScript type annotation.
const parameter = {
name: param.name.getText(),
name: typeof param.name === 'object' ? param.name.getText() : (param.name || ''),
}

if(param?.initializer) {
parameter.default = param.initializer.getText();
parameter.default = param.initializer.getText?.() || '';
}

if(param?.questionToken) {
if(param?.questionToken || param?.optional) {
parameter.optional = true;
}

if(param?.type) {
parameter.type = {text: param.type.getText() }
// Use typeNode (set by the adapter) instead of param.type (which is the ESTree discriminant)
if(param?.typeNode) {
parameter.type = {text: param.typeNode.getText?.() || ''}
}

parameters.push(parameter);
Expand Down
Loading