Skip to content
Prev Previous commit
Next Next commit
Reverts the parser changes
  • Loading branch information
arcanis committed Mar 24, 2022
commit 36f845bf66f53f0f2e8078d195a0b19f121b7281
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export const readSyml = async (source: PortablePath): Promise<any> => {
const fileContent = await exports.readFile(source, `utf8`);

try {
return await parseSyml(fileContent);
return parseSyml(fileContent);
} catch (error) {
throw new Error(`Invalid syml file (${source})`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ describe(`Commands`, () => {
await run(`add`, `inject-node-gyp`);

const content = await xfs.readFilePromise(`${path}/yarn.lock` as PortablePath, `utf8`);
const lock = await parseSyml(content);
const lock = parseSyml(content);

await expect(lock).toMatchObject({
expect(lock).toMatchObject({
[`inject-node-gyp@npm:^1.0.0`]: {
dependencies: {
[`node-gyp`]: `latest`,
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-essentials/sources/commands/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ async function autofixMergeConflicts(configuration: Configuration, immutable: bo
let parsedLeft;
let parsedRight;
try {
parsedLeft = await parseSyml(left);
parsedRight = await parseSyml(right);
parsedLeft = parseSyml(left);
parsedRight = parseSyml(right);
} catch (error) {
throw new ReportError(MessageName.AUTOMERGE_FAILED_TO_PARSE, `The individual variants of the lockfile failed to parse`);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-essentials/sources/commands/plugin/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const REMOTE_REGISTRY = `https://raw.githubusercontent.com/yarnpkg/berry/master/

export async function getAvailablePlugins(configuration: Configuration) {
const raw = await httpUtils.get(REMOTE_REGISTRY, {configuration});
const data = await parseSyml(raw.toString());
const data = parseSyml(raw.toString());

return data;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-nm/sources/NodeModulesLinker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ async function findInstallState(project: Project, {unrollAliases = false}: {unro
if (!xfs.existsSync(installStatePath))
return null;

const locatorState = await parseSyml(await xfs.readFilePromise(installStatePath, `utf8`));
const locatorState = parseSyml(await xfs.readFilePromise(installStatePath, `utf8`));

// If we have a higher serialized version than we can handle, ignore the state alltogether
if (locatorState.__metadata.version > STATE_FILE_VERSION)
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-version/sources/versionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export async function resolveVersionFiles(project: Project, {prerelease = null}:

const versionPath = ppath.join(deferredVersionFolder, entry);
const versionContent = await xfs.readFilePromise(versionPath, `utf8`);
const versionData = await parseSyml(versionContent);
const versionData = parseSyml(versionContent);

for (const [identStr, decision] of Object.entries(versionData.releases || {})) {
if (decision === Decision.DECLINE)
Expand Down Expand Up @@ -146,7 +146,7 @@ export async function updateVersionFiles(project: Project) {

const versionPath = ppath.join(deferredVersionFolder, entry);
const versionContent = await xfs.readFilePromise(versionPath, `utf8`);
const versionData = await parseSyml(versionContent);
const versionData = parseSyml(versionContent);

const releases = versionData?.releases;
if (!releases)
Expand Down Expand Up @@ -211,7 +211,7 @@ export async function openVersionFile(project: Project, {allowEmpty = false}: {a
? await xfs.readFilePromise(versionPath, `utf8`)
: `{}`;

const versionData = await parseSyml(versionContent);
const versionData = parseSyml(versionContent);
const releaseStore: Releases = new Map();

for (const identStr of versionData.declined || []) {
Expand Down
6 changes: 3 additions & 3 deletions packages/yarnpkg-core/sources/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ export class Configuration {

let data;
try {
data = await parseSyml(content) as any;
data = parseSyml(content) as any;
} catch (error) {
let tip = ``;

Expand All @@ -1186,7 +1186,7 @@ export class Configuration {

if (xfs.existsSync(homeRcFilePath)) {
const content = await xfs.readFilePromise(homeRcFilePath, `utf8`);
const data = await parseSyml(content) as any;
const data = parseSyml(content) as any;

return {path: homeRcFilePath, cwd: homeFolder, data};
}
Expand Down Expand Up @@ -1228,7 +1228,7 @@ export class Configuration {
const configurationPath = ppath.join(cwd, rcFilename as PortablePath);

const current = xfs.existsSync(configurationPath)
? await parseSyml(await xfs.readFilePromise(configurationPath, `utf8`)) as any
? parseSyml(await xfs.readFilePromise(configurationPath, `utf8`)) as any
: {};

let patched = false;
Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-core/sources/LegacyMigrationResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class LegacyMigrationResolver implements Resolver {
return;

const content = await xfs.readFilePromise(lockfilePath, `utf8`);
const parsed = await parseSyml(content);
const parsed = parseSyml(content);

// No need to enable it either if the lockfile is modern
if (Object.prototype.hasOwnProperty.call(parsed, `__metadata`))
Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-core/sources/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export class Project {
// We store the salted checksum of the lockfile in order to invalidate the install state when needed
this.lockFileChecksum = makeLockfileChecksum(content);

const parsed: any = await parseSyml(content);
const parsed: any = parseSyml(content);

// Protects against v1 lockfiles
if (parsed.__metadata) {
Expand Down
6 changes: 2 additions & 4 deletions packages/yarnpkg-parsers/sources/shell.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {Argument, ArgumentSegment, ArithmeticExpression, Command, CommandChain, CommandChainThen, CommandLine, CommandLineThen, EnvSegment, RedirectArgument, ShellLine, ValueArgument} from './grammars/shell';

export async function parseShell(source: string, options: {isGlobPattern: (arg: string) => boolean} = {isGlobPattern: () => false}): Promise<ShellLine> {
const {parse} = await import(`./grammars/shell`);
import {Argument, ArgumentSegment, ArithmeticExpression, Command, CommandChain, CommandChainThen, CommandLine, CommandLineThen, EnvSegment, parse, RedirectArgument, ShellLine, ValueArgument} from './grammars/shell';

export function parseShell(source: string, options: {isGlobPattern: (arg: string) => boolean} = {isGlobPattern: () => false}): ShellLine {
try {
return parse(source, options);
} catch (error) {
Expand Down
18 changes: 9 additions & 9 deletions packages/yarnpkg-parsers/sources/syml.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {safeLoad, FAILSAFE_SCHEMA} from 'js-yaml';

import {parse} from './grammars/syml';

const simpleStringPattern = /^(?![-?:,\][{}#&*!|>'"%@` \t\r\n]).([ \t]*(?![,\][{}:# \t\r\n]).)*$/;

// The following keys will always be stored at the top of the object, in the
Expand Down Expand Up @@ -124,22 +128,18 @@ export function stringifySyml(value: any) {

stringifySyml.PreserveOrdering = PreserveOrdering;

async function parseViaPeg(source: string) {
function parseViaPeg(source: string) {
if (!source.endsWith(`\n`))
source += `\n`;

const {parse} = await import('./grammars/syml');

return parse(source);
}

const LEGACY_REGEXP = /^(#.*(\r?\n))*?#\s+yarn\s+lockfile\s+v1\r?\n/i;

async function parseViaJsYaml(source: string) {
function parseViaJsYaml(source: string) {
if (LEGACY_REGEXP.test(source))
return await parseViaPeg(source);

const {default: {safeLoad, FAILSAFE_SCHEMA}} = await import('js-yaml');
return parseViaPeg(source);

const value = safeLoad(source, {
schema: FAILSAFE_SCHEMA,
Expand All @@ -160,6 +160,6 @@ async function parseViaJsYaml(source: string) {
return value as {[key: string]: string};
}

export async function parseSyml(source: string) {
return await parseViaJsYaml(source);
export function parseSyml(source: string) {
return parseViaJsYaml(source);
}
28 changes: 14 additions & 14 deletions packages/yarnpkg-parsers/tests/shell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,24 @@ const ANSI_C_STRING_ESCAPE_TESTS: Array<[string, string]> = [
describe(`Shell parser`, () => {
describe(`Valid commands`, () => {
for (const command of VALID_COMMANDS) {
it(`should parse "${command}"`, async () => {
await expect(parseShell(command)).rejects.toThrow();
it(`should parse "${command}"`, () => {
expect(() => parseShell(command)).not.toThrow();
});
}
});

describe(`Invalid commands`, () => {
for (const command of INVALID_COMMANDS) {
it(`shouldn't parse "${command}"`, async () => {
await expect(parseShell(command)).rejects.toThrow();
it(`shouldn't parse "${command}"`, () => {
expect(() => parseShell(command)).toThrow();
});
}
});

describe(`Redirections`, () => {
describe(`fd`, () => {
it(`shouldn't parse fds that aren't single digits as part of the redirection`, async () => {
await expect(parseShell(`echo 10> /dev/null`)).resolves.toStrictEqual([expect.objectContaining({
it(`shouldn't parse fds that aren't single digits as part of the redirection`, () => {
expect(parseShell(`echo 10> /dev/null`)).toStrictEqual([expect.objectContaining({
command: expect.objectContaining({
chain: expect.objectContaining({
args: [
Expand All @@ -102,8 +102,8 @@ describe(`Shell parser`, () => {
})]);
});

it(`shouldn't parse fds that aren't directly next to the redirection as part of the redirection`, async () => {
await expect(parseShell(`echo 1 > /dev/null`)).resolves.toStrictEqual([expect.objectContaining({
it(`shouldn't parse fds that aren't directly next to the redirection as part of the redirection`, () => {
expect(parseShell(`echo 1 > /dev/null`)).toStrictEqual([expect.objectContaining({
command: expect.objectContaining({
chain: expect.objectContaining({
args: [
Expand All @@ -119,9 +119,9 @@ describe(`Shell parser`, () => {
});

describe(`String parse`, () => {
it(`should parse parse double quote string currectly`, async () => {
it(`should parse parse double quote string currectly`, () => {
for (const [original, raw] of DOUBLE_QUOTE_STRING_ESCAPE_TESTS) {
await expect(parseShell(`echo "${original}"`)).resolves.toStrictEqual([expect.objectContaining({
expect(parseShell(`echo "${original}"`)).toStrictEqual([expect.objectContaining({
command: expect.objectContaining({
chain: expect.objectContaining({
args: [
Expand All @@ -134,9 +134,9 @@ describe(`Shell parser`, () => {
}
});

it(`should parse parse ANSI-C quote string currectly`, async () => {
it(`should parse parse ANSI-C quote string currectly`, () => {
for (const [original, raw] of ANSI_C_STRING_ESCAPE_TESTS) {
await expect(parseShell(`echo $'${original}'`)).resolves.toStrictEqual([expect.objectContaining({
expect(parseShell(`echo $'${original}'`)).toStrictEqual([expect.objectContaining({
command: expect.objectContaining({
chain: expect.objectContaining({
args: [
Expand Down Expand Up @@ -177,8 +177,8 @@ const STRINGIFIER_TESTS: Array<[string, string]> = [

describe(`Shell stringifier`, () => {
for (const [original, prettyPrinted] of STRINGIFIER_TESTS) {
it(`should pretty print '${original}' as '${prettyPrinted}'`, async () => {
expect(stringifyShell(await parseShell(original))).toStrictEqual(prettyPrinted);
it(`should pretty print '${original}' as '${prettyPrinted}'`, () => {
expect(stringifyShell(parseShell(original))).toStrictEqual(prettyPrinted);
});
}
});
2 changes: 1 addition & 1 deletion packages/yarnpkg-sdks/sources/generateSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class IntegrationsFile {

let data;
try {
data = await parseSyml(content || `{}`);
data = parseSyml(content || `{}`);
} catch (error) {
error.message += ` (when parsing ${path})`;
throw error;
Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-shell/sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ export async function execute(command: string, args: Array<string> = [], {
(stdin as PassThrough).end();
}

const ast = await parseShell(command, glob);
const ast = parseShell(command, glob);

// If the shell line doesn't use the args, inject it at the end of the
// right-most command
Expand Down