Skip to content

Commit

Permalink
Prettier v3 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
calebeby committed Jul 25, 2023
1 parent 016fecf commit 1342577
Show file tree
Hide file tree
Showing 62 changed files with 373 additions and 352 deletions.
22 changes: 14 additions & 8 deletions build.js → build.mjs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
const { writeFileSync } = require('node:fs');
const { join, sep } = require('node:path');
import { writeFileSync } from 'node:fs';
import { dirname, join, sep } from 'node:path';
import { fileURLToPath } from 'node:url';

const prettier = require('prettier');
import { format } from 'prettier';

const { configs, environments } = require('./src/config');
import configFile from './src/config.js';

const { configs, environments } = configFile;

const resolveStart = '__REQUIRE_RESOLVE__';
const resolveEnd = '__END_REQUIRE_RESOLVE__';

const __dirname = dirname(fileURLToPath(import.meta.url));

// Require.resolve needs to be dynamic and cannot be statically stringified with JSON.stringify
const stringify = (data) =>
`module.exports = ${JSON.stringify(data, (k, v) => {
if (k === 'parser' && typeof v === 'string' && v.startsWith(__dirname)) {
const pathWithoutNodeModules = v.replace(
join(__dirname, `node_modules${sep}`),
''
'',
);
// Takes the file path and changes it to just the name of the package the path was in
const packagePath = pathWithoutNodeModules.startsWith('@')
Expand All @@ -29,12 +34,13 @@ const stringify = (data) =>
})}`.replace(
// Wrap the relative parser path with require.resolve
new RegExp(`"${resolveStart}(.*?)${resolveEnd}"`, 'g'),
(_match, replacement) => `require.resolve("${replacement}")`
(_match, replacement) => `require.resolve("${replacement}")`,
);

const createFile = (data) =>
// Clean up the file so that it is readable
prettier.format(stringify(data), { parser: 'babel', singleQuote: true });
format(stringify(data), { parser: 'babel', singleQuote: true });

// Snapshots the merged config to make debugging rules easier and to reduce dependencies
writeFileSync(join('dist', 'config.js'), createFile({ configs, environments }));
const text = await createFile({ configs, environments });
writeFileSync(join('dist', 'config.js'), text);
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ function method(key, body) {
'method',
t.identifier(key),
[],
t.blockStatement(body)
t.blockStatement(body),
);
}

