Skip to content

Commit

Permalink
Handle Java keywords for applicatoinIds (#676)
Browse files Browse the repository at this point in the history
* Handle Java keywords for applicatoinIds

- Check for Java keywords when testing.
- Add _ before a keyword when generating.

See #672 for details
  • Loading branch information
andreban authored Apr 26, 2022
1 parent 8d936ea commit 24ec1ac
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/core/src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ const extractZipPromise = promisify(extractZip);
const DISALLOWED_ANDROID_PACKAGE_CHARS_REGEX = /[^a-zA-Z0-9_\.]/g;
const VALID_PACKAGE_ID_SEGMENT_REGEX = /^[a-zA-Z][A-Za-z0-9_]*$/;

// List of keywords for Java 11, as listed at
// https://docs.oracle.com/javase/specs/jls/se11/html/jls-3.html#jls-3.9.
const JAVA_KEYWORDS = [
'abstract', 'assert', 'boolean', 'break', 'byte', 'case', 'catch', 'char', 'class', 'const',
'continue', 'default', 'do', 'double', 'else', 'enum', 'extends', 'final', 'finally', 'float',
'for', 'goto', 'if', 'implements', 'import', 'instanceof', 'int', 'interface', 'long', 'native',
'new', 'package', 'private', 'protected', 'public', 'return', 'short', 'static', 'strictfp',
'super', 'switch', 'synchronized', 'this', 'throw', 'throws', 'transient', 'try', 'void',
'volatile', 'while',
];

export async function execute(
cmd: string[], env: NodeJS.ProcessEnv, log?: Log): Promise<{stdout: string; stderr: string}> {
const joinedCmd = cmd.join(' ');
Expand Down Expand Up @@ -146,6 +157,14 @@ export function generatePackageId(host: string): string | null {
if (part.trim().length === 0) {
continue;
}

// Package names cannot contain Java keywords. The recommendation is adding an '_' before the
// keyword. See https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html.
if (JAVA_KEYWORDS.includes(part)) {
packageId.push('_' + part);
continue;
}

packageId.push(part);
}

Expand Down Expand Up @@ -195,6 +214,13 @@ export function validatePackageId(input: string): string | null {
}

for (const part of parts) {
// Package names cannot contain Java keywords. The recommendation is adding an '_' before the
// keyword. See https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html.
if (JAVA_KEYWORDS.includes(part)) {
return `Invalid packageId section: "${part}". ${part} is a Java keyword and cannot be used` +
'as a package section. Consider adding an "_" before the section name.';
}

if (part.match(VALID_PACKAGE_ID_SEGMENT_REGEX) === null) {
return `Invalid packageId section: "${part}". Only alphanumeric characters and ` +
'underscore [a-zA-Z0-9_] are allowed in packageId sections. Each section must ' +
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/spec/lib/utilSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ describe('util', () => {
expect(result).toBe('com.appspot.pwa_directory_test.twa');
});

it('adds _ to section with Java keywords', () => {
const result = util.generatePackageId('new.example.com');
expect(result).toBe('com.example._new.twa');
});

it('handles input that generates empty section', () => {
expect(util.generatePackageId('.pwadirectory')).toBe('pwadirectory.twa');
expect(util.generatePackageId('pwadirectory.')).toBe('pwadirectory.twa');
Expand Down Expand Up @@ -225,6 +230,10 @@ describe('util', () => {
expect(util.validatePackageId('com.char.1twa')).not.toBeNull();
expect(util.validatePackageId('_com.char.1twa')).not.toBeNull();
});

it('package sections cannot be Java keywords', () => {
expect(util.validatePackageId('com.example.new')).not.toBeNull();
});
});

describe('rmdirs', () => {
Expand Down

0 comments on commit 24ec1ac

Please sign in to comment.