function takeDecorators(node) {
let result;
if (node.decorators && node.decorators.length > 0) {
result = t.arrayExpression(
node.decorators.map((decorator) => decorator.expression)
node.decorators.map((decorator) => decorator.expression),
);
}

Expand Down Expand Up @@ -58,7 +58,7 @@ function extractElementDescriptor(/* this: File, */ classRef, superRef, path) {
throw path.buildCodeFrameError(
`Private ${
isMethod ? 'methods' : 'fields'
} in decorated classes are not supported yet.`
} in decorated classes are not supported yet.`,
);
}

Expand All @@ -72,7 +72,7 @@ function extractElementDescriptor(/* this: File, */ classRef, superRef, path) {
scope,
file: this,
},
true
true,
).replace();

const properties = [
Expand All @@ -88,7 +88,7 @@ function extractElementDescriptor(/* this: File, */ classRef, superRef, path) {
properties.push(prop('value', nameFunction({ node, id, scope }) || node));
} else if (node.value) {
properties.push(
method('value', template.statements.ast`return ${node.value}`)
method('value', template.statements.ast`return ${node.value}`),
);
} else {
properties.push(prop('value', scope.buildUndefinedNode()));
Expand Down Expand Up @@ -132,7 +132,7 @@ export function buildDecoratedClass(ref, path, elements, file) {

const classDecorators = takeDecorators(node);
const definitions = t.arrayExpression(
elements.map(extractElementDescriptor.bind(file, node.id, superId))
elements.map(extractElementDescriptor.bind(file, node.id, superId)),
);

let replacement = template.expression.ast`
Expand All @@ -149,7 +149,7 @@ export function buildDecoratedClass(ref, path, elements, file) {

if (!isStrict) {
replacement.arguments[1].body.directives.push(
t.directive(t.directiveLiteral('use strict'))
t.directive(t.directiveLiteral('use strict')),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function buildPrivateNamesNodes(privateNamesMap, loose, state) {
initNodes.push(
template.statement.ast`
var ${id} = ${state.addHelper('classPrivateFieldLooseKey')}("${name}")
`
`,
);
} else if (isMethod && !isStatic) {
if (isAccessor) {
Expand Down Expand Up @@ -237,7 +237,7 @@ const privateNameHandlerSpec = {
this.receiver(member),
t.cloneNode(id),
]),
t.identifier('value')
t.identifier('value'),
);
},

Expand All @@ -260,7 +260,7 @@ const privateNameHandlerLoose = {
BASE: file.addHelper('classPrivateFieldLooseBase'),
REF: object,
PROP: privateNamesMap.get(name).id,
})
}),
);
},
};
Expand All @@ -270,7 +270,7 @@ export function transformPrivateNamesUsage(
path,
privateNamesMap,
loose,
state
state,
) {
const body = path.get('body');

Expand Down Expand Up @@ -418,8 +418,8 @@ function buildPublicFieldInitLoose(ref, prop) {
t.assignmentExpression(
'=',
t.memberExpression(ref, key, computed || t.isLiteral(key)),
value
)
value,
),
);
}

Expand All @@ -432,7 +432,7 @@ function buildPublicFieldInitSpec(ref, prop, state) {
ref,
computed || t.isLiteral(key) ? key : t.stringLiteral(key.name),
value,
])
]),
);
}

Expand Down Expand Up @@ -487,7 +487,7 @@ function buildPrivateMethodDeclaration(prop, privateNamesMap, loose = false) {
params,
body,
generator,
async
async,
);
const isGetter = getId && !getterDeclared && params.length === 0;
const isSetter = setId && !setterDeclared && params.length > 0;
Expand Down Expand Up @@ -516,7 +516,7 @@ function buildPrivateMethodDeclaration(prop, privateNamesMap, loose = false) {
return t.variableDeclaration('var', [
t.variableDeclarator(
id,
t.functionExpression(id, params, body, generator, async)
t.functionExpression(id, params, body, generator, async),
),
]);
}
Expand Down Expand Up @@ -566,7 +566,7 @@ export function buildFieldsInitNodes(
props,
privateNamesMap,
state,
loose
loose,
) {
const staticNodes = [];
const instanceNodes = [];
Expand All @@ -591,14 +591,14 @@ export function buildFieldsInitNodes(
case isStatic && isPrivate && isField && loose: {
needsClassRef = true;
staticNodes.push(
buildPrivateFieldInitLoose(t.cloneNode(ref), prop, privateNamesMap)
buildPrivateFieldInitLoose(t.cloneNode(ref), prop, privateNamesMap),
);
break;
}
case isStatic && isPrivate && isField && !loose: {
needsClassRef = true;
staticNodes.push(
buildPrivateStaticFieldInitSpec(prop, privateNamesMap)
buildPrivateStaticFieldInitSpec(prop, privateNamesMap),
);
break;
}
Expand All @@ -610,13 +610,13 @@ export function buildFieldsInitNodes(
case isStatic && isPublic && isField && !loose: {
needsClassRef = true;
staticNodes.push(
buildPublicFieldInitSpec(t.cloneNode(ref), prop, state)
buildPublicFieldInitSpec(t.cloneNode(ref), prop, state),
);
break;
}
case isInstance && isPrivate && isField && loose: {
instanceNodes.push(
buildPrivateFieldInitLoose(t.thisExpression(), prop, privateNamesMap)
buildPrivateFieldInitLoose(t.thisExpression(), prop, privateNamesMap),
);
break;
}
Expand All @@ -625,17 +625,21 @@ export function buildFieldsInitNodes(
buildPrivateInstanceFieldInitSpec(
t.thisExpression(),
prop,
privateNamesMap
)
privateNamesMap,
),
);
break;
}
case isInstance && isPrivate && isMethod && loose: {
instanceNodes.unshift(
buildPrivateMethodInitLoose(t.thisExpression(), prop, privateNamesMap)
buildPrivateMethodInitLoose(
t.thisExpression(),
prop,
privateNamesMap,
),
);
staticNodes.push(
buildPrivateMethodDeclaration(prop, privateNamesMap, loose)
buildPrivateMethodDeclaration(prop, privateNamesMap, loose),
);
break;
}
Expand All @@ -644,21 +648,21 @@ export function buildFieldsInitNodes(
buildPrivateInstanceMethodInitSpec(
t.thisExpression(),
prop,
privateNamesMap
)
privateNamesMap,
),
);
staticNodes.push(
buildPrivateMethodDeclaration(prop, privateNamesMap, loose)
buildPrivateMethodDeclaration(prop, privateNamesMap, loose),
);
break;
}
case isStatic && isPrivate && isMethod && !loose: {
needsClassRef = true;
staticNodes.push(
buildPrivateStaticFieldInitSpec(prop, privateNamesMap)
buildPrivateStaticFieldInitSpec(prop, privateNamesMap),
);
staticNodes.unshift(
buildPrivateMethodDeclaration(prop, privateNamesMap, loose)
buildPrivateMethodDeclaration(prop, privateNamesMap, loose),
);
break;
}
Expand All @@ -669,11 +673,11 @@ export function buildFieldsInitNodes(
t.cloneNode(ref),
prop,
state,
privateNamesMap
)
privateNamesMap,
),
);
staticNodes.unshift(
buildPrivateMethodDeclaration(prop, privateNamesMap, loose)
buildPrivateMethodDeclaration(prop, privateNamesMap, loose),
);
break;
}
Expand All @@ -683,7 +687,7 @@ export function buildFieldsInitNodes(
}
case isInstance && isPublic && isField && !loose: {
instanceNodes.push(
buildPublicFieldInitSpec(t.thisExpression(), prop, state)
buildPublicFieldInitSpec(t.thisExpression(), prop, state),
);
break;
}
Expand All @@ -706,7 +710,7 @@ export function buildFieldsInitNodes(
if (path.isClassExpression()) {
path.scope.push({ id: ref });
path.replaceWith(
t.assignmentExpression('=', t.cloneNode(ref), path.node)
t.assignmentExpression('=', t.cloneNode(ref), path.node),
);
} else if (!path.node.id) {
// Anonymous class declaration
Expand Down
22 changes: 12 additions & 10 deletions fixtures/babel/packages/babel-plugin-transform-for-of/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default declare((api, options) => {

if (loose === true && assumeArray === true) {
throw new Error(
`The loose and assumeArray options cannot be used together in @babel/plugin-transform-for-of`
`The loose and assumeArray options cannot be used together in @babel/plugin-transform-for-of`,
);
}

Expand Down Expand Up @@ -39,15 +39,15 @@ export default declare((api, options) => {
const item = t.memberExpression(
t.cloneNode(array),
t.cloneNode(i),
true
true,
);
let assignment;
if (t.isVariableDeclaration(left)) {
assignment = left;
assignment.declarations[0].init = item;
} else {
assignment = t.expressionStatement(
t.assignmentExpression('=', left, item)
t.assignmentExpression('=', left, item),
);
}

Expand All @@ -56,7 +56,7 @@ export default declare((api, options) => {
if (
body.isBlockStatement() &&
Object.keys(path.getBindingIdentifiers()).some((id) =>
body.scope.hasOwnBinding(id)
body.scope.hasOwnBinding(id),
)
) {
blockBody = t.blockStatement([assignment, body.node]);
Expand All @@ -71,11 +71,11 @@ export default declare((api, options) => {
t.binaryExpression(
'<',
t.cloneNode(i),
t.memberExpression(t.cloneNode(array), t.identifier('length'))
t.memberExpression(t.cloneNode(array), t.identifier('length')),
),
t.updateExpression('++', t.cloneNode(i)),
blockBody
)
blockBody,
),
);
},
},
Expand Down Expand Up @@ -133,7 +133,7 @@ export default declare((api, options) => {
const iterationValue = t.memberExpression(
t.cloneNode(right),
t.cloneNode(iterationKey),
true
true,
);

const left = node.left;
Expand All @@ -142,7 +142,9 @@ export default declare((api, options) => {
loop.body.body.unshift(left);
} else {
loop.body.body.unshift(
t.expressionStatement(t.assignmentExpression('=', left, iterationValue))
t.expressionStatement(
t.assignmentExpression('=', left, iterationValue),
),
);
}

Expand Down Expand Up @@ -175,7 +177,7 @@ export default declare((api, options) => {
const stepKey = scope.generateUid('step');
const stepValue = t.memberExpression(
t.identifier(stepKey),
t.identifier('value')
t.identifier('value'),
);

const declar = t.isVariableDeclaration(left)
Expand Down
Loading

0 comments on commit 1342577

Please sign in to comment